Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ var process = module.exports = {};

var cachedSetTimeout;
var cachedClearTimeout;
var cachedQueueMicrotask

function defaultSetTimeout() {
throw new Error('setTimeout has not been defined');
}
function defaultClearTimeout () {
throw new Error('clearTimeout has not been defined');
}
function detectQueueMicrotask () {
if (cachedQueueMicrotask) {
return true;
}
try {
if (cachedQueueMicrotask !== queueMicrotask) {
cachedQueueMicrotask = queueMicrotask;
return true;
}
return false;
} catch (e) {
return false;
}
}
(function () {
try {
if (typeof setTimeout === 'function') {
Expand All @@ -34,6 +49,7 @@ function defaultClearTimeout () {
} catch (e) {
cachedClearTimeout = defaultClearTimeout;
}
detectQueueMicrotask();
} ())
function runTimeout(fun) {
if (cachedSetTimeout === setTimeout) {
Expand Down Expand Up @@ -138,8 +154,13 @@ process.nextTick = function (fun) {
args[i - 1] = arguments[i];
}
}

queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
if (detectQueueMicrotask()) {
return cachedQueueMicrotask(drainQueue);
}

runTimeout(drainQueue);
}
};
Expand Down