summaryrefslogtreecommitdiff
path: root/lib/timers.js
diff options
context:
space:
mode:
authorTimothy J Fontaine <tjfontaine@gmail.com>2012-08-07 22:12:01 -0400
committerBen Noordhuis <info@bnoordhuis.nl>2012-08-12 00:07:22 +0200
commit382f22f22959e686b10a5f42333c468e6654cb97 (patch)
tree643db0defa3796d0dd94b64cb419864b800eeb9f /lib/timers.js
parentd15bfc04cde2bbfb2ce534639255dfcdab3cfcb3 (diff)
downloadnode-new-382f22f22959e686b10a5f42333c468e6654cb97.tar.gz
timers: implement setImmediate
Diffstat (limited to 'lib/timers.js')
-rw-r--r--lib/timers.js71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/timers.js b/lib/timers.js
index 03db989b11..2d50cbddec 100644
--- a/lib/timers.js
+++ b/lib/timers.js
@@ -277,3 +277,74 @@ Timeout.prototype.close = function() {
exports.unenroll(this);
}
};
+
+
+var immediateTimer = null;
+var immediateQueue = { started: false };
+L.init(immediateQueue);
+
+
+function lazyImmediateInit() { // what's in a name?
+ if (immediateTimer) return;
+ immediateTimer = new Timer;
+ immediateTimer.ontimeout = processImmediate;
+}
+
+
+function processImmediate() {
+ var immediate;
+ if (L.isEmpty(immediateQueue)) {
+ immediateTimer.stop();
+ immediateQueue.started = false;
+ } else {
+ immediate = L.shift(immediateQueue);
+
+ if (immediate.domain) immediate.domain.enter();
+
+ immediate._onTimeout();
+
+ if (immediate.domain) immediate.domain.exit();
+ }
+};
+
+
+exports.setImmediate = function(callback) {
+ var immediate = {}, args;
+
+ L.init(immediate);
+
+ immediate._onTimeout = callback;
+
+ if (arguments.length > 1) {
+ args = Array.prototype.slice.call(arguments, 1);
+ immediate._onTimeout = function() {
+ callback.apply(null, args);
+ };
+ }
+
+ if (!immediateQueue.started) {
+ lazyImmediateInit();
+ immediateTimer.start(0, 1);
+ immediateQueue.started = true;
+ }
+
+ if (process.domain) immediate.domain = process.domain;
+
+ L.append(immediateQueue, immediate);
+
+ return immediate;
+};
+
+
+exports.clearImmediate = function(immediate) {
+ if (!immediate) return;
+
+ immediate._onTimeout = undefined;
+
+ L.remove(immediate);
+
+ if (L.isEmpty(immediateQueue)) {
+ immediateTimer.stop();
+ immediateQueue.started = false;
+ }
+};