2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
const TimerWrap = process.binding('timer_wrap').Timer;
|
2015-09-26 21:27:36 +00:00
|
|
|
const L = require('internal/linkedlist');
|
2016-02-26 19:18:52 +00:00
|
|
|
const assert = require('assert');
|
2014-12-16 22:17:28 +00:00
|
|
|
const util = require('util');
|
|
|
|
const debug = util.debuglog('timer');
|
2016-02-26 19:18:52 +00:00
|
|
|
const kOnTimeout = TimerWrap.kOnTimeout | 0;
|
2013-08-15 17:23:36 +00:00
|
|
|
|
2011-07-11 22:30:24 +00:00
|
|
|
// Timeout values > TIMEOUT_MAX are set to 1.
|
2015-01-21 16:36:59 +00:00
|
|
|
const TIMEOUT_MAX = 2147483647; // 2^31-1
|
2011-07-11 22:30:24 +00:00
|
|
|
|
2011-01-13 10:09:03 +00:00
|
|
|
// IDLE TIMEOUTS
|
2016-02-26 19:18:52 +00:00
|
|
|
// Object maps containing linked lists of timers, keyed and sorted by their
|
|
|
|
// duration in milliseconds.
|
2011-01-13 10:09:03 +00:00
|
|
|
//
|
|
|
|
// Because often many sockets will have the same idle timeout we will not
|
|
|
|
// use one timeout watcher per item. It is too much overhead. Instead
|
|
|
|
// we'll use a single watcher for all sockets with the same timeout value
|
|
|
|
// and a linked list. This technique is described in the libev manual:
|
|
|
|
// http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Be_smart_about_timeouts
|
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
const refedLists = {};
|
|
|
|
const unrefedLists = {};
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2015-10-01 05:18:36 +00:00
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
// Schedule or re-schedule a timer.
|
|
|
|
// The item must have been enroll()'d first.
|
2015-10-01 05:18:36 +00:00
|
|
|
exports.active = function(item) {
|
2016-02-26 19:18:52 +00:00
|
|
|
insert(item, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Internal APIs that need timeouts should use `_unrefActive()` instead of
|
|
|
|
// `active()` so that they do not unnecessarily keep the process open.
|
|
|
|
exports._unrefActive = function(item) {
|
|
|
|
insert(item, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// The underlying logic for scheduling or re-scheduling a timer.
|
|
|
|
function insert(item, unrefed) {
|
2015-10-01 05:18:36 +00:00
|
|
|
const msecs = item._idleTimeout;
|
2015-10-12 23:03:59 +00:00
|
|
|
if (msecs < 0 || msecs === undefined) return;
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
item._idleStart = TimerWrap.now();
|
2015-10-01 05:18:36 +00:00
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
const lists = unrefed === true ? unrefedLists : refedLists;
|
2011-06-08 02:35:50 +00:00
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
var list = lists[msecs];
|
|
|
|
if (!list) {
|
|
|
|
debug('no %d list was found in insert, creating a new one', msecs);
|
|
|
|
list = new TimersList(msecs, unrefed);
|
2011-01-18 22:26:32 +00:00
|
|
|
L.init(list);
|
2016-02-26 19:18:52 +00:00
|
|
|
list._timer._list = list;
|
|
|
|
|
|
|
|
if (unrefed === true) list._timer.unref();
|
|
|
|
list._timer.start(msecs, 0);
|
2010-10-26 18:56:32 +00:00
|
|
|
|
|
|
|
lists[msecs] = list;
|
2016-02-26 19:18:52 +00:00
|
|
|
list._timer[kOnTimeout] = listOnTimeout;
|
2012-12-27 19:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
L.append(list, item);
|
|
|
|
assert(!L.isEmpty(list)); // list is not empty
|
2016-02-26 19:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function TimersList(msecs, unrefed) {
|
|
|
|
this._idleNext = null; // Create the list with the linkedlist properties to
|
|
|
|
this._idlePrev = null; // prevent any unnecessary hidden class changes.
|
|
|
|
this._timer = new TimerWrap();
|
|
|
|
this._unrefed = unrefed;
|
|
|
|
this.msecs = msecs;
|
|
|
|
}
|
2012-12-27 19:40:42 +00:00
|
|
|
|
|
|
|
function listOnTimeout() {
|
2016-02-26 19:18:52 +00:00
|
|
|
var list = this._list;
|
|
|
|
var msecs = list.msecs;
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2013-05-21 22:22:05 +00:00
|
|
|
debug('timeout callback %d', msecs);
|
2012-12-27 19:40:42 +00:00
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
var now = TimerWrap.now();
|
2013-05-21 22:22:05 +00:00
|
|
|
debug('now: %s', now);
|
2012-12-27 19:40:42 +00:00
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
var diff, timer;
|
|
|
|
while (timer = L.peek(list)) {
|
|
|
|
diff = now - timer._idleStart;
|
|
|
|
|
2012-10-26 05:49:22 +00:00
|
|
|
if (diff < msecs) {
|
2016-02-26 19:18:52 +00:00
|
|
|
this.start(msecs - diff, 0);
|
2013-05-21 22:22:05 +00:00
|
|
|
debug('%d list wait because diff is %d', msecs, diff);
|
2012-12-27 19:40:42 +00:00
|
|
|
return;
|
2016-02-26 19:18:52 +00:00
|
|
|
}
|
|
|
|
|
2012-12-27 19:40:42 +00:00
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
L.remove(timer);
|
|
|
|
assert(timer !== L.peek(list));
|
2012-12-27 19:40:42 +00:00
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
if (!timer._onTimeout) continue;
|
|
|
|
|
|
|
|
var domain = timer.domain;
|
|
|
|
if (domain) {
|
|
|
|
|
|
|
|
// If the timer callback throws and the
|
2012-12-27 19:40:42 +00:00
|
|
|
// domain or uncaughtException handler ignore the exception,
|
|
|
|
// other timers that expire on this tick should still run.
|
|
|
|
//
|
2016-02-26 19:18:52 +00:00
|
|
|
// https://github.com/nodejs/node-v0.x-archive/issues/2631
|
|
|
|
if (domain._disposed)
|
2013-09-24 21:12:11 +00:00
|
|
|
continue;
|
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
domain.enter();
|
2012-12-27 19:40:42 +00:00
|
|
|
}
|
2016-02-26 19:18:52 +00:00
|
|
|
|
|
|
|
tryOnTimeout(timer, list);
|
|
|
|
|
|
|
|
if (domain)
|
|
|
|
domain.exit();
|
2010-10-26 18:56:32 +00:00
|
|
|
}
|
|
|
|
|
2013-05-21 22:22:05 +00:00
|
|
|
debug('%d list empty', msecs);
|
2012-12-27 19:40:42 +00:00
|
|
|
assert(L.isEmpty(list));
|
2016-02-26 19:18:52 +00:00
|
|
|
this.close();
|
|
|
|
if (list._unrefed === true) {
|
|
|
|
delete unrefedLists[msecs];
|
|
|
|
} else {
|
|
|
|
delete refedLists[msecs];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// An optimization so that the try/finally only de-optimizes what is in this
|
|
|
|
// smaller function.
|
|
|
|
function tryOnTimeout(timer, list) {
|
|
|
|
timer._called = true;
|
|
|
|
var threw = true;
|
|
|
|
try {
|
|
|
|
timer._onTimeout();
|
|
|
|
threw = false;
|
|
|
|
} finally {
|
|
|
|
if (!threw) return;
|
|
|
|
|
|
|
|
// We need to continue processing after domain error handling
|
|
|
|
// is complete, but not by using whatever domain was left over
|
|
|
|
// when the timeout threw its exception.
|
|
|
|
const domain = process.domain;
|
|
|
|
process.domain = null;
|
|
|
|
process.nextTick(listOnTimeoutNT, list);
|
|
|
|
process.domain = domain;
|
|
|
|
}
|
2010-10-26 18:56:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-04 18:40:25 +00:00
|
|
|
function listOnTimeoutNT(list) {
|
2016-02-26 19:18:52 +00:00
|
|
|
list._timer[kOnTimeout]();
|
2015-05-04 18:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-16 19:34:15 +00:00
|
|
|
function reuse(item) {
|
2011-01-18 22:26:32 +00:00
|
|
|
L.remove(item);
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
var list = refedLists[item._idleTimeout];
|
2015-10-16 19:34:15 +00:00
|
|
|
// if empty - reuse the watcher
|
2011-01-18 22:26:32 +00:00
|
|
|
if (list && L.isEmpty(list)) {
|
2015-10-16 19:34:15 +00:00
|
|
|
debug('reuse hit');
|
2016-02-26 19:18:52 +00:00
|
|
|
list._timer.stop();
|
|
|
|
delete refedLists[item._idleTimeout];
|
|
|
|
return list._timer;
|
2015-10-16 19:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const unenroll = exports.unenroll = function(item) {
|
2016-02-26 19:18:52 +00:00
|
|
|
var handle = reuse(item);
|
|
|
|
if (handle) {
|
2011-01-13 10:22:09 +00:00
|
|
|
debug('unenroll: list empty');
|
2016-02-26 19:18:52 +00:00
|
|
|
handle.close();
|
2010-10-26 18:56:32 +00:00
|
|
|
}
|
2011-12-22 13:42:20 +00:00
|
|
|
// if active is called later, then we want to make sure not to insert again
|
|
|
|
item._idleTimeout = -1;
|
2010-10-26 18:56:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Does not start the time, just sets up the members needed.
|
2010-12-02 04:59:06 +00:00
|
|
|
exports.enroll = function(item, msecs) {
|
2014-12-16 22:17:28 +00:00
|
|
|
if (typeof msecs !== 'number') {
|
2015-10-14 21:10:25 +00:00
|
|
|
throw new TypeError('"msecs" argument must be a number');
|
2014-12-16 22:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (msecs < 0 || !isFinite(msecs)) {
|
2015-10-14 21:10:25 +00:00
|
|
|
throw new RangeError('"msecs" argument must be ' +
|
|
|
|
'a non-negative finite number');
|
2014-12-16 22:17:28 +00:00
|
|
|
}
|
|
|
|
|
2010-10-26 19:14:17 +00:00
|
|
|
// if this item was already in a list somewhere
|
2010-10-26 18:56:32 +00:00
|
|
|
// then we should unenroll it from that
|
2010-10-26 19:14:17 +00:00
|
|
|
if (item._idleNext) unenroll(item);
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2013-03-21 17:38:30 +00:00
|
|
|
// Ensure that msecs fits into signed int32
|
2014-12-16 22:17:28 +00:00
|
|
|
if (msecs > TIMEOUT_MAX) {
|
|
|
|
msecs = TIMEOUT_MAX;
|
2013-03-21 17:38:30 +00:00
|
|
|
}
|
|
|
|
|
2010-10-26 19:14:17 +00:00
|
|
|
item._idleTimeout = msecs;
|
2011-01-18 22:26:32 +00:00
|
|
|
L.init(item);
|
2010-10-26 18:56:32 +00:00
|
|
|
};
|
|
|
|
|
2011-01-13 10:22:09 +00:00
|
|
|
|
2010-10-26 19:52:31 +00:00
|
|
|
/*
|
|
|
|
* DOM-style timers
|
|
|
|
*/
|
2010-10-26 19:14:17 +00:00
|
|
|
|
|
|
|
|
2015-03-20 15:55:19 +00:00
|
|
|
exports.setTimeout = function(callback, after) {
|
2015-12-20 11:30:04 +00:00
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new TypeError('"callback" argument must be a function');
|
|
|
|
}
|
|
|
|
|
2012-07-01 20:58:22 +00:00
|
|
|
after *= 1; // coalesce to number or NaN
|
|
|
|
|
|
|
|
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
|
2011-07-11 22:30:24 +00:00
|
|
|
after = 1; // schedule on next tick, follows browser behaviour
|
|
|
|
}
|
2011-06-08 02:35:50 +00:00
|
|
|
|
2015-03-20 15:55:19 +00:00
|
|
|
var timer = new Timeout(after);
|
|
|
|
var length = arguments.length;
|
|
|
|
var ontimeout = callback;
|
|
|
|
switch (length) {
|
2015-01-10 15:49:21 +00:00
|
|
|
// fast cases
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
case 3:
|
2015-11-26 15:55:05 +00:00
|
|
|
ontimeout = () => callback.call(timer, arguments[2]);
|
2015-01-10 15:49:21 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2015-11-26 15:55:05 +00:00
|
|
|
ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
|
2015-01-10 15:49:21 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2015-03-20 15:55:19 +00:00
|
|
|
ontimeout =
|
2015-11-26 15:55:05 +00:00
|
|
|
() => callback.call(timer, arguments[2], arguments[3], arguments[4]);
|
2015-01-10 15:49:21 +00:00
|
|
|
break;
|
|
|
|
// slow case
|
|
|
|
default:
|
2015-03-20 15:55:19 +00:00
|
|
|
var args = new Array(length - 2);
|
|
|
|
for (var i = 2; i < length; i++)
|
2015-01-10 15:49:21 +00:00
|
|
|
args[i - 2] = arguments[i];
|
2015-11-26 15:55:05 +00:00
|
|
|
ontimeout = () => callback.apply(timer, args);
|
2015-01-10 15:49:21 +00:00
|
|
|
break;
|
2010-10-26 19:52:31 +00:00
|
|
|
}
|
2015-03-20 15:55:19 +00:00
|
|
|
timer._onTimeout = ontimeout;
|
2010-10-26 19:14:17 +00:00
|
|
|
|
2012-04-06 23:26:18 +00:00
|
|
|
if (process.domain) timer.domain = process.domain;
|
|
|
|
|
2011-07-11 22:30:24 +00:00
|
|
|
exports.active(timer);
|
|
|
|
|
2010-10-26 19:14:17 +00:00
|
|
|
return timer;
|
|
|
|
};
|
|
|
|
|
2010-10-26 19:52:31 +00:00
|
|
|
|
2010-12-02 04:59:06 +00:00
|
|
|
exports.clearTimeout = function(timer) {
|
2013-08-15 17:23:36 +00:00
|
|
|
if (timer && (timer[kOnTimeout] || timer._onTimeout)) {
|
|
|
|
timer[kOnTimeout] = timer._onTimeout = null;
|
2013-03-21 11:19:03 +00:00
|
|
|
if (timer instanceof Timeout) {
|
2011-06-08 02:35:50 +00:00
|
|
|
timer.close(); // for after === 0
|
|
|
|
} else {
|
|
|
|
exports.unenroll(timer);
|
|
|
|
}
|
2010-10-29 07:00:43 +00:00
|
|
|
}
|
2010-10-26 19:52:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-20 16:20:43 +00:00
|
|
|
exports.setInterval = function(callback, repeat) {
|
2015-12-20 11:30:04 +00:00
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new TypeError('"callback" argument must be a function');
|
|
|
|
}
|
|
|
|
|
2012-07-01 20:58:22 +00:00
|
|
|
repeat *= 1; // coalesce to number or NaN
|
|
|
|
|
|
|
|
if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) {
|
2011-07-11 22:30:24 +00:00
|
|
|
repeat = 1; // schedule on next tick, follows browser behaviour
|
|
|
|
}
|
|
|
|
|
2013-03-21 11:19:03 +00:00
|
|
|
var timer = new Timeout(repeat);
|
2015-03-20 16:20:43 +00:00
|
|
|
var length = arguments.length;
|
|
|
|
var ontimeout = callback;
|
|
|
|
switch (length) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
case 3:
|
2015-11-26 15:55:05 +00:00
|
|
|
ontimeout = () => callback.call(timer, arguments[2]);
|
2015-03-20 16:20:43 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2015-11-26 15:55:05 +00:00
|
|
|
ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
|
2015-03-20 16:20:43 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
ontimeout =
|
2015-11-26 15:55:05 +00:00
|
|
|
() => callback.call(timer, arguments[2], arguments[3], arguments[4]);
|
2015-03-20 16:20:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
var args = new Array(length - 2);
|
|
|
|
for (var i = 2; i < length; i += 1)
|
|
|
|
args[i - 2] = arguments[i];
|
2015-11-26 15:55:05 +00:00
|
|
|
ontimeout = () => callback.apply(timer, args);
|
2015-03-20 16:20:43 +00:00
|
|
|
break;
|
2015-01-10 15:49:21 +00:00
|
|
|
}
|
2015-03-20 16:20:43 +00:00
|
|
|
timer._onTimeout = wrapper;
|
|
|
|
timer._repeat = ontimeout;
|
2013-03-21 11:19:03 +00:00
|
|
|
|
|
|
|
if (process.domain) timer.domain = process.domain;
|
|
|
|
exports.active(timer);
|
2010-10-26 19:52:31 +00:00
|
|
|
|
2010-10-26 19:14:17 +00:00
|
|
|
return timer;
|
2013-03-21 11:19:03 +00:00
|
|
|
|
|
|
|
function wrapper() {
|
2015-11-26 15:55:05 +00:00
|
|
|
timer._repeat();
|
2015-04-02 22:14:08 +00:00
|
|
|
|
|
|
|
// Timer might be closed - no point in restarting it
|
|
|
|
if (!timer._repeat)
|
|
|
|
return;
|
|
|
|
|
2013-03-21 11:19:03 +00:00
|
|
|
// If timer is unref'd (or was - it's permanently removed from the list.)
|
|
|
|
if (this._handle) {
|
|
|
|
this._handle.start(repeat, 0);
|
|
|
|
} else {
|
|
|
|
timer._idleTimeout = repeat;
|
|
|
|
exports.active(timer);
|
|
|
|
}
|
|
|
|
}
|
2010-10-26 19:14:17 +00:00
|
|
|
};
|
|
|
|
|
2010-10-26 19:52:31 +00:00
|
|
|
|
2010-12-02 04:59:06 +00:00
|
|
|
exports.clearInterval = function(timer) {
|
2013-03-21 11:19:03 +00:00
|
|
|
if (timer && timer._repeat) {
|
2015-03-26 15:52:36 +00:00
|
|
|
timer._repeat = null;
|
2013-03-21 11:19:03 +00:00
|
|
|
clearTimeout(timer);
|
2010-10-26 19:14:17 +00:00
|
|
|
}
|
|
|
|
};
|
2012-07-13 02:19:01 +00:00
|
|
|
|
2013-03-21 11:19:03 +00:00
|
|
|
|
2015-01-21 16:36:59 +00:00
|
|
|
const Timeout = function(after) {
|
2015-03-21 16:39:35 +00:00
|
|
|
this._called = false;
|
2012-07-13 02:19:01 +00:00
|
|
|
this._idleTimeout = after;
|
|
|
|
this._idlePrev = this;
|
|
|
|
this._idleNext = this;
|
2012-10-25 04:53:35 +00:00
|
|
|
this._idleStart = null;
|
|
|
|
this._onTimeout = null;
|
2015-03-26 15:52:36 +00:00
|
|
|
this._repeat = null;
|
2012-07-13 02:19:01 +00:00
|
|
|
};
|
|
|
|
|
2014-11-26 20:27:57 +00:00
|
|
|
|
|
|
|
function unrefdHandle() {
|
|
|
|
this.owner._onTimeout();
|
2014-12-19 00:51:08 +00:00
|
|
|
if (!this.owner._repeat)
|
2014-11-26 20:27:57 +00:00
|
|
|
this.owner.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-13 02:19:01 +00:00
|
|
|
Timeout.prototype.unref = function() {
|
2014-12-15 16:25:31 +00:00
|
|
|
if (this._handle) {
|
|
|
|
this._handle.unref();
|
2016-02-03 20:27:40 +00:00
|
|
|
} else if (typeof this._onTimeout === 'function') {
|
2016-02-26 19:18:52 +00:00
|
|
|
var now = TimerWrap.now();
|
2012-10-25 04:53:35 +00:00
|
|
|
if (!this._idleStart) this._idleStart = now;
|
|
|
|
var delay = this._idleStart + this._idleTimeout - now;
|
2012-08-17 12:11:33 +00:00
|
|
|
if (delay < 0) delay = 0;
|
2015-03-21 16:39:35 +00:00
|
|
|
|
|
|
|
// Prevent running cb again when unref() is called during the same cb
|
2015-10-16 19:34:15 +00:00
|
|
|
if (this._called && !this._repeat) {
|
|
|
|
exports.unenroll(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var handle = reuse(this);
|
2015-03-21 16:39:35 +00:00
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
this._handle = handle || new TimerWrap();
|
2014-11-26 20:27:57 +00:00
|
|
|
this._handle.owner = this;
|
|
|
|
this._handle[kOnTimeout] = unrefdHandle;
|
2012-08-17 12:11:33 +00:00
|
|
|
this._handle.start(delay, 0);
|
2012-08-11 22:31:44 +00:00
|
|
|
this._handle.domain = this.domain;
|
2012-07-13 02:19:01 +00:00
|
|
|
this._handle.unref();
|
|
|
|
}
|
2015-09-13 15:21:51 +00:00
|
|
|
return this;
|
2012-07-13 02:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Timeout.prototype.ref = function() {
|
|
|
|
if (this._handle)
|
|
|
|
this._handle.ref();
|
2015-09-13 15:21:51 +00:00
|
|
|
return this;
|
2012-07-13 02:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Timeout.prototype.close = function() {
|
|
|
|
this._onTimeout = null;
|
|
|
|
if (this._handle) {
|
2013-08-15 17:23:36 +00:00
|
|
|
this._handle[kOnTimeout] = null;
|
2012-07-13 02:19:01 +00:00
|
|
|
this._handle.close();
|
|
|
|
} else {
|
|
|
|
exports.unenroll(this);
|
|
|
|
}
|
2015-09-13 15:21:51 +00:00
|
|
|
return this;
|
2012-07-13 02:19:01 +00:00
|
|
|
};
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
|
2013-02-06 02:13:02 +00:00
|
|
|
var immediateQueue = {};
|
2012-08-08 02:12:01 +00:00
|
|
|
L.init(immediateQueue);
|
|
|
|
|
|
|
|
|
|
|
|
function processImmediate() {
|
2013-07-11 22:58:11 +00:00
|
|
|
var queue = immediateQueue;
|
2014-12-09 15:01:05 +00:00
|
|
|
var domain, immediate;
|
2013-02-06 02:13:02 +00:00
|
|
|
|
2013-07-11 22:58:11 +00:00
|
|
|
immediateQueue = {};
|
|
|
|
L.init(immediateQueue);
|
2012-08-08 02:12:01 +00:00
|
|
|
|
2013-07-11 22:58:11 +00:00
|
|
|
while (L.isEmpty(queue) === false) {
|
2013-09-24 21:12:11 +00:00
|
|
|
immediate = L.shift(queue);
|
2014-01-09 19:11:40 +00:00
|
|
|
domain = immediate.domain;
|
2013-09-24 21:12:11 +00:00
|
|
|
|
2014-01-09 19:11:40 +00:00
|
|
|
if (domain)
|
|
|
|
domain.enter();
|
2013-09-24 21:12:11 +00:00
|
|
|
|
|
|
|
var threw = true;
|
|
|
|
try {
|
|
|
|
immediate._onImmediate();
|
|
|
|
threw = false;
|
|
|
|
} finally {
|
|
|
|
if (threw) {
|
|
|
|
if (!L.isEmpty(queue)) {
|
|
|
|
// Handle any remaining on next tick, assuming we're still
|
|
|
|
// alive to do so.
|
|
|
|
while (!L.isEmpty(immediateQueue)) {
|
|
|
|
L.append(queue, L.shift(immediateQueue));
|
|
|
|
}
|
|
|
|
immediateQueue = queue;
|
|
|
|
process.nextTick(processImmediate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 19:11:40 +00:00
|
|
|
if (domain)
|
|
|
|
domain.exit();
|
2013-07-11 22:58:11 +00:00
|
|
|
}
|
2012-08-08 02:12:01 +00:00
|
|
|
|
2013-07-11 22:58:11 +00:00
|
|
|
// Only round-trip to C++ land if we have to. Calling clearImmediate() on an
|
|
|
|
// immediate that's in |queue| is okay. Worst case is we make a superfluous
|
|
|
|
// call to NeedImmediateCallbackSetter().
|
|
|
|
if (L.isEmpty(immediateQueue)) {
|
|
|
|
process._needImmediateCallback = false;
|
2012-08-08 02:12:01 +00:00
|
|
|
}
|
2012-08-27 19:51:25 +00:00
|
|
|
}
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
|
2013-10-07 19:39:52 +00:00
|
|
|
function Immediate() { }
|
|
|
|
|
|
|
|
Immediate.prototype.domain = undefined;
|
|
|
|
Immediate.prototype._onImmediate = undefined;
|
|
|
|
Immediate.prototype._idleNext = undefined;
|
|
|
|
Immediate.prototype._idlePrev = undefined;
|
|
|
|
|
|
|
|
|
2015-01-10 15:49:21 +00:00
|
|
|
exports.setImmediate = function(callback, arg1, arg2, arg3) {
|
2015-12-20 11:30:04 +00:00
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new TypeError('"callback" argument must be a function');
|
|
|
|
}
|
|
|
|
|
2015-01-10 15:49:21 +00:00
|
|
|
var i, args;
|
|
|
|
var len = arguments.length;
|
2013-10-07 19:39:52 +00:00
|
|
|
var immediate = new Immediate();
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
L.init(immediate);
|
|
|
|
|
2015-01-10 15:49:21 +00:00
|
|
|
switch (len) {
|
|
|
|
// fast cases
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
immediate._onImmediate = callback;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
immediate._onImmediate = function() {
|
|
|
|
callback.call(immediate, arg1);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
immediate._onImmediate = function() {
|
|
|
|
callback.call(immediate, arg1, arg2);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
immediate._onImmediate = function() {
|
|
|
|
callback.call(immediate, arg1, arg2, arg3);
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
// slow case
|
|
|
|
default:
|
|
|
|
args = new Array(len - 1);
|
|
|
|
for (i = 1; i < len; i++)
|
|
|
|
args[i - 1] = arguments[i];
|
|
|
|
|
|
|
|
immediate._onImmediate = function() {
|
|
|
|
callback.apply(immediate, args);
|
|
|
|
};
|
|
|
|
break;
|
2012-08-08 02:12:01 +00:00
|
|
|
}
|
|
|
|
|
2013-02-06 02:13:02 +00:00
|
|
|
if (!process._needImmediateCallback) {
|
|
|
|
process._needImmediateCallback = true;
|
|
|
|
process._immediateCallback = processImmediate;
|
2012-08-08 02:12:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 21:12:11 +00:00
|
|
|
if (process.domain)
|
|
|
|
immediate.domain = process.domain;
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
L.append(immediateQueue, immediate);
|
|
|
|
|
|
|
|
return immediate;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.clearImmediate = function(immediate) {
|
|
|
|
if (!immediate) return;
|
|
|
|
|
2013-02-06 02:13:02 +00:00
|
|
|
immediate._onImmediate = undefined;
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
L.remove(immediate);
|
|
|
|
|
|
|
|
if (L.isEmpty(immediateQueue)) {
|
2013-02-06 02:13:02 +00:00
|
|
|
process._needImmediateCallback = false;
|
2012-08-08 02:12:01 +00:00
|
|
|
}
|
|
|
|
};
|