Skip to content

Commit

Permalink
Fixes #1022
Browse files Browse the repository at this point in the history
  • Loading branch information
petkaantonov committed Apr 12, 2016
1 parent f2a58f8 commit b8e580c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
28 changes: 17 additions & 11 deletions src/schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var schedule;
var noAsyncScheduler = function() {
throw new Error(NO_ASYNC_SCHEDULER);
};
var NativePromise = util.getNativePromise();
// This file figures out which scheduler to use for Bluebird. It normalizes
// async task scheduling across target platforms. Note that not all JS target
// platforms come supported. The scheduler is overridable with `setScheduler`.
Expand All @@ -17,6 +18,11 @@ if (util.isNode && typeof MutationObserver === "undefined") {
schedule = util.isRecentNode
? function(fn) { GlobalSetImmediate.call(global, fn); }
: function(fn) { ProcessNextTick.call(process, fn); };
} else if (typeof NativePromise === "function") {
var nativePromise = NativePromise.resolve();
schedule = function(fn) {
nativePromise.then(fn);
};
// Outside of Node, we're using MutationObservers because they provide low
// latency. The second check is to guard against iOS standalone apps which
// do not fire DOM mutation events for some reason on iOS 8.3+.
Expand All @@ -32,23 +38,23 @@ if (util.isNode && typeof MutationObserver === "undefined") {
var div2 = document.createElement("div");
var o2 = new MutationObserver(function() {
div.classList.toggle("foo");
toggleScheduled = false;
toggleScheduled = false;
});
o2.observe(div2, opts);

var scheduleToggle = function() {
if (toggleScheduled) return;
toggleScheduled = true;
div2.classList.toggle("foo");
};
toggleScheduled = true;
div2.classList.toggle("foo");
};

return function schedule(fn) {
var o = new MutationObserver(function() {
o.disconnect();
fn();
});
o.observe(div, opts);
scheduleToggle();
return function schedule(fn) {
var o = new MutationObserver(function() {
o.disconnect();
fn();
});
o.observe(div, opts);
scheduleToggle();
};
})();
// setImmediate has higher latency but is still pretty good. This is useful for
Expand Down
14 changes: 13 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,17 @@ function env(key, def) {
return isNode ? process.env[key] : def;
}

function getNativePromise() {
if (typeof Promise === "function") {
try {
var promise = new Promise(function(){});
if ({}.toString.call(promise) === "[object Promise]") {
return Promise;
}
} catch (e) {}
}
}

var ret = {
isClass: isClass,
isIdentifier: isIdentifier,
Expand Down Expand Up @@ -356,7 +367,8 @@ var ret = {
typeof chrome.loadTimes === "function",
isNode: isNode,
env: env,
global: globalObject
global: globalObject,
getNativePromise: getNativePromise
};
ret.isRecentNode = ret.isNode && (function() {
var version = process.versions.node.split(".").map(Number);
Expand Down

0 comments on commit b8e580c

Please sign in to comment.