2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-03-10 13:17:42 +00:00
|
|
|
const async_wrap = process.binding('async_wrap');
|
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');
|
2017-04-14 19:42:47 +00:00
|
|
|
const internalUtil = require('internal/util');
|
|
|
|
const { createPromise, promiseResolve } = process.binding('util');
|
2017-03-10 13:17:42 +00:00
|
|
|
const async_hooks = require('async_hooks');
|
2016-02-26 19:18:52 +00:00
|
|
|
const assert = require('assert');
|
2014-12-16 22:17:28 +00:00
|
|
|
const util = require('util');
|
2017-08-07 06:42:55 +00:00
|
|
|
const errors = require('internal/errors');
|
2014-12-16 22:17:28 +00:00
|
|
|
const debug = util.debuglog('timer');
|
2016-02-26 19:18:52 +00:00
|
|
|
const kOnTimeout = TimerWrap.kOnTimeout | 0;
|
2017-03-10 13:17:42 +00:00
|
|
|
const initTriggerId = async_hooks.initTriggerId;
|
|
|
|
// Two arrays that share state between C++ and JS.
|
2017-09-26 13:50:10 +00:00
|
|
|
const { async_hook_fields, async_id_fields } = async_wrap;
|
2017-03-10 13:17:42 +00:00
|
|
|
// The needed emit*() functions.
|
|
|
|
const { emitInit, emitBefore, emitAfter, emitDestroy } = async_hooks;
|
|
|
|
// Grab the constants necessary for working with internal arrays.
|
2017-09-26 13:50:10 +00:00
|
|
|
const { kInit, kDestroy, kAsyncIdCounter } = async_wrap.constants;
|
2017-03-10 13:17:42 +00:00
|
|
|
// Symbols for storing async id state.
|
|
|
|
const async_id_symbol = Symbol('asyncId');
|
2017-09-26 13:50:10 +00:00
|
|
|
const trigger_async_id_symbol = Symbol('triggerAsyncId');
|
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
|
|
|
|
2016-02-26 19:19:51 +00:00
|
|
|
|
|
|
|
// HOW and WHY the timers implementation works the way it does.
|
|
|
|
//
|
|
|
|
// Timers are crucial to Node.js. Internally, any TCP I/O connection creates a
|
|
|
|
// timer so that we can time out of connections. Additionally, many user
|
2017-02-12 13:16:52 +00:00
|
|
|
// libraries and applications also use timers. As such there may be a
|
2016-02-26 19:19:51 +00:00
|
|
|
// significantly large amount of timeouts scheduled at any given time.
|
|
|
|
// Therefore, it is very important that the timers implementation is performant
|
|
|
|
// and efficient.
|
|
|
|
//
|
|
|
|
// Note: It is suggested you first read though the lib/internal/linkedlist.js
|
|
|
|
// linked list implementation, since timers depend on it extensively. It can be
|
|
|
|
// somewhat counter-intuitive at first, as it is not actually a class. Instead,
|
|
|
|
// it is a set of helpers that operate on an existing object.
|
|
|
|
//
|
|
|
|
// In order to be as performant as possible, the architecture and data
|
|
|
|
// structures are designed so that they are optimized to handle the following
|
|
|
|
// use cases as efficiently as possible:
|
|
|
|
|
|
|
|
// - Adding a new timer. (insert)
|
|
|
|
// - Removing an existing timer. (remove)
|
|
|
|
// - Handling a timer timing out. (timeout)
|
|
|
|
//
|
|
|
|
// Whenever possible, the implementation tries to make the complexity of these
|
|
|
|
// operations as close to constant-time as possible.
|
|
|
|
// (So that performance is not impacted by the number of scheduled timers.)
|
|
|
|
//
|
|
|
|
// Object maps are kept which contain linked lists keyed by their duration in
|
|
|
|
// milliseconds.
|
|
|
|
// The linked lists within also have some meta-properties, one of which is a
|
|
|
|
// TimerWrap C++ handle, which makes the call after the duration to process the
|
|
|
|
// list it is attached to.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// ╔════ > Object Map
|
|
|
|
// ║
|
|
|
|
// ╠══
|
|
|
|
// ║ refedLists: { '40': { }, '320': { etc } } (keys of millisecond duration)
|
|
|
|
// ╚══ ┌─────────┘
|
|
|
|
// │
|
|
|
|
// ╔══ │
|
|
|
|
// ║ TimersList { _idleNext: { }, _idlePrev: (self), _timer: (TimerWrap) }
|
|
|
|
// ║ ┌────────────────┘
|
|
|
|
// ║ ╔══ │ ^
|
|
|
|
// ║ ║ { _idleNext: { }, _idlePrev: { }, _onTimeout: (callback) }
|
|
|
|
// ║ ║ ┌───────────┘
|
|
|
|
// ║ ║ │ ^
|
|
|
|
// ║ ║ { _idleNext: { etc }, _idlePrev: { }, _onTimeout: (callback) }
|
|
|
|
// ╠══ ╠══
|
|
|
|
// ║ ║
|
|
|
|
// ║ ╚════ > Actual JavaScript timeouts
|
|
|
|
// ║
|
|
|
|
// ╚════ > Linked List
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// With this, virtually constant-time insertion (append), removal, and timeout
|
|
|
|
// is possible in the JavaScript layer. Any one list of timers is able to be
|
|
|
|
// sorted by just appending to it because all timers within share the same
|
|
|
|
// duration. Therefore, any timer added later will always have been scheduled to
|
|
|
|
// timeout later, thus only needing to be appended.
|
|
|
|
// Removal from an object-property linked list is also virtually constant-time
|
|
|
|
// as can be seen in the lib/internal/linkedlist.js implementation.
|
2017-01-26 19:29:39 +00:00
|
|
|
// Timeouts only need to process any timers currently due to expire, which will
|
2016-02-26 19:19:51 +00:00
|
|
|
// always be at the beginning of the list for reasons stated above. Any timers
|
|
|
|
// after the first one encountered that does not yet need to timeout will also
|
|
|
|
// always be due to timeout at a later time.
|
|
|
|
//
|
|
|
|
// Less-than constant time operations are thus contained in two places:
|
|
|
|
// TimerWrap's backing libuv timers implementation (a performant heap-based
|
|
|
|
// queue), and the object map lookup of a specific list by the duration of
|
|
|
|
// timers within (or creation of a new list).
|
|
|
|
// However, these operations combined have shown to be trivial in comparison to
|
|
|
|
// other alternative timers architectures.
|
|
|
|
|
|
|
|
|
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
|
|
|
//
|
2016-02-26 19:19:51 +00:00
|
|
|
// The difference between these two objects is that the former contains timers
|
|
|
|
// that will keep the process open if they are the only thing left, while the
|
|
|
|
// latter will not.
|
|
|
|
//
|
|
|
|
// - key = time in milliseconds
|
|
|
|
// - value = linked list
|
2016-09-20 06:10:50 +00:00
|
|
|
const refedLists = Object.create(null);
|
|
|
|
const unrefedLists = Object.create(null);
|
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-08-22 18:46:36 +00:00
|
|
|
const active = 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.
|
2016-02-26 19:19:51 +00:00
|
|
|
//
|
|
|
|
// Appends a timer onto the end of an existing timers list, or creates a new
|
|
|
|
// TimerWrap backed list if one does not already exist for the specified timeout
|
|
|
|
// duration.
|
2016-02-26 19:18:52 +00:00
|
|
|
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:19:51 +00:00
|
|
|
// Use an existing list if there is one, otherwise we need to make a new one.
|
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);
|
2016-09-20 06:10:50 +00:00
|
|
|
lists[msecs] = list = createTimersList(msecs, unrefed);
|
2012-12-27 19:40:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 13:17:42 +00:00
|
|
|
if (!item[async_id_symbol] || item._destroyed) {
|
|
|
|
item._destroyed = false;
|
2017-09-26 13:50:10 +00:00
|
|
|
item[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
|
|
|
|
item[trigger_async_id_symbol] = initTriggerId();
|
2017-03-10 13:17:42 +00:00
|
|
|
if (async_hook_fields[kInit] > 0)
|
2017-09-26 13:50:10 +00:00
|
|
|
emitInit(
|
|
|
|
item[async_id_symbol], 'Timeout', item[trigger_async_id_symbol], item
|
|
|
|
);
|
2017-03-10 13:17:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-20 06:10:50 +00:00
|
|
|
function createTimersList(msecs, unrefed) {
|
|
|
|
// Make a new linked list of timers, and create a TimerWrap to schedule
|
|
|
|
// processing for the list.
|
|
|
|
const list = new TimersList(msecs, unrefed);
|
|
|
|
L.init(list);
|
|
|
|
list._timer._list = list;
|
|
|
|
|
|
|
|
if (unrefed === true) list._timer.unref();
|
|
|
|
list._timer.start(msecs);
|
|
|
|
|
|
|
|
list._timer[kOnTimeout] = listOnTimeout;
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
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;
|
2016-12-29 07:22:35 +00:00
|
|
|
this.nextTick = false;
|
2016-02-26 19:18:52 +00:00
|
|
|
}
|
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
|
|
|
|
2016-12-29 07:22:35 +00:00
|
|
|
if (list.nextTick) {
|
|
|
|
list.nextTick = false;
|
|
|
|
process.nextTick(listOnTimeoutNT, list);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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();
|
2015-07-24 00:09:21 +00:00
|
|
|
debug('now: %d', 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;
|
|
|
|
|
2016-02-26 19:19:51 +00:00
|
|
|
// Check if this loop iteration is too early for the next timer.
|
|
|
|
// This happens if there are more timers scheduled for later in the list.
|
2012-10-26 05:49:22 +00:00
|
|
|
if (diff < msecs) {
|
2015-07-24 00:09:21 +00:00
|
|
|
var timeRemaining = msecs - (TimerWrap.now() - timer._idleStart);
|
|
|
|
if (timeRemaining < 0) {
|
|
|
|
timeRemaining = 0;
|
|
|
|
}
|
2016-08-06 12:29:50 +00:00
|
|
|
this.start(timeRemaining);
|
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
|
|
|
}
|
|
|
|
|
2016-02-26 19:19:51 +00:00
|
|
|
// The actual logic for when a timeout happens.
|
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
|
|
|
|
2017-03-10 13:17:42 +00:00
|
|
|
if (!timer._onTimeout) {
|
|
|
|
if (async_hook_fields[kDestroy] > 0 && !timer._destroyed &&
|
|
|
|
typeof timer[async_id_symbol] === 'number') {
|
|
|
|
emitDestroy(timer[async_id_symbol]);
|
|
|
|
timer._destroyed = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-26 19:18:52 +00:00
|
|
|
|
|
|
|
var domain = timer.domain;
|
|
|
|
if (domain) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-26 19:19:51 +00:00
|
|
|
// If `L.peek(list)` returned nothing, the list was either empty or we have
|
|
|
|
// called all of the timer timeouts.
|
|
|
|
// As such, we can remove the list and clean up the TimerWrap C++ handle.
|
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-07-21 18:50:25 +00:00
|
|
|
|
|
|
|
// Either refedLists[msecs] or unrefedLists[msecs] may have been removed and
|
|
|
|
// recreated since the reference to `list` was created. Make sure they're
|
|
|
|
// the same instance of the list before destroying.
|
|
|
|
if (list._unrefed === true && list === unrefedLists[msecs]) {
|
2016-02-26 19:18:52 +00:00
|
|
|
delete unrefedLists[msecs];
|
2016-07-21 18:50:25 +00:00
|
|
|
} else if (list === refedLists[msecs]) {
|
2016-02-26 19:18:52 +00:00
|
|
|
delete refedLists[msecs];
|
|
|
|
}
|
2017-02-20 05:31:15 +00:00
|
|
|
|
|
|
|
// Do not close the underlying handle if its ownership has changed
|
|
|
|
// (e.g it was unrefed in its callback).
|
|
|
|
if (this.owner)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.close();
|
2016-02-26 19:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-06 06:35:52 +00:00
|
|
|
// An optimization so that the try/finally only de-optimizes (since at least v8
|
|
|
|
// 4.7) what is in this smaller function.
|
2016-02-26 19:18:52 +00:00
|
|
|
function tryOnTimeout(timer, list) {
|
|
|
|
timer._called = true;
|
2017-03-10 13:17:42 +00:00
|
|
|
const timerAsyncId = (typeof timer[async_id_symbol] === 'number') ?
|
2017-07-05 16:21:40 +00:00
|
|
|
timer[async_id_symbol] : null;
|
2016-02-26 19:18:52 +00:00
|
|
|
var threw = true;
|
2017-03-10 13:17:42 +00:00
|
|
|
if (timerAsyncId !== null)
|
2017-09-26 13:50:10 +00:00
|
|
|
emitBefore(timerAsyncId, timer[trigger_async_id_symbol]);
|
2016-02-26 19:18:52 +00:00
|
|
|
try {
|
2016-09-20 06:10:50 +00:00
|
|
|
ontimeout(timer);
|
2016-02-26 19:18:52 +00:00
|
|
|
threw = false;
|
|
|
|
} finally {
|
2017-03-10 13:17:42 +00:00
|
|
|
if (timerAsyncId !== null) {
|
|
|
|
if (!threw)
|
2017-07-02 13:59:23 +00:00
|
|
|
emitAfter(timerAsyncId);
|
2017-03-10 13:17:42 +00:00
|
|
|
if (!timer._repeat && async_hook_fields[kDestroy] > 0 &&
|
|
|
|
!timer._destroyed) {
|
|
|
|
emitDestroy(timerAsyncId);
|
|
|
|
timer._destroyed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 19:18:52 +00:00
|
|
|
if (!threw) return;
|
|
|
|
|
2016-12-29 07:22:35 +00:00
|
|
|
// Postpone all later list events to next tick. We need to do this
|
|
|
|
// so that the events are called in the order they were created.
|
|
|
|
const lists = list._unrefed === true ? unrefedLists : refedLists;
|
|
|
|
for (var key in lists) {
|
|
|
|
if (key > list.msecs) {
|
|
|
|
lists[key].nextTick = true;
|
|
|
|
}
|
|
|
|
}
|
2016-02-26 19:18:52 +00:00
|
|
|
// 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;
|
2016-02-26 19:19:51 +00:00
|
|
|
// If we threw, we need to process the rest of the list in nextTick.
|
2016-02-26 19:18:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-26 19:19:51 +00:00
|
|
|
// A convenience function for re-using TimerWrap handles more easily.
|
|
|
|
//
|
|
|
|
// This mostly exists to fix https://github.com/nodejs/node/issues/1264.
|
|
|
|
// Handles in libuv take at least one `uv_run` to be registered as unreferenced.
|
|
|
|
// Re-using an existing handle allows us to skip that, so that a second `uv_run`
|
|
|
|
// will return no active handles, even when running `setTimeout(fn).unref()`.
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-26 19:19:51 +00:00
|
|
|
// Remove a timer. Cancels the timeout and resets the relevant timer properties.
|
2015-10-16 19:34:15 +00:00
|
|
|
const unenroll = exports.unenroll = function(item) {
|
2017-03-10 13:17:42 +00:00
|
|
|
// Fewer checks may be possible, but these cover everything.
|
|
|
|
if (async_hook_fields[kDestroy] > 0 &&
|
|
|
|
item &&
|
|
|
|
typeof item[async_id_symbol] === 'number' &&
|
|
|
|
!item._destroyed) {
|
|
|
|
emitDestroy(item[async_id_symbol]);
|
|
|
|
item._destroyed = true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-02-26 19:19:51 +00:00
|
|
|
// Make a regular object able to act as a timer by setting some properties.
|
|
|
|
// This function does not start the timer, see `active()`.
|
|
|
|
// Using existing objects as timers slightly reduces object overhead.
|
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') {
|
2017-08-07 06:42:55 +00:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'msecs',
|
|
|
|
'number', msecs);
|
2014-12-16 22:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (msecs < 0 || !isFinite(msecs)) {
|
2017-08-07 06:42:55 +00:00
|
|
|
throw new errors.RangeError('ERR_VALUE_OUT_OF_RANGE', 'msecs',
|
|
|
|
'a non-negative finite number', msecs);
|
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) {
|
2016-05-24 20:39:54 +00:00
|
|
|
process.emitWarning(`${msecs} does not fit into a 32-bit signed integer.` +
|
|
|
|
`\nTimer duration was truncated to ${TIMEOUT_MAX}.`,
|
|
|
|
'TimeoutOverflowWarning');
|
2014-12-16 22:17:28 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2017-04-14 19:42:47 +00:00
|
|
|
function setTimeout(callback, after, arg1, arg2, arg3) {
|
2015-12-20 11:30:04 +00:00
|
|
|
if (typeof callback !== 'function') {
|
2017-08-07 06:42:55 +00:00
|
|
|
throw new errors.TypeError('ERR_INVALID_CALLBACK');
|
2015-12-20 11:30:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 06:10:50 +00:00
|
|
|
var len = arguments.length;
|
|
|
|
var args;
|
|
|
|
if (len === 3) {
|
|
|
|
args = [arg1];
|
|
|
|
} else if (len === 4) {
|
|
|
|
args = [arg1, arg2];
|
|
|
|
} else if (len > 4) {
|
|
|
|
args = [arg1, arg2, arg3];
|
|
|
|
for (var i = 5; i < len; i++)
|
|
|
|
// extend array dynamically, makes .apply run much faster in v6.0.0
|
|
|
|
args[i - 2] = arguments[i];
|
2011-07-11 22:30:24 +00:00
|
|
|
}
|
2011-06-08 02:35:50 +00:00
|
|
|
|
2016-09-20 06:10:50 +00:00
|
|
|
return createSingleTimeout(callback, after, args);
|
2017-04-14 19:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout[internalUtil.promisify.custom] = function(after, value) {
|
|
|
|
const promise = createPromise();
|
|
|
|
createSingleTimeout(promise, after, [value]);
|
|
|
|
return promise;
|
2016-09-20 06:10:50 +00:00
|
|
|
};
|
|
|
|
|
2017-04-14 19:42:47 +00:00
|
|
|
exports.setTimeout = setTimeout;
|
|
|
|
|
2016-09-20 06:10:50 +00:00
|
|
|
function createSingleTimeout(callback, after, args) {
|
|
|
|
after *= 1; // coalesce to number or NaN
|
2016-05-24 20:39:54 +00:00
|
|
|
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
|
|
|
|
if (after > TIMEOUT_MAX) {
|
|
|
|
process.emitWarning(`${after} does not fit into` +
|
|
|
|
' a 32-bit signed integer.' +
|
|
|
|
'\nTimeout duration was set to 1.',
|
|
|
|
'TimeoutOverflowWarning');
|
|
|
|
}
|
2017-06-17 13:11:45 +00:00
|
|
|
after = 1; // schedule on next tick, follows browser behavior
|
2016-05-24 20:39:54 +00:00
|
|
|
}
|
2010-10-26 19:14:17 +00:00
|
|
|
|
2016-09-20 06:10:50 +00:00
|
|
|
var timer = new Timeout(after, callback, args);
|
|
|
|
if (process.domain)
|
|
|
|
timer.domain = process.domain;
|
2012-04-06 23:26:18 +00:00
|
|
|
|
2015-08-22 18:46:36 +00:00
|
|
|
active(timer);
|
2011-07-11 22:30:24 +00:00
|
|
|
|
2010-10-26 19:14:17 +00:00
|
|
|
return timer;
|
2016-09-20 06:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function ontimeout(timer) {
|
|
|
|
var args = timer._timerArgs;
|
|
|
|
var callback = timer._onTimeout;
|
2017-04-14 19:42:47 +00:00
|
|
|
if (typeof callback !== 'function')
|
|
|
|
return promiseResolve(callback, args[0]);
|
2016-09-20 06:10:50 +00:00
|
|
|
if (!args)
|
2017-05-11 03:59:04 +00:00
|
|
|
timer._onTimeout();
|
2016-09-20 06:10:50 +00:00
|
|
|
else {
|
|
|
|
switch (args.length) {
|
|
|
|
case 1:
|
2017-05-11 03:59:04 +00:00
|
|
|
timer._onTimeout(args[0]);
|
2016-09-20 06:10:50 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2017-05-11 03:59:04 +00:00
|
|
|
timer._onTimeout(args[0], args[1]);
|
2016-09-20 06:10:50 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2017-05-11 03:59:04 +00:00
|
|
|
timer._onTimeout(args[0], args[1], args[2]);
|
2016-09-20 06:10:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-05-11 03:59:04 +00:00
|
|
|
Function.prototype.apply.call(callback, timer, args);
|
2016-09-20 06:10:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (timer._repeat)
|
|
|
|
rearm(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function rearm(timer) {
|
2016-11-18 20:47:15 +00:00
|
|
|
// // Do not re-arm unenroll'd or closed timers.
|
|
|
|
if (timer._idleTimeout === -1) return;
|
|
|
|
|
2016-09-20 06:10:50 +00:00
|
|
|
// If timer is unref'd (or was - it's permanently removed from the list.)
|
|
|
|
if (timer._handle && timer instanceof Timeout) {
|
|
|
|
timer._handle.start(timer._repeat);
|
|
|
|
} else {
|
|
|
|
timer._idleTimeout = timer._repeat;
|
|
|
|
active(timer);
|
|
|
|
}
|
|
|
|
}
|
2010-10-26 19:14:17 +00:00
|
|
|
|
2010-10-26 19:52:31 +00:00
|
|
|
|
2015-08-22 18:46:36 +00:00
|
|
|
const clearTimeout = 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 {
|
2015-08-22 18:46:36 +00:00
|
|
|
unenroll(timer);
|
2011-06-08 02:35:50 +00:00
|
|
|
}
|
2010-10-29 07:00:43 +00:00
|
|
|
}
|
2010-10-26 19:52:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-09-20 06:10:50 +00:00
|
|
|
exports.setInterval = function(callback, repeat, arg1, arg2, arg3) {
|
2015-12-20 11:30:04 +00:00
|
|
|
if (typeof callback !== 'function') {
|
2017-08-07 06:42:55 +00:00
|
|
|
throw new errors.TypeError('ERR_INVALID_CALLBACK');
|
2015-12-20 11:30:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 06:10:50 +00:00
|
|
|
var len = arguments.length;
|
|
|
|
var args;
|
|
|
|
if (len === 3) {
|
|
|
|
args = [arg1];
|
|
|
|
} else if (len === 4) {
|
|
|
|
args = [arg1, arg2];
|
|
|
|
} else if (len > 4) {
|
|
|
|
args = [arg1, arg2, arg3];
|
|
|
|
for (var i = 5; i < len; i++)
|
|
|
|
// extend array dynamically, makes .apply run much faster in v6.0.0
|
|
|
|
args[i - 2] = arguments[i];
|
|
|
|
}
|
2012-07-01 20:58:22 +00:00
|
|
|
|
2016-09-20 06:10:50 +00:00
|
|
|
return createRepeatTimeout(callback, repeat, args);
|
|
|
|
};
|
|
|
|
|
|
|
|
function createRepeatTimeout(callback, repeat, args) {
|
|
|
|
repeat *= 1; // coalesce to number or NaN
|
2016-05-24 20:39:54 +00:00
|
|
|
if (!(repeat >= 1 && repeat <= TIMEOUT_MAX)) {
|
|
|
|
if (repeat > TIMEOUT_MAX) {
|
|
|
|
process.emitWarning(`${repeat} does not fit into` +
|
|
|
|
' a 32-bit signed integer.' +
|
|
|
|
'\nInterval duration was set to 1.',
|
|
|
|
'TimeoutOverflowWarning');
|
|
|
|
}
|
2017-06-17 13:11:45 +00:00
|
|
|
repeat = 1; // schedule on next tick, follows browser behavior
|
2016-05-24 20:39:54 +00:00
|
|
|
}
|
2011-07-11 22:30:24 +00:00
|
|
|
|
2016-09-20 06:10:50 +00:00
|
|
|
var timer = new Timeout(repeat, callback, args);
|
|
|
|
timer._repeat = repeat;
|
|
|
|
if (process.domain)
|
|
|
|
timer.domain = process.domain;
|
2013-03-21 11:19:03 +00:00
|
|
|
|
2015-08-22 18:46:36 +00:00
|
|
|
active(timer);
|
2010-10-26 19:52:31 +00:00
|
|
|
|
2010-10-26 19:14:17 +00:00
|
|
|
return timer;
|
2016-09-20 06:10:50 +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
|
|
|
|
2016-09-20 06:10:50 +00:00
|
|
|
function Timeout(after, callback, args) {
|
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;
|
2016-09-20 06:10:50 +00:00
|
|
|
this._onTimeout = callback;
|
|
|
|
this._timerArgs = args;
|
2015-03-26 15:52:36 +00:00
|
|
|
this._repeat = null;
|
2017-03-10 13:17:42 +00:00
|
|
|
this._destroyed = false;
|
2017-09-26 13:50:10 +00:00
|
|
|
this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
|
|
|
|
this[trigger_async_id_symbol] = initTriggerId();
|
2017-03-10 13:17:42 +00:00
|
|
|
if (async_hook_fields[kInit] > 0)
|
2017-09-26 13:50:10 +00:00
|
|
|
emitInit(
|
|
|
|
this[async_id_symbol], 'Timeout', this[trigger_async_id_symbol], this
|
|
|
|
);
|
2016-03-21 16:28:30 +00:00
|
|
|
}
|
2012-07-13 02:19:01 +00:00
|
|
|
|
2014-11-26 20:27:57 +00:00
|
|
|
|
|
|
|
function unrefdHandle() {
|
2016-11-18 20:47:15 +00:00
|
|
|
// Don't attempt to call the callback if it is not a function.
|
|
|
|
if (typeof this.owner._onTimeout === 'function') {
|
|
|
|
ontimeout(this.owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we clean up if the callback is no longer a function
|
|
|
|
// even if the timer is an interval.
|
2016-12-08 09:27:05 +00:00
|
|
|
if (!this.owner._repeat ||
|
|
|
|
typeof this.owner._onTimeout !== 'function') {
|
2014-11-26 20:27:57 +00:00
|
|
|
this.owner.close();
|
2016-11-18 20:47:15 +00:00
|
|
|
}
|
2014-11-26 20:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2015-08-22 18:46:36 +00:00
|
|
|
unenroll(this);
|
2015-10-16 19:34:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var handle = reuse(this);
|
2016-09-06 15:49:19 +00:00
|
|
|
if (handle) {
|
|
|
|
handle._list = undefined;
|
|
|
|
}
|
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;
|
2016-08-06 12:29:50 +00:00
|
|
|
this._handle.start(delay);
|
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) {
|
2017-03-10 13:17:42 +00:00
|
|
|
// Fewer checks may be possible, but these cover everything.
|
|
|
|
if (async_hook_fields[kDestroy] > 0 &&
|
|
|
|
this &&
|
|
|
|
typeof this[async_id_symbol] === 'number' &&
|
|
|
|
!this._destroyed) {
|
|
|
|
emitDestroy(this[async_id_symbol]);
|
|
|
|
this._destroyed = true;
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:47:15 +00:00
|
|
|
this._idleTimeout = -1;
|
2013-08-15 17:23:36 +00:00
|
|
|
this._handle[kOnTimeout] = null;
|
2012-07-13 02:19:01 +00:00
|
|
|
this._handle.close();
|
|
|
|
} else {
|
2015-08-22 18:46:36 +00:00
|
|
|
unenroll(this);
|
2012-07-13 02:19:01 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
|
2016-09-18 01:28:20 +00:00
|
|
|
// A linked list for storing `setImmediate()` requests
|
|
|
|
function ImmediateList() {
|
|
|
|
this.head = null;
|
|
|
|
this.tail = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Appends an item to the end of the linked list, adjusting the current tail's
|
|
|
|
// previous and next pointers where applicable
|
|
|
|
ImmediateList.prototype.append = function(item) {
|
|
|
|
if (this.tail) {
|
|
|
|
this.tail._idleNext = item;
|
|
|
|
item._idlePrev = this.tail;
|
|
|
|
} else {
|
|
|
|
this.head = item;
|
|
|
|
}
|
|
|
|
this.tail = item;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Removes an item from the linked list, adjusting the pointers of adjacent
|
|
|
|
// items and the linked list's head or tail pointers as necessary
|
|
|
|
ImmediateList.prototype.remove = function(item) {
|
|
|
|
if (item._idleNext) {
|
|
|
|
item._idleNext._idlePrev = item._idlePrev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item._idlePrev) {
|
|
|
|
item._idlePrev._idleNext = item._idleNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item === this.head)
|
|
|
|
this.head = item._idleNext;
|
|
|
|
if (item === this.tail)
|
|
|
|
this.tail = item._idlePrev;
|
|
|
|
|
|
|
|
item._idleNext = null;
|
|
|
|
item._idlePrev = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a single linked list instance only once at startup
|
|
|
|
var immediateQueue = new ImmediateList();
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
function processImmediate() {
|
2016-09-18 01:28:20 +00:00
|
|
|
var immediate = immediateQueue.head;
|
|
|
|
var tail = immediateQueue.tail;
|
|
|
|
var domain;
|
2013-02-06 02:13:02 +00:00
|
|
|
|
2016-09-18 01:28:20 +00:00
|
|
|
// Clear the linked list early in case new `setImmediate()` calls occur while
|
|
|
|
// immediate callbacks are executed
|
|
|
|
immediateQueue.head = immediateQueue.tail = null;
|
2012-08-08 02:12:01 +00:00
|
|
|
|
2016-09-18 01:28:20 +00:00
|
|
|
while (immediate) {
|
2014-01-09 19:11:40 +00:00
|
|
|
domain = immediate.domain;
|
2013-09-24 21:12:11 +00:00
|
|
|
|
2016-11-23 10:57:49 +00:00
|
|
|
if (!immediate._onImmediate) {
|
|
|
|
immediate = immediate._idleNext;
|
2016-04-28 00:31:59 +00:00
|
|
|
continue;
|
2016-11-23 10:57:49 +00:00
|
|
|
}
|
2016-04-28 00:31:59 +00:00
|
|
|
|
2014-01-09 19:11:40 +00:00
|
|
|
if (domain)
|
|
|
|
domain.enter();
|
2013-09-24 21:12:11 +00:00
|
|
|
|
2016-04-28 00:31:59 +00:00
|
|
|
immediate._callback = immediate._onImmediate;
|
2016-10-13 22:02:52 +00:00
|
|
|
|
|
|
|
// Save next in case `clearImmediate(immediate)` is called from callback
|
|
|
|
var next = immediate._idleNext;
|
|
|
|
|
2016-09-18 01:28:20 +00:00
|
|
|
tryOnImmediate(immediate, tail);
|
2013-09-24 21:12:11 +00:00
|
|
|
|
2014-01-09 19:11:40 +00:00
|
|
|
if (domain)
|
|
|
|
domain.exit();
|
2016-09-18 01:28:20 +00:00
|
|
|
|
2016-10-13 22:02:52 +00:00
|
|
|
// If `clearImmediate(immediate)` wasn't called from the callback, use the
|
|
|
|
// `immediate`'s next item
|
|
|
|
if (immediate._idleNext)
|
|
|
|
immediate = immediate._idleNext;
|
|
|
|
else
|
|
|
|
immediate = next;
|
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().
|
2016-09-18 01:28:20 +00:00
|
|
|
if (!immediateQueue.head) {
|
2013-07-11 22:58:11 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2015-12-06 06:35:52 +00:00
|
|
|
// An optimization so that the try/finally only de-optimizes (since at least v8
|
|
|
|
// 4.7) what is in this smaller function.
|
2016-09-18 01:28:20 +00:00
|
|
|
function tryOnImmediate(immediate, oldTail) {
|
2015-12-06 06:35:52 +00:00
|
|
|
var threw = true;
|
2017-09-26 13:50:10 +00:00
|
|
|
emitBefore(immediate[async_id_symbol], immediate[trigger_async_id_symbol]);
|
2015-12-06 06:35:52 +00:00
|
|
|
try {
|
2017-07-16 21:32:39 +00:00
|
|
|
// make the actual call outside the try/finally to allow it to be optimized
|
2016-04-28 00:31:59 +00:00
|
|
|
runCallback(immediate);
|
2015-12-06 06:35:52 +00:00
|
|
|
threw = false;
|
|
|
|
} finally {
|
2017-03-10 13:17:42 +00:00
|
|
|
// clearImmediate checks _callback === null for kDestroy hooks.
|
|
|
|
immediate._callback = null;
|
|
|
|
if (!threw)
|
2017-07-02 13:59:23 +00:00
|
|
|
emitAfter(immediate[async_id_symbol]);
|
2017-03-10 13:17:42 +00:00
|
|
|
if (async_hook_fields[kDestroy] > 0 && !immediate._destroyed) {
|
|
|
|
emitDestroy(immediate[async_id_symbol]);
|
|
|
|
immediate._destroyed = true;
|
|
|
|
}
|
|
|
|
|
2016-09-18 01:28:20 +00:00
|
|
|
if (threw && immediate._idleNext) {
|
2015-12-06 06:35:52 +00:00
|
|
|
// Handle any remaining on next tick, assuming we're still alive to do so.
|
2016-09-18 01:28:20 +00:00
|
|
|
const curHead = immediateQueue.head;
|
|
|
|
const next = immediate._idleNext;
|
|
|
|
if (curHead) {
|
|
|
|
curHead._idlePrev = oldTail;
|
|
|
|
oldTail._idleNext = curHead;
|
|
|
|
next._idlePrev = null;
|
|
|
|
immediateQueue.head = next;
|
|
|
|
} else {
|
|
|
|
immediateQueue.head = next;
|
|
|
|
immediateQueue.tail = oldTail;
|
2015-12-06 06:35:52 +00:00
|
|
|
}
|
|
|
|
process.nextTick(processImmediate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 00:31:59 +00:00
|
|
|
function runCallback(timer) {
|
|
|
|
const argv = timer._argv;
|
|
|
|
const argc = argv ? argv.length : 0;
|
2017-04-14 19:42:47 +00:00
|
|
|
if (typeof timer._callback !== 'function')
|
|
|
|
return promiseResolve(timer._callback, argv[0]);
|
2016-04-28 00:31:59 +00:00
|
|
|
switch (argc) {
|
|
|
|
// fast-path callbacks with 0-3 arguments
|
|
|
|
case 0:
|
|
|
|
return timer._callback();
|
|
|
|
case 1:
|
|
|
|
return timer._callback(argv[0]);
|
|
|
|
case 2:
|
|
|
|
return timer._callback(argv[0], argv[1]);
|
|
|
|
case 3:
|
|
|
|
return timer._callback(argv[0], argv[1], argv[2]);
|
|
|
|
// more than 3 arguments run slower with .apply
|
|
|
|
default:
|
2017-05-11 03:59:04 +00:00
|
|
|
return Function.prototype.apply.call(timer._callback, timer, argv);
|
2016-04-28 00:31:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-06 06:35:52 +00:00
|
|
|
|
2013-10-07 19:39:52 +00:00
|
|
|
|
2016-04-28 00:31:59 +00:00
|
|
|
function Immediate() {
|
|
|
|
// assigning the callback here can cause optimize/deoptimize thrashing
|
|
|
|
// so have caller annotate the object (node v6.0.0, v8 5.0.71.35)
|
|
|
|
this._idleNext = null;
|
|
|
|
this._idlePrev = null;
|
|
|
|
this._callback = null;
|
|
|
|
this._argv = null;
|
|
|
|
this._onImmediate = null;
|
2017-03-10 13:17:42 +00:00
|
|
|
this._destroyed = false;
|
2016-04-28 00:31:59 +00:00
|
|
|
this.domain = process.domain;
|
2017-09-26 13:50:10 +00:00
|
|
|
this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
|
|
|
|
this[trigger_async_id_symbol] = initTriggerId();
|
2017-03-10 13:17:42 +00:00
|
|
|
if (async_hook_fields[kInit] > 0)
|
2017-09-26 13:50:10 +00:00
|
|
|
emitInit(
|
|
|
|
this[async_id_symbol], 'Immediate', this[trigger_async_id_symbol], this
|
|
|
|
);
|
2016-04-28 00:31:59 +00:00
|
|
|
}
|
2013-10-07 19:39:52 +00:00
|
|
|
|
2017-04-14 19:42:47 +00:00
|
|
|
function setImmediate(callback, arg1, arg2, arg3) {
|
2015-12-20 11:30:04 +00:00
|
|
|
if (typeof callback !== 'function') {
|
2017-08-07 06:42:55 +00:00
|
|
|
throw new errors.TypeError('ERR_INVALID_CALLBACK');
|
2015-12-20 11:30:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-10 15:49:21 +00:00
|
|
|
var i, args;
|
2012-08-08 02:12:01 +00:00
|
|
|
|
2016-04-28 00:31:59 +00:00
|
|
|
switch (arguments.length) {
|
2015-01-10 15:49:21 +00:00
|
|
|
// fast cases
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
case 2:
|
2016-04-28 00:31:59 +00:00
|
|
|
args = [arg1];
|
2015-01-10 15:49:21 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2016-04-28 00:31:59 +00:00
|
|
|
args = [arg1, arg2];
|
2015-01-10 15:49:21 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-04-28 00:31:59 +00:00
|
|
|
args = [arg1, arg2, arg3];
|
|
|
|
for (i = 4; i < arguments.length; i++)
|
|
|
|
// extend array dynamically, makes .apply run much faster in v6.0.0
|
2015-01-10 15:49:21 +00:00
|
|
|
args[i - 1] = arguments[i];
|
|
|
|
break;
|
2012-08-08 02:12:01 +00:00
|
|
|
}
|
2016-09-18 01:28:20 +00:00
|
|
|
return createImmediate(args, callback);
|
2017-04-14 19:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setImmediate[internalUtil.promisify.custom] = function(value) {
|
|
|
|
const promise = createPromise();
|
|
|
|
createImmediate([value], promise);
|
|
|
|
return promise;
|
2016-09-18 01:28:20 +00:00
|
|
|
};
|
|
|
|
|
2017-04-14 19:42:47 +00:00
|
|
|
exports.setImmediate = setImmediate;
|
|
|
|
|
2016-09-18 01:28:20 +00:00
|
|
|
function createImmediate(args, callback) {
|
2016-04-28 00:31:59 +00:00
|
|
|
// declaring it `const immediate` causes v6.0.0 to deoptimize this function
|
|
|
|
var immediate = new Immediate();
|
|
|
|
immediate._callback = callback;
|
|
|
|
immediate._argv = args;
|
|
|
|
immediate._onImmediate = callback;
|
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
|
|
|
}
|
|
|
|
|
2016-09-18 01:28:20 +00:00
|
|
|
immediateQueue.append(immediate);
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
return immediate;
|
2016-09-18 01:28:20 +00:00
|
|
|
}
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
exports.clearImmediate = function(immediate) {
|
|
|
|
if (!immediate) return;
|
|
|
|
|
2017-03-10 13:17:42 +00:00
|
|
|
if (async_hook_fields[kDestroy] > 0 &&
|
|
|
|
immediate._callback !== null &&
|
|
|
|
!immediate._destroyed) {
|
|
|
|
emitDestroy(immediate[async_id_symbol]);
|
|
|
|
immediate._destroyed = true;
|
|
|
|
}
|
|
|
|
|
2016-09-18 01:28:20 +00:00
|
|
|
immediate._onImmediate = null;
|
2012-08-08 02:12:01 +00:00
|
|
|
|
2016-09-18 01:28:20 +00:00
|
|
|
immediateQueue.remove(immediate);
|
2012-08-08 02:12:01 +00:00
|
|
|
|
2016-09-18 01:28:20 +00:00
|
|
|
if (!immediateQueue.head) {
|
2013-02-06 02:13:02 +00:00
|
|
|
process._needImmediateCallback = false;
|
2012-08-08 02:12:01 +00:00
|
|
|
}
|
|
|
|
};
|