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 var browserWindow = (typeof window !== 'undefined') ? window : undefined;
30 var browserGlobal = browserWindow || {};
31 var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
32 var isNode = typeof self === 'undefined' && typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';
34 // test for web worker but not in IE10
35 var 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
44 process.nextTick(flush);
49 function useVertxTimer() {
55 function useMutationObserver() {
57 var observer = new BrowserMutationObserver(flush);
58 var node = document.createTextNode('');
59 observer.observe(node, { characterData: true });
62 node.data = (iterations = ++iterations % 2);
67 function useMessageChannel() {
68 var channel = new MessageChannel();
69 channel.port1.onmessage = flush;
71 channel.port2.postMessage(0);
75 function useSetTimeout() {
81 var queue = new Array(1000);
83 for (var i = 0; i < len; i+=2) {
84 var callback = queue[i];
90 queue[i+1] = undefined;
96 function attemptVertx() {
99 var vertx = r('vertx');
100 vertxNext = vertx.runOnLoop || vertx.runOnContext;
101 return useVertxTimer();
103 return useSetTimeout();
108 // Decide what async method to use to triggering processing of queued callbacks:
110 scheduleFlush = useNextTick();
111 } else if (BrowserMutationObserver) {
112 scheduleFlush = useMutationObserver();
113 } else if (isWorker) {
114 scheduleFlush = useMessageChannel();
115 } else if (browserWindow === undefined && typeof require === 'function') {
116 scheduleFlush = attemptVertx();
118 scheduleFlush = useSetTimeout();