5 export var asap = function asap(callback, arg) {
10 // If len is 2, that means that we need to schedule an async flush.
11 // If additional callbacks are queued before the queue is flushed, they
12 // will be processed by this flush that we are scheduling.
13 if (customSchedulerFn) {
14 customSchedulerFn(flush);
21 export function setScheduler(scheduleFn) {
22 customSchedulerFn = scheduleFn;
25 export function setAsap(asapFn) {
29 const browserWindow = (typeof window !== 'undefined') ? window : undefined;
30 const browserGlobal = browserWindow || {};
31 const BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
32 const isNode = typeof self === 'undefined' && typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';
34 // test for web worker but not in IE10
35 const isWorker = typeof Uint8ClampedArray !== 'undefined' &&
36 typeof importScripts !== 'undefined' &&
37 typeof MessageChannel !== 'undefined';
40 function useNextTick() {
41 // node version 0.10.x displays a deprecation warning when nextTick is used recursively
42 // see https://github.com/cujojs/when/issues/410 for details
43 return () => process.nextTick(flush);
47 function useVertxTimer() {
48 if (typeof vertxNext !== 'undefined') {
54 return useSetTimeout();
57 function useMutationObserver() {
59 const observer = new BrowserMutationObserver(flush);
60 const node = document.createTextNode('');
61 observer.observe(node, { characterData: true });
64 node.data = (iterations = ++iterations % 2);
69 function useMessageChannel() {
70 const channel = new MessageChannel();
71 channel.port1.onmessage = flush;
72 return () => channel.port2.postMessage(0);
75 function useSetTimeout() {
76 // Store setTimeout reference so es6-promise will be unaffected by
77 // other code modifying setTimeout (like sinon.useFakeTimers())
78 const globalSetTimeout = setTimeout;
79 return () => globalSetTimeout(flush, 1);
82 const queue = new Array(1000);
84 for (let i = 0; i < len; i+=2) {
85 let callback = queue[i];
91 queue[i+1] = undefined;
97 function attemptVertx() {
100 const vertx = r('vertx');
101 vertxNext = vertx.runOnLoop || vertx.runOnContext;
102 return useVertxTimer();
104 return useSetTimeout();
109 // Decide what async method to use to triggering processing of queued callbacks:
111 scheduleFlush = useNextTick();
112 } else if (BrowserMutationObserver) {
113 scheduleFlush = useMutationObserver();
114 } else if (isWorker) {
115 scheduleFlush = useMessageChannel();
116 } else if (browserWindow === undefined && typeof require === 'function') {
117 scheduleFlush = attemptVertx();
119 scheduleFlush = useSetTimeout();