Skip to content

Commit

Permalink
Introduce ASAP dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Oct 30, 2013
1 parent 71c5da4 commit afbd7a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 136 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"node": ">=0.6.0",
"teleport": ">=0.2.0"
},
"dependencies": {},
"dependencies": {
"asap": "~1.0.0"
},
"devDependencies": {
"jshint": "~2.1.9",
"cover": "*",
Expand Down
148 changes: 13 additions & 135 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,130 +44,7 @@ var qFileName;
// used for fallback in "allResolved"
var noop = function () {};

// Use the fastest possible means to execute a task in a future turn
// of the event loop.
var nextTick =(function () {
// linked list of tasks (single, with head node)
var head = {task: void 0, next: null};
var tail = head;
var flushing = false;
var requestTick = 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;
}

nextTick = function (task) {
tail = tail.next = {
task: task,
domain: isNodeJS && process.domain,
next: null
};

if (!flushing) {
flushing = true;
requestTick();
}
};

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;

requestTick = 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") {
requestTick = setImmediate.bind(window, flush);
} else {
requestTick = function () {
setImmediate(flush);
};
}

} else if (typeof MessageChannel !== "undefined") {
// modern browsers
// http://www.nonblocking.io/2011/06/windownexttick.html
var channel = new MessageChannel();
// At least Safari Version 6.0.5 (8536.30.1) intermittently cannot create
// working message ports the first time a page loads.
channel.port1.onmessage = function () {
requestTick = requestPortTick;
channel.port1.onmessage = flush;
flush();
};
var requestPortTick = function () {
// Opera requires us to provide a message payload, regardless of
// whether we use it.
channel.port2.postMessage(0);
};
requestTick = function () {
setTimeout(flush, 0);
requestPortTick();
};

} else {
// old browsers
requestTick = function () {
setTimeout(flush, 0);
};
}

return nextTick;
})();
var asap = require("asap");

// Attempt to make generics safe in the face of downstream
// modifications.
Expand Down Expand Up @@ -409,7 +286,8 @@ Q.resolve = Q;
* Performs a task in a future turn of the event loop.
* @param {Function} task
*/
Q.nextTick = nextTick;
// TODO deprecate
Q.nextTick = asap;

/**
* Controls whether or not long stack traces will be on
Expand Down Expand Up @@ -447,7 +325,7 @@ function defer() {
progressListeners.push(operands[1]);
}
} else {
nextTick(function () {
asap(function () {
resolvedPromise.promiseDispatch.apply(resolvedPromise, args);
});
}
Expand Down Expand Up @@ -495,7 +373,7 @@ function defer() {
promise.source = newPromise;

array_reduce(messages, function (undefined, message) {
nextTick(function () {
asap(function () {
newPromise.promiseDispatch.apply(newPromise, message);
});
}, void 0);
Expand Down Expand Up @@ -533,7 +411,7 @@ function defer() {
}

array_reduce(progressListeners, function (undefined, progressListener) {
nextTick(function () {
asap(function () {
progressListener(progress);
});
}, void 0);
Expand Down Expand Up @@ -742,7 +620,7 @@ Promise.prototype.then = function (fulfilled, rejected, progressed) {
return typeof progressed === "function" ? progressed(value) : value;
}

nextTick(function () {
asap(function () {
self.promiseDispatch(function (value) {
if (done) {
return;
Expand Down Expand Up @@ -1060,7 +938,7 @@ function fulfill(value) {
*/
function coerce(promise) {
var deferred = defer();
nextTick(function () {
asap(function () {
try {
promise.then(deferred.resolve, deferred.reject, deferred.notify);
} catch (exception) {
Expand Down Expand Up @@ -1213,7 +1091,7 @@ function dispatch(object, op, args) {
Promise.prototype.dispatch = function (op, args) {
var self = this;
var deferred = defer();
nextTick(function () {
asap(function () {
self.promiseDispatch(deferred.resolve, op, args);
});
return deferred.promise;
Expand Down Expand Up @@ -1557,7 +1435,7 @@ Promise.prototype.done = function (fulfilled, rejected, progress) {
var onUnhandledError = function (error) {
// forward to a future turn so that ``when``
// does not catch it and turn it into a rejection.
nextTick(function () {
asap(function () {
makeStackTraceLong(error, promise);
if (Q.onerror) {
Q.onerror(error);
Expand All @@ -1567,7 +1445,7 @@ Promise.prototype.done = function (fulfilled, rejected, progress) {
});
};

// Avoid unnecessary `nextTick`ing via an unnecessary `when`.
// Avoid unnecessary `asap`ing via an unnecessary `when`.
var promise = fulfilled || rejected || progress ?
this.then(fulfilled, rejected, progress) :
this;
Expand Down Expand Up @@ -1797,11 +1675,11 @@ function nodeify(object, nodeback) {
Promise.prototype.nodeify = function (nodeback) {
if (nodeback) {
this.then(function (value) {
nextTick(function () {
asap(function () {
nodeback(null, value);
});
}, function (error) {
nextTick(function () {
asap(function () {
nodeback(error);
});
});
Expand Down

0 comments on commit afbd7a8

Please sign in to comment.