diff options
Diffstat (limited to 'deps/npm/node_modules/dezalgo/node_modules/asap/asap.js')
-rw-r--r-- | deps/npm/node_modules/dezalgo/node_modules/asap/asap.js | 162 |
1 files changed, 57 insertions, 105 deletions
diff --git a/deps/npm/node_modules/dezalgo/node_modules/asap/asap.js b/deps/npm/node_modules/dezalgo/node_modules/asap/asap.js index 2f85516cd..f04fcd58f 100644 --- a/deps/npm/node_modules/dezalgo/node_modules/asap/asap.js +++ b/deps/npm/node_modules/dezalgo/node_modules/asap/asap.js @@ -1,113 +1,65 @@ - -// Use the fastest possible means to execute a task in a future turn -// of the event loop. - -// linked list of tasks (single, with head node) -var head = {task: void 0, next: null}; -var tail = head; -var flushing = false; -var requestFlush = void 0; -var isNodeJS = false; - -function flush() { - /* jshint loopfunc: true */ - - while (head.next) { - head = head.next; - var task = head.task; - head.task = void 0; - var domain = head.domain; - - if (domain) { - head.domain = void 0; - domain.enter(); - } - - try { - task(); - - } catch (e) { - if (isNodeJS) { - // In node, uncaught exceptions are considered fatal errors. - // Re-throw them synchronously to interrupt flushing! - - // Ensure continuation if the uncaught exception is suppressed - // listening "uncaughtException" events (as domains does). - // Continue in next event to avoid tick recursion. - if (domain) { - domain.exit(); - } - setTimeout(flush, 0); - if (domain) { - domain.enter(); - } - - throw e; - - } else { - // In browsers, uncaught exceptions are not fatal. - // Re-throw them asynchronously to avoid slow-downs. - setTimeout(function() { - throw e; - }, 0); - } - } - - if (domain) { - domain.exit(); - } - } - - flushing = false; -} - -if (typeof process !== "undefined" && process.nextTick) { - // Node.js before 0.9. Note that some fake-Node environments, like the - // Mocha test runner, introduce a `process` global without a `nextTick`. - isNodeJS = true; - - requestFlush = function () { - process.nextTick(flush); - }; - -} else if (typeof setImmediate === "function") { - // In IE10, Node.js 0.9+, or https://github.com/NobleJS/setImmediate - if (typeof window !== "undefined") { - requestFlush = setImmediate.bind(window, flush); +"use strict"; + +var rawAsap = require("./raw"); +var freeTasks = []; + +/** + * Calls a task as soon as possible after returning, in its own event, with + * priority over IO events. An exception thrown in a task can be handled by + * `process.on("uncaughtException") or `domain.on("error")`, but will otherwise + * crash the process. If the error is handled, all subsequent tasks will + * resume. + * + * @param {{call}} task A callable object, typically a function that takes no + * arguments. + */ +module.exports = asap; +function asap(task) { + var rawTask; + if (freeTasks.length) { + rawTask = freeTasks.pop(); } else { - requestFlush = function () { - setImmediate(flush); - }; + rawTask = new RawTask(); } - -} else if (typeof MessageChannel !== "undefined") { - // modern browsers - // http://www.nonblocking.io/2011/06/windownexttick.html - var channel = new MessageChannel(); - channel.port1.onmessage = flush; - requestFlush = function () { - channel.port2.postMessage(0); - }; - -} else { - // old browsers - requestFlush = function () { - setTimeout(flush, 0); - }; + rawTask.task = task; + rawTask.domain = process.domain; + rawAsap(rawTask); } -function asap(task) { - tail = tail.next = { - task: task, - domain: isNodeJS && process.domain, - next: null - }; +function RawTask() { + this.task = null; + this.domain = null; +} - if (!flushing) { - flushing = true; - requestFlush(); +RawTask.prototype.call = function () { + if (this.domain) { + this.domain.enter(); + } + var threw = true; + try { + this.task.call(); + threw = false; + // If the task throws an exception (presumably) Node.js restores the + // domain stack for the next event. + if (this.domain) { + this.domain.exit(); + } + } finally { + // We use try/finally and a threw flag to avoid messing up stack traces + // when we catch and release errors. + if (threw) { + // In Node.js, uncaught exceptions are considered fatal errors. + // Re-throw them to interrupt flushing! + // Ensure that flushing continues if an uncaught exception is + // suppressed listening process.on("uncaughtException") or + // domain.on("error"). + rawAsap.requestFlush(); + } + // If the task threw an error, we do not want to exit the domain here. + // Exiting the domain would prevent the domain from catching the error. + this.task = null; + this.domain = null; + freeTasks.push(this); } }; -module.exports = asap; - |