Skip to content

Commit

Permalink
Add an efficient task-runner based on Promise.resolve().then if avail…
Browse files Browse the repository at this point in the history
…able.

From Raynos/observ-struct#2 (comment)
this is an efficient implementation of `EnqueueTask` on platforms which
implement `Promise` natively (even if we end up overriding the native
`Promise` to work around bugs).
  • Loading branch information
cscott committed Mar 4, 2014
1 parent c3211ab commit 9a22051
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,9 +1124,20 @@
window.addEventListener("message", handleMessage, true);
return setZeroTimeout;
};
var makePromiseAsap = function() {
// An efficient task-scheduler based on a pre-existing Promise
// implementation, which we can use even if we override the
// global Promise below (in order to workaround bugs)
// https://github.com/Raynos/observ-hash/issues/2#issuecomment-35857671
var P = globals.Promise;
return P && P.resolve && function(task) {
return P.resolve().then(task);
};
};
var enqueue = ES.IsCallable(globals.setImmediate) ?
globals.setImmediate.bind(globals) :
typeof process === 'object' && process.nextTick ? process.nextTick :
makePromiseAsap() ||
ES.IsCallable(window.postMessage) ? makeZeroTimeout() :
function(task) { setTimeout(task, 0); }; // fallback

Expand Down

0 comments on commit 9a22051

Please sign in to comment.