Commit 3987a459 authored by Stefan Penner's avatar Stefan Penner

Merge pull request #116 from lukemelia/task-queue

Add callbacks to a queue and schedule a task to flush the queue instead...
parents dd67073e e32cd162
var browserGlobal = (typeof window !== 'undefined') ? window : {}; var browserGlobal = (typeof window !== 'undefined') ? window : {};
var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
var async;
var local = (typeof global !== 'undefined') ? global : this; var local = (typeof global !== 'undefined') ? global : this;
// old node // node
function useNextTick() { function useNextTick() {
return function(callback, arg) { return function() {
process.nextTick(function() { process.nextTick(flush);
callback(arg);
});
};
}
// node >= 0.10.x
function useSetImmediate() {
return function(callback, arg) {
/* global setImmediate */
setImmediate(function(){
callback(arg);
});
}; };
} }
function useMutationObserver() { function useMutationObserver() {
var queue = []; var observer = new BrowserMutationObserver(flush);
var observer = new BrowserMutationObserver(function() {
var toProcess = queue.slice();
queue = [];
toProcess.forEach(function(tuple) {
var callback = tuple[0], arg= tuple[1];
callback(arg);
});
});
var element = document.createElement('div'); var element = document.createElement('div');
observer.observe(element, { attributes: true }); observer.observe(element, { attributes: true });
...@@ -44,28 +20,46 @@ function useMutationObserver() { ...@@ -44,28 +20,46 @@ function useMutationObserver() {
observer = null; observer = null;
}, false); }, false);
return function(callback, arg) { return function() {
queue.push([callback, arg]);
element.setAttribute('drainQueue', 'drainQueue'); element.setAttribute('drainQueue', 'drainQueue');
}; };
} }
function useSetTimeout() { function useSetTimeout() {
return function(callback, arg) { return function() {
local.setTimeout(function() { local.setTimeout(flush, 1);
callback(arg);
}, 1);
}; };
} }
if (typeof setImmediate === 'function') { var queue = [];
async = useSetImmediate(); function flush() {
} else if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') { for (var i = 0; i < queue.length; i++) {
async = useNextTick(); var tuple = queue[i];
var callback = tuple[0], arg = tuple[1];
callback(arg);
}
queue = [];
}
var scheduleFlush;
// Decide what async method to use to triggering processing of queued callbacks:
if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
scheduleFlush = useNextTick();
} else if (BrowserMutationObserver) { } else if (BrowserMutationObserver) {
async = useMutationObserver(); scheduleFlush = useMutationObserver();
} else { } else {
async = useSetTimeout(); scheduleFlush = useSetTimeout();
}
function async(callback, arg) {
var length = queue.push([callback, arg]);
if (length === 1) {
// If length is 1, that means that we need to schedule an async flush.
// If additional callbacks are queued before the queue is flushed, they
// will be processed by this flush that we are scheduling.
scheduleFlush();
}
} }
export { async }; export { async };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment