Use zeroTimeout bounded to the window.

This commit is contained in:
Irakli Gozalishvili 2011-09-09 14:56:15 +02:00
commit 738e7c1a0b

View file

@ -20,6 +20,7 @@
* *
* Contributor(s): * Contributor(s):
* Fabian Jakobs <fabian AT ajax DOT org> * Fabian Jakobs <fabian AT ajax DOT org>
* Irakli Gozalishvili <rfobic@gmail.com> (http://jeditoolkit.com)
* *
* Alternatively, the contents of this file may be used under the terms of * Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or * either the GNU General Public License Version 2 or later (the "GPL"), or
@ -39,10 +40,11 @@ define(function(require, exports, module) {
var event = require("pilot/event"); var event = require("pilot/event");
var RenderLoop = function(onRender) { var RenderLoop = function(onRender, window) {
this.onRender = onRender; this.onRender = onRender;
this.pending = false; this.pending = false;
this.changes = 0; this.changes = 0;
this.setTimeoutZero = this.setTimeoutZero.bind(window);
}; };
(function() { (function() {
@ -70,31 +72,31 @@ var RenderLoop = function(onRender) {
window.msRequestAnimationFrame; window.msRequestAnimationFrame;
if (this.setTimeoutZero) { if (this.setTimeoutZero) {
this.setTimeoutZero = this.setTimeoutZero;
this.setTimeoutZero = this.setTimeoutZero.bind(window)
} else if (window.postMessage) { } else if (window.postMessage) {
this.messageName = "zero-timeout-message"; this.setTimeoutZero = (function(messageName, attached, listener) {
return function setTimeoutZero(callback) {
// Set up listener if not listening already.
if (!attached) {
event.addListener(this, "message", function(e) {
if (listener && e.data == messageName) {
event.stopPropagation(e);
listener();
}
});
attached = true;
}
this.setTimeoutZero = function(callback) { listener = callback;
if (!this.attached) { this.postMessage(messageName, "*");
var _self = this; };
event.addListener(window, "message", function(e) { })("zero-timeout-message", false, null);
if (_self.callback && e.data == _self.messageName) {
event.stopPropagation(e);
_self.callback();
}
});
this.attached = true;
}
this.callback = callback;
window.postMessage(this.messageName, "*");
}
} else { } else {
this.setTimeoutZero = function(callback) { this.setTimeoutZero = function(callback) {
setTimeout(callback, 0); this.setTimeout(callback, 0);
} }
} }