Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 74eff1c

Browse files
committed
feat(scheduling): Support Promise.then() fallbacks to enqueue a microtask
1 parent eef1a59 commit 74eff1c

File tree

4 files changed

+190
-30
lines changed

4 files changed

+190
-30
lines changed

dist/zone-microtask.js

+91-13
Original file line numberDiff line numberDiff line change
@@ -1111,21 +1111,9 @@ module.exports = {
11111111

11121112
}).call(this,{},typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
11131113
},{}],4:[function(require,module,exports){
1114+
(function (global){
11141115
'use strict';
11151116

1116-
// TODO(vicb) asap implementation should work if Promise is not available
1117-
// es6-promise has fallbacks
1118-
1119-
if (typeof Promise === "undefined" || Promise.toString().indexOf("[native code]") === -1) {
1120-
throw new Error('Promise is not available natively !')
1121-
}
1122-
1123-
var resolvedPromise = Promise.resolve(void 0);
1124-
1125-
function asap(fn) {
1126-
resolvedPromise.then(fn);
1127-
}
1128-
11291117
function scheduleMicrotask(fn) {
11301118
asap(this.bind(fn));
11311119
}
@@ -1139,6 +1127,96 @@ module.exports = {
11391127
addMicrotaskSupport: addMicrotaskSupport
11401128
};
11411129

1130+
// TODO(vicb): There are plan to be able to use asap() from es6-promise
1131+
// see https://github.com/jakearchibald/es6-promise/pull/113
1132+
// for now adapt code from asap.js in es6-promise
1133+
// Note: the node support has been dropped here
1134+
1135+
// TODO(vicb): Create a benchmark for the different methods & the usage of the queue
1136+
// see https://github.com/angular/zone.js/issues/97
1137+
1138+
var hasNativePromise = false;
1139+
var len = 0;
1140+
1141+
if (typeof Promise ==! "undefined" && Promise.toString().indexOf("[native code]") !== -1) {
1142+
hasNativePromise = true;
1143+
}
1144+
1145+
function asap(callback) {
1146+
queue[len] = callback;
1147+
len += 1;
1148+
if (len === 1) {
1149+
scheduleFlush();
1150+
}
1151+
}
1152+
1153+
var browserWindow = (typeof global.window !== 'undefined') ? global.window : undefined;
1154+
var browserGlobal = browserWindow || {};
1155+
var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
1156+
1157+
// test for web worker but not in IE10
1158+
var isWorker = typeof Uint8ClampedArray !== 'undefined' &&
1159+
typeof importScripts !== 'undefined' &&
1160+
typeof MessageChannel !== 'undefined';
1161+
1162+
function useMutationObserver() {
1163+
var iterations = 0;
1164+
var observer = new BrowserMutationObserver(flush);
1165+
var node = document.createTextNode('');
1166+
observer.observe(node, { characterData: true });
1167+
1168+
return function() {
1169+
node.data = (iterations = ++iterations % 2);
1170+
};
1171+
}
1172+
1173+
// web worker
1174+
function useMessageChannel() {
1175+
var channel = new MessageChannel();
1176+
channel.port1.onmessage = flush;
1177+
return function () {
1178+
channel.port2.postMessage(0);
1179+
};
1180+
}
1181+
1182+
function useSetTimeout() {
1183+
return function() {
1184+
setTimeout(flush, 1);
1185+
};
1186+
}
1187+
1188+
function usePromise() {
1189+
var resolvedPromise = Promise.resolve(void 0);
1190+
return function() {
1191+
resolvedPromise.then(flush);
1192+
}
1193+
}
1194+
1195+
var queue = new Array(1000);
1196+
1197+
function flush() {
1198+
for (var i = 0; i < len; i++) {
1199+
var callback = queue[i];
1200+
callback();
1201+
queue[i] = undefined;
1202+
}
1203+
1204+
len = 0;
1205+
}
1206+
1207+
var scheduleFlush;
1208+
// Decide what async method to use to triggering processing of queued callbacks:
1209+
if (hasNativePromise) {
1210+
scheduleFlush = usePromise();
1211+
} else if (BrowserMutationObserver) {
1212+
scheduleFlush = useMutationObserver();
1213+
} else if (isWorker) {
1214+
scheduleFlush = useMessageChannel();
1215+
} else {
1216+
scheduleFlush = useSetTimeout();
1217+
}
1218+
1219+
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
11421220
},{}],5:[function(require,module,exports){
11431221
(function (global){
11441222
'use strict';

0 commit comments

Comments
 (0)