Skip to content

Commit

Permalink
Add a faster setTimeout(0) implementation for Promises.
Browse files Browse the repository at this point in the history
On Chrome 27/28, setTimeout(0) is throttled to as long as 1Hz, which makes
our tests fail.  Use postMessage where possible for a true immediate dispatch.
  • Loading branch information
cscott committed Feb 28, 2014
1 parent 4c4d683 commit d990901
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,29 @@

// find an appropriate setImmediate-alike
var setTimeout = globals.setTimeout;
var makeZeroTimeout = function() {
// from http://dbaron.org/log/20100309-faster-timeouts
var timeouts = [];
var messageName = "zero-timeout-message";
var setZeroTimeout = function(fn) {
timeouts.push(fn);
window.postMessage(messageName, "*");
};
var handleMessage = function(event) {
if (event.source == window && event.data == messageName) {
event.stopPropagation();
if (timeouts.length === 0) { return; }
var fn = timeouts.shift();
fn();
}
};
window.addEventListener("message", handleMessage, true);
return setZeroTimeout;
};
var enqueue = ES.IsCallable(globals.setImmediate) ?
globals.setImmediate.bind(globals) :
typeof process === 'object' && process.nextTick ? process.nextTick :
ES.IsCallable(window.postMessage) ? makeZeroTimeout() :
function(task) { setTimeout(task, 0); }; // fallback

var triggerPromiseReactions = function(reactions, x) {
Expand Down

0 comments on commit d990901

Please sign in to comment.