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-12-18 12:43:53 +00:00
|
|
|
const {
|
2018-05-13 15:42:22 +00:00
|
|
|
getLibuvNow,
|
2018-02-05 19:29:32 +00:00
|
|
|
setupTimers,
|
2018-05-13 15:42:22 +00:00
|
|
|
scheduleTimer,
|
|
|
|
toggleTimerRef,
|
|
|
|
immediateInfo,
|
|
|
|
toggleImmediateRef
|
|
|
|
} = internalBinding('timers');
|
2015-09-26 21:27:36 +00:00
|
|
|
const L = require('internal/linkedlist');
|
2018-05-05 17:50:21 +00:00
|
|
|
const PriorityQueue = require('internal/priority_queue');
|
2018-02-11 21:35:59 +00:00
|
|
|
const {
|
|
|
|
async_id_symbol,
|
|
|
|
trigger_async_id_symbol,
|
|
|
|
Timeout,
|
2018-05-05 17:50:21 +00:00
|
|
|
kRefed,
|
2018-02-12 00:20:42 +00:00
|
|
|
initAsyncResource,
|
2018-02-11 21:35:59 +00:00
|
|
|
validateTimerDuration
|
|
|
|
} = require('internal/timers');
|
2017-04-14 19:42:47 +00:00
|
|
|
const internalUtil = require('internal/util');
|
2014-12-16 22:17:28 +00:00
|
|
|
const util = require('util');
|
2018-02-27 13:55:32 +00:00
|
|
|
const { ERR_INVALID_CALLBACK } = require('internal/errors').codes;
|
2019-03-06 11:54:12 +00:00
|
|
|
|
|
|
|
let debuglog;
|
|
|
|
function debug(...args) {
|
|
|
|
if (!debuglog) {
|
|
|
|
debuglog = require('internal/util/debuglog').debuglog('timer');
|
|
|
|
}
|
|
|
|
debuglog(...args);
|
|
|
|
}
|
|
|
|
|
2017-10-07 14:50:42 +00:00
|
|
|
const {
|
2018-02-11 21:35:59 +00:00
|
|
|
destroyHooksExist,
|
2017-10-07 14:50:42 +00:00
|
|
|
// The needed emit*() functions.
|
|
|
|
emitBefore,
|
|
|
|
emitAfter,
|
|
|
|
emitDestroy
|
2017-11-12 17:46:55 +00:00
|
|
|
} = require('internal/async_hooks');
|
2013-08-15 17:23:36 +00:00
|
|
|
|
2017-12-26 19:17:37 +00:00
|
|
|
// *Must* match Environment::ImmediateInfo::Fields in src/env.h.
|
|
|
|
const kCount = 0;
|
2018-01-13 21:51:28 +00:00
|
|
|
const kRefCount = 1;
|
|
|
|
const kHasOutstanding = 2;
|
2017-12-26 19:17:37 +00:00
|
|
|
|
2018-05-13 15:42:22 +00:00
|
|
|
// Call into C++ to assign callbacks that are responsible for processing
|
|
|
|
// Immediates and TimerLists.
|
|
|
|
setupTimers(processImmediate, processTimers);
|
2017-11-15 23:43:12 +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.
|
|
|
|
//
|
2018-03-29 01:38:17 +00:00
|
|
|
// Note: It is suggested you first read through the lib/internal/linkedlist.js
|
2016-02-26 19:19:51 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2018-02-04 19:38:18 +00:00
|
|
|
/* eslint-disable node-core/non-ascii-character */
|
2016-02-26 19:19:51 +00:00
|
|
|
//
|
|
|
|
// ╔════ > Object Map
|
|
|
|
// ║
|
|
|
|
// ╠══
|
2018-05-05 17:50:21 +00:00
|
|
|
// ║ lists: { '40': { }, '320': { etc } } (keys of millisecond duration)
|
|
|
|
// ╚══ ┌────┘
|
2016-02-26 19:19:51 +00:00
|
|
|
// │
|
|
|
|
// ╔══ │
|
2018-05-05 17:50:21 +00:00
|
|
|
// ║ TimersList { _idleNext: { }, _idlePrev: (self) }
|
2016-02-26 19:19:51 +00:00
|
|
|
// ║ ┌────────────────┘
|
|
|
|
// ║ ╔══ │ ^
|
|
|
|
// ║ ║ { _idleNext: { }, _idlePrev: { }, _onTimeout: (callback) }
|
|
|
|
// ║ ║ ┌───────────┘
|
|
|
|
// ║ ║ │ ^
|
|
|
|
// ║ ║ { _idleNext: { etc }, _idlePrev: { }, _onTimeout: (callback) }
|
|
|
|
// ╠══ ╠══
|
|
|
|
// ║ ║
|
|
|
|
// ║ ╚════ > Actual JavaScript timeouts
|
|
|
|
// ║
|
|
|
|
// ╚════ > Linked List
|
|
|
|
//
|
2018-02-04 19:38:18 +00:00
|
|
|
/* eslint-enable node-core/non-ascii-character */
|
2016-02-26 19:19:51 +00:00
|
|
|
//
|
|
|
|
// 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:
|
2018-05-05 17:50:21 +00:00
|
|
|
// The PriorityQueue — an efficient binary heap implementation that does all
|
|
|
|
// operations in worst-case O(log n) time — which manages the order of expiring
|
|
|
|
// Timeout lists 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 timers architectures.
|
2016-02-26 19:19:51 +00:00
|
|
|
|
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
// Object map containing linked lists of timers, keyed and sorted by their
|
2016-02-26 19:18:52 +00:00
|
|
|
// duration in milliseconds.
|
2011-01-13 10:09:03 +00:00
|
|
|
//
|
2016-02-26 19:19:51 +00:00
|
|
|
// - key = time in milliseconds
|
|
|
|
// - value = linked list
|
2018-05-05 17:50:21 +00:00
|
|
|
const lists = Object.create(null);
|
|
|
|
|
|
|
|
// This is a priority queue with a custom sorting function that first compares
|
|
|
|
// the expiry times of two lists and if they're the same then compares their
|
|
|
|
// individual IDs to determine which list was created first.
|
|
|
|
const queue = new PriorityQueue(compareTimersLists, setPosition);
|
|
|
|
|
|
|
|
function compareTimersLists(a, b) {
|
|
|
|
const expiryDiff = a.expiry - b.expiry;
|
|
|
|
if (expiryDiff === 0) {
|
|
|
|
if (a.id < b.id)
|
|
|
|
return -1;
|
|
|
|
if (a.id > b.id)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return expiryDiff;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setPosition(node, pos) {
|
|
|
|
node.priorityQueuePosition = pos;
|
|
|
|
}
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
let nextExpiry = Infinity;
|
|
|
|
|
|
|
|
let timerListId = Number.MIN_SAFE_INTEGER;
|
|
|
|
let refCount = 0;
|
|
|
|
|
|
|
|
function incRefCount() {
|
|
|
|
if (refCount++ === 0)
|
2018-05-13 15:42:22 +00:00
|
|
|
toggleTimerRef(true);
|
2018-05-05 17:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function decRefCount() {
|
|
|
|
if (--refCount === 0)
|
2018-05-13 15:42:22 +00:00
|
|
|
toggleTimerRef(false);
|
2018-05-05 17:50:21 +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) {
|
2018-05-13 15:42:22 +00:00
|
|
|
insert(item, true, getLibuvNow());
|
2016-02-26 19:18:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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) {
|
2018-05-13 15:42:22 +00:00
|
|
|
insert(item, false, getLibuvNow());
|
2016-02-26 19:18:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
2018-05-13 15:42:22 +00:00
|
|
|
// list if one does not already exist for the specified timeout duration.
|
2018-05-05 17:50:21 +00:00
|
|
|
function insert(item, refed, start) {
|
2018-12-04 01:41:58 +00:00
|
|
|
let msecs = item._idleTimeout;
|
2018-05-05 17:50:21 +00:00
|
|
|
if (msecs < 0 || msecs === undefined)
|
|
|
|
return;
|
2010-10-26 18:56:32 +00:00
|
|
|
|
2018-12-04 01:41:58 +00:00
|
|
|
// Truncate so that accuracy of sub-milisecond timers is not assumed.
|
|
|
|
msecs = Math.trunc(msecs);
|
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
item._idleStart = start;
|
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];
|
2017-11-29 17:46:13 +00:00
|
|
|
if (list === undefined) {
|
2016-02-26 19:18:52 +00:00
|
|
|
debug('no %d list was found in insert, creating a new one', msecs);
|
2018-05-05 17:50:21 +00:00
|
|
|
const expiry = start + msecs;
|
|
|
|
lists[msecs] = list = new TimersList(expiry, msecs);
|
|
|
|
queue.insert(list);
|
|
|
|
|
|
|
|
if (nextExpiry > expiry) {
|
2018-05-13 15:42:22 +00:00
|
|
|
scheduleTimer(msecs);
|
2018-05-05 17:50:21 +00:00
|
|
|
nextExpiry = expiry;
|
|
|
|
}
|
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;
|
2018-02-12 00:20:42 +00:00
|
|
|
initAsyncResource(item, 'Timeout');
|
2017-03-10 13:17:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
if (refed === !item[kRefed]) {
|
|
|
|
if (refed)
|
|
|
|
incRefCount();
|
|
|
|
else
|
|
|
|
decRefCount();
|
|
|
|
}
|
|
|
|
item[kRefed] = refed;
|
|
|
|
|
2012-12-27 19:40:42 +00:00
|
|
|
L.append(list, item);
|
2016-02-26 19:18:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
function TimersList(expiry, msecs) {
|
2017-11-29 17:46:13 +00:00
|
|
|
this._idleNext = this; // Create the list with the linkedlist properties to
|
2019-01-21 00:22:27 +00:00
|
|
|
this._idlePrev = this; // Prevent any unnecessary hidden class changes.
|
2018-05-05 17:50:21 +00:00
|
|
|
this.expiry = expiry;
|
|
|
|
this.id = timerListId++;
|
2016-02-26 19:18:52 +00:00
|
|
|
this.msecs = msecs;
|
2018-05-05 17:50:21 +00:00
|
|
|
this.priorityQueuePosition = null;
|
2016-02-26 19:18:52 +00:00
|
|
|
}
|
2012-12-27 19:40:42 +00:00
|
|
|
|
2018-09-26 22:22:12 +00:00
|
|
|
// Make sure the linked list only shows the minimal necessary information.
|
|
|
|
TimersList.prototype[util.inspect.custom] = function(_, options) {
|
|
|
|
return util.inspect(this, {
|
|
|
|
...options,
|
|
|
|
// Only inspect one level.
|
|
|
|
depth: 0,
|
|
|
|
// It should not recurse.
|
|
|
|
customInspect: false
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
const { _tickCallback: runNextTicks } = process;
|
2018-02-05 19:29:32 +00:00
|
|
|
function processTimers(now) {
|
2018-05-05 17:50:21 +00:00
|
|
|
debug('process timer lists %d', now);
|
|
|
|
nextExpiry = Infinity;
|
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
let list;
|
|
|
|
let ranAtLeastOneList = false;
|
2018-05-05 17:50:21 +00:00
|
|
|
while (list = queue.peek()) {
|
2018-05-13 15:42:22 +00:00
|
|
|
if (list.expiry > now) {
|
|
|
|
nextExpiry = list.expiry;
|
|
|
|
return refCount > 0 ? nextExpiry : -nextExpiry;
|
|
|
|
}
|
2018-09-13 14:35:15 +00:00
|
|
|
if (ranAtLeastOneList)
|
2018-05-05 17:50:21 +00:00
|
|
|
runNextTicks();
|
2018-05-13 15:42:22 +00:00
|
|
|
else
|
2018-09-13 14:35:15 +00:00
|
|
|
ranAtLeastOneList = true;
|
2018-05-05 17:50:21 +00:00
|
|
|
listOnTimeout(list, now);
|
|
|
|
}
|
2018-05-13 15:42:22 +00:00
|
|
|
return 0;
|
2018-02-05 19:29:32 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
function listOnTimeout(list, now) {
|
2018-01-31 15:36:32 +00:00
|
|
|
const 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 diff, timer;
|
2018-09-13 14:35:15 +00:00
|
|
|
let ranAtLeastOneTimer = false;
|
2016-02-26 19:18:52 +00:00
|
|
|
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) {
|
2018-11-07 05:44:31 +00:00
|
|
|
list.expiry = Math.max(timer._idleStart + msecs, now + 1);
|
2018-05-05 17:50:21 +00:00
|
|
|
list.id = timerListId++;
|
|
|
|
queue.percolateDown(1);
|
2013-05-21 22:22:05 +00:00
|
|
|
debug('%d list wait because diff is %d', msecs, diff);
|
2018-05-05 17:50:21 +00:00
|
|
|
return;
|
2016-02-26 19:18:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
if (ranAtLeastOneTimer)
|
|
|
|
runNextTicks();
|
|
|
|
else
|
|
|
|
ranAtLeastOneTimer = true;
|
|
|
|
|
2016-02-26 19:19:51 +00:00
|
|
|
// The actual logic for when a timeout happens.
|
2016-02-26 19:18:52 +00:00
|
|
|
L.remove(timer);
|
2018-05-05 17:50:21 +00:00
|
|
|
|
|
|
|
const asyncId = timer[async_id_symbol];
|
2012-12-27 19:40:42 +00:00
|
|
|
|
2017-03-10 13:17:42 +00:00
|
|
|
if (!timer._onTimeout) {
|
2018-05-05 17:50:21 +00:00
|
|
|
if (timer[kRefed])
|
|
|
|
refCount--;
|
|
|
|
timer[kRefed] = null;
|
|
|
|
|
|
|
|
if (destroyHooksExist() && !timer._destroyed) {
|
|
|
|
emitDestroy(asyncId);
|
2017-03-10 13:17:42 +00:00
|
|
|
timer._destroyed = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2016-02-26 19:18:52 +00:00
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
emitBefore(asyncId, timer[trigger_async_id_symbol]);
|
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
let start;
|
|
|
|
if (timer._repeat)
|
|
|
|
start = getLibuvNow();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const args = timer._timerArgs;
|
2019-03-10 00:26:17 +00:00
|
|
|
if (args === undefined)
|
2018-09-13 14:35:15 +00:00
|
|
|
timer._onTimeout();
|
|
|
|
else
|
|
|
|
Reflect.apply(timer._onTimeout, timer, args);
|
|
|
|
} finally {
|
|
|
|
if (timer._repeat && timer._idleTimeout !== -1) {
|
|
|
|
timer._idleTimeout = timer._repeat;
|
|
|
|
if (start === undefined)
|
|
|
|
start = getLibuvNow();
|
|
|
|
insert(timer, timer[kRefed], start);
|
|
|
|
} else {
|
|
|
|
if (timer[kRefed])
|
|
|
|
refCount--;
|
|
|
|
timer[kRefed] = null;
|
|
|
|
|
|
|
|
if (destroyHooksExist() && !timer._destroyed) {
|
|
|
|
emitDestroy(timer[async_id_symbol]);
|
|
|
|
timer._destroyed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-05 17:50:21 +00:00
|
|
|
|
|
|
|
emitAfter(asyncId);
|
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.
|
2018-05-05 17:50:21 +00:00
|
|
|
// As such, we can remove the list from the object map and the PriorityQueue.
|
2013-05-21 22:22:05 +00:00
|
|
|
debug('%d list empty', msecs);
|
2017-02-20 05:31:15 +00:00
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
// The current list 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 === lists[msecs]) {
|
|
|
|
delete lists[msecs];
|
|
|
|
queue.shift();
|
|
|
|
}
|
2018-02-05 19:29:32 +00:00
|
|
|
}
|
2016-02-26 19:18:52 +00:00
|
|
|
|
|
|
|
|
2016-02-26 19:19:51 +00:00
|
|
|
// Remove a timer. Cancels the timeout and resets the relevant timer properties.
|
2018-01-09 18:25:20 +00:00
|
|
|
function unenroll(item) {
|
2017-03-10 13:17:42 +00:00
|
|
|
// Fewer checks may be possible, but these cover everything.
|
2018-02-11 21:35:59 +00:00
|
|
|
if (destroyHooksExist() &&
|
2018-05-05 17:50:21 +00:00
|
|
|
item[async_id_symbol] !== undefined &&
|
2017-03-10 13:17:42 +00:00
|
|
|
!item._destroyed) {
|
|
|
|
emitDestroy(item[async_id_symbol]);
|
|
|
|
item._destroyed = true;
|
|
|
|
}
|
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
L.remove(item);
|
|
|
|
|
|
|
|
// We only delete refed lists because unrefed ones are incredibly likely
|
|
|
|
// to come from http and be recreated shortly after.
|
|
|
|
// TODO: Long-term this could instead be handled by creating an internal
|
|
|
|
// clearTimeout that makes it clear that the list should not be deleted.
|
|
|
|
// That function could then be used by http and other similar modules.
|
|
|
|
if (item[kRefed]) {
|
2018-12-04 01:41:58 +00:00
|
|
|
// Compliment truncation during insert().
|
|
|
|
const msecs = Math.trunc(item._idleTimeout);
|
|
|
|
const list = lists[msecs];
|
2018-05-05 17:50:21 +00:00
|
|
|
if (list !== undefined && L.isEmpty(list)) {
|
|
|
|
debug('unenroll: list empty');
|
|
|
|
queue.removeAt(list.priorityQueuePosition);
|
|
|
|
delete lists[list.msecs];
|
|
|
|
}
|
|
|
|
|
|
|
|
decRefCount();
|
2010-10-26 18:56:32 +00:00
|
|
|
}
|
2018-05-05 17:50:21 +00:00
|
|
|
item[kRefed] = null;
|
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// If active is called later, then we want to make sure not to insert again
|
2011-12-22 13:42:20 +00:00
|
|
|
item._idleTimeout = -1;
|
2018-01-09 18:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.unenroll = util.deprecate(unenroll,
|
|
|
|
'timers.unenroll() is deprecated. ' +
|
|
|
|
'Please use clearTimeout instead.',
|
2018-02-04 18:59:53 +00:00
|
|
|
'DEP0096');
|
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.
|
2018-01-09 18:25:20 +00:00
|
|
|
function enroll(item, msecs) {
|
2019-02-20 15:47:40 +00:00
|
|
|
msecs = validateTimerDuration(msecs, 'msecs');
|
2014-12-16 22:17:28 +00:00
|
|
|
|
2019-01-21 00:22:27 +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
|
|
|
|
2011-01-18 22:26:32 +00:00
|
|
|
L.init(item);
|
2018-04-11 08:43:21 +00:00
|
|
|
item._idleTimeout = msecs;
|
2018-01-09 18:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.enroll = util.deprecate(enroll,
|
2018-02-04 18:59:53 +00:00
|
|
|
'timers.enroll() is deprecated. ' +
|
2018-02-10 21:13:46 +00:00
|
|
|
'Please use setTimeout instead.',
|
2018-02-04 18:59:53 +00:00
|
|
|
'DEP0095');
|
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') {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_INVALID_CALLBACK();
|
2015-12-20 11:30:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:07:26 +00:00
|
|
|
var i, args;
|
|
|
|
switch (arguments.length) {
|
|
|
|
// fast cases
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
args = [arg1];
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
args = [arg1, arg2];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
args = [arg1, arg2, arg3];
|
|
|
|
for (i = 5; i < arguments.length; i++) {
|
2018-12-03 16:15:45 +00:00
|
|
|
// Extend array dynamically, makes .apply run much faster in v6.0.0
|
2017-11-25 13:07:26 +00:00
|
|
|
args[i - 2] = arguments[i];
|
|
|
|
}
|
|
|
|
break;
|
2011-07-11 22:30:24 +00:00
|
|
|
}
|
2011-06-08 02:35:50 +00:00
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
const timeout = new Timeout(callback, after, args, false);
|
2017-02-03 21:05:59 +00:00
|
|
|
active(timeout);
|
|
|
|
|
|
|
|
return timeout;
|
2017-04-14 19:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout[internalUtil.promisify.custom] = function(after, value) {
|
2019-03-10 00:26:17 +00:00
|
|
|
const args = value !== undefined ? [value] : value;
|
2018-05-18 22:25:07 +00:00
|
|
|
return new Promise((resolve) => {
|
2019-03-10 00:26:17 +00:00
|
|
|
active(new Timeout(resolve, after, args, false));
|
2018-05-18 22:25:07 +00:00
|
|
|
});
|
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
|
|
|
|
2018-04-29 08:49:06 +00:00
|
|
|
const clearTimeout = exports.clearTimeout = function clearTimeout(timer) {
|
2018-02-05 18:09:08 +00:00
|
|
|
if (timer && timer._onTimeout) {
|
|
|
|
timer._onTimeout = null;
|
2018-05-05 17:50:21 +00:00
|
|
|
unenroll(timer);
|
2010-10-29 07:00:43 +00:00
|
|
|
}
|
2010-10-26 19:52:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-04-29 08:49:06 +00:00
|
|
|
exports.setInterval = function setInterval(callback, repeat, arg1, arg2, arg3) {
|
2015-12-20 11:30:04 +00:00
|
|
|
if (typeof callback !== 'function') {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_INVALID_CALLBACK();
|
2015-12-20 11:30:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 13:07:26 +00:00
|
|
|
var i, args;
|
|
|
|
switch (arguments.length) {
|
|
|
|
// fast cases
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
args = [arg1];
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
args = [arg1, arg2];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
args = [arg1, arg2, arg3];
|
|
|
|
for (i = 5; i < arguments.length; i++) {
|
2018-12-03 16:15:45 +00:00
|
|
|
// Extend array dynamically, makes .apply run much faster in v6.0.0
|
2017-11-25 13:07:26 +00:00
|
|
|
args[i - 2] = arguments[i];
|
|
|
|
}
|
|
|
|
break;
|
2016-09-20 06:10:50 +00:00
|
|
|
}
|
2012-07-01 20:58:22 +00:00
|
|
|
|
2018-05-05 17:50:21 +00:00
|
|
|
const timeout = new Timeout(callback, repeat, args, true);
|
2017-02-03 21:05:59 +00:00
|
|
|
active(timeout);
|
|
|
|
|
|
|
|
return timeout;
|
2016-09-20 06:10:50 +00:00
|
|
|
};
|
|
|
|
|
2018-04-11 15:49:07 +00:00
|
|
|
exports.clearInterval = function clearInterval(timer) {
|
|
|
|
// clearTimeout and clearInterval can be used to clear timers created from
|
|
|
|
// both setTimeout and setInterval, as specified by HTML Living Standard:
|
|
|
|
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
|
|
|
|
clearTimeout(timer);
|
2010-10-26 19:14:17 +00:00
|
|
|
};
|
2012-07-13 02:19:01 +00:00
|
|
|
|
2014-11-26 20:27:57 +00:00
|
|
|
|
2012-07-13 02:19:01 +00:00
|
|
|
Timeout.prototype.unref = function() {
|
2018-05-05 17:50:21 +00:00
|
|
|
if (this[kRefed]) {
|
|
|
|
this[kRefed] = false;
|
|
|
|
decRefCount();
|
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
|
|
|
};
|
|
|
|
|
|
|
|
Timeout.prototype.ref = function() {
|
2018-05-05 17:50:21 +00:00
|
|
|
if (this[kRefed] === false) {
|
|
|
|
this[kRefed] = true;
|
|
|
|
incRefCount();
|
|
|
|
}
|
2015-09-13 15:21:51 +00:00
|
|
|
return this;
|
2012-07-13 02:19:01 +00:00
|
|
|
};
|
|
|
|
|
2018-05-22 22:01:53 +00:00
|
|
|
Timeout.prototype.hasRef = function() {
|
|
|
|
return !!this[kRefed];
|
|
|
|
};
|
|
|
|
|
2012-07-13 02:19:01 +00:00
|
|
|
Timeout.prototype.close = function() {
|
2018-05-05 17:50:21 +00:00
|
|
|
clearTimeout(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
|
|
|
|
|
|
|
|
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) {
|
2017-11-29 17:46:13 +00:00
|
|
|
if (this.tail !== null) {
|
2016-09-18 01:28:20 +00:00
|
|
|
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) {
|
2017-11-29 17:46:13 +00:00
|
|
|
if (item._idleNext !== null) {
|
2016-09-18 01:28:20 +00:00
|
|
|
item._idleNext._idlePrev = item._idlePrev;
|
|
|
|
}
|
|
|
|
|
2017-11-29 17:46:13 +00:00
|
|
|
if (item._idlePrev !== null) {
|
2016-09-18 01:28:20 +00:00
|
|
|
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
|
2017-12-26 19:17:37 +00:00
|
|
|
const immediateQueue = new ImmediateList();
|
|
|
|
|
|
|
|
// If an uncaught exception was thrown during execution of immediateQueue,
|
|
|
|
// this queue will store all remaining Immediates that need to run upon
|
|
|
|
// resolution of all error handling (if process is still alive).
|
|
|
|
const outstandingQueue = new ImmediateList();
|
2012-08-08 02:12:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
function processImmediate() {
|
2017-12-26 19:17:37 +00:00
|
|
|
const queue = outstandingQueue.head !== null ?
|
|
|
|
outstandingQueue : immediateQueue;
|
|
|
|
var immediate = queue.head;
|
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
|
2018-09-13 14:35:15 +00:00
|
|
|
if (queue !== outstandingQueue) {
|
|
|
|
queue.head = queue.tail = null;
|
|
|
|
immediateInfo[kHasOutstanding] = 1;
|
|
|
|
}
|
2016-04-28 00:31:59 +00:00
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
let prevImmediate;
|
|
|
|
let ranAtLeastOneImmediate = false;
|
2018-01-13 21:51:28 +00:00
|
|
|
while (immediate !== null) {
|
2018-09-13 14:35:15 +00:00
|
|
|
if (ranAtLeastOneImmediate)
|
|
|
|
runNextTicks();
|
|
|
|
else
|
|
|
|
ranAtLeastOneImmediate = true;
|
2017-12-26 19:17:37 +00:00
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
// It's possible for this current Immediate to be cleared while executing
|
|
|
|
// the next tick queue above, which means we need to use the previous
|
|
|
|
// Immediate's _idleNext which is guaranteed to not have been cleared.
|
|
|
|
if (immediate._destroyed) {
|
|
|
|
outstandingQueue.head = immediate = prevImmediate._idleNext;
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-13 22:02:52 +00:00
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
immediate._destroyed = true;
|
|
|
|
|
|
|
|
immediateInfo[kCount]--;
|
2018-01-13 21:51:28 +00:00
|
|
|
if (immediate[kRefed])
|
2018-09-13 14:35:15 +00:00
|
|
|
immediateInfo[kRefCount]--;
|
2018-05-22 22:01:53 +00:00
|
|
|
immediate[kRefed] = null;
|
2018-01-13 21:51:28 +00:00
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
prevImmediate = immediate;
|
2017-12-26 19:17:37 +00:00
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
const asyncId = immediate[async_id_symbol];
|
|
|
|
emitBefore(asyncId, immediate[trigger_async_id_symbol]);
|
2013-09-24 21:12:11 +00:00
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
try {
|
|
|
|
const argv = immediate._argv;
|
|
|
|
if (!argv)
|
|
|
|
immediate._onImmediate();
|
|
|
|
else
|
|
|
|
Reflect.apply(immediate._onImmediate, immediate, argv);
|
|
|
|
} finally {
|
|
|
|
immediate._onImmediate = null;
|
2017-12-26 19:17:37 +00:00
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
if (destroyHooksExist())
|
|
|
|
emitDestroy(asyncId);
|
2012-08-08 02:12:01 +00:00
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
outstandingQueue.head = immediate = immediate._idleNext;
|
2017-03-10 13:17:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
emitAfter(asyncId);
|
2015-12-06 06:35:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 14:35:15 +00:00
|
|
|
if (queue === outstandingQueue)
|
|
|
|
outstandingQueue.head = null;
|
|
|
|
immediateInfo[kHasOutstanding] = 0;
|
2016-04-28 00:31:59 +00:00
|
|
|
}
|
2015-12-06 06:35:52 +00:00
|
|
|
|
2013-10-07 19:39:52 +00:00
|
|
|
|
2018-01-13 21:51:28 +00:00
|
|
|
const Immediate = class Immediate {
|
|
|
|
constructor(callback, args) {
|
|
|
|
this._idleNext = null;
|
|
|
|
this._idlePrev = null;
|
2018-12-10 12:27:32 +00:00
|
|
|
// This must be set to null first to avoid function tracking
|
2018-01-13 21:51:28 +00:00
|
|
|
// on the hidden class, revisit in V8 versions after 6.2
|
|
|
|
this._onImmediate = null;
|
|
|
|
this._onImmediate = callback;
|
|
|
|
this._argv = args;
|
|
|
|
this._destroyed = false;
|
|
|
|
this[kRefed] = false;
|
|
|
|
|
2018-02-12 00:20:42 +00:00
|
|
|
initAsyncResource(this, 'Immediate');
|
2018-01-13 21:51:28 +00:00
|
|
|
|
|
|
|
this.ref();
|
|
|
|
immediateInfo[kCount]++;
|
2017-11-25 13:07:26 +00:00
|
|
|
|
2018-01-13 21:51:28 +00:00
|
|
|
immediateQueue.append(this);
|
2017-11-25 13:07:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 21:51:28 +00:00
|
|
|
ref() {
|
|
|
|
if (this[kRefed] === false) {
|
|
|
|
this[kRefed] = true;
|
|
|
|
if (immediateInfo[kRefCount]++ === 0)
|
|
|
|
toggleImmediateRef(true);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2017-11-25 13:07:26 +00:00
|
|
|
|
2018-01-13 21:51:28 +00:00
|
|
|
unref() {
|
|
|
|
if (this[kRefed] === true) {
|
|
|
|
this[kRefed] = false;
|
|
|
|
if (--immediateInfo[kRefCount] === 0)
|
|
|
|
toggleImmediateRef(false);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2018-05-22 22:01:53 +00:00
|
|
|
|
|
|
|
hasRef() {
|
|
|
|
return !!this[kRefed];
|
|
|
|
}
|
2018-01-13 21:51:28 +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') {
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_INVALID_CALLBACK();
|
2015-12-20 11:30:04 +00:00
|
|
|
}
|
|
|
|
|
2015-01-10 15:49:21 +00:00
|
|
|
var i, args;
|
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];
|
2017-11-25 13:07:26 +00:00
|
|
|
for (i = 4; i < arguments.length; i++) {
|
2018-12-03 16:15:45 +00:00
|
|
|
// 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];
|
2017-11-25 13:07:26 +00:00
|
|
|
}
|
2015-01-10 15:49:21 +00:00
|
|
|
break;
|
2012-08-08 02:12:01 +00:00
|
|
|
}
|
2017-11-25 13:07:26 +00:00
|
|
|
|
|
|
|
return new Immediate(callback, args);
|
2017-04-14 19:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setImmediate[internalUtil.promisify.custom] = function(value) {
|
2018-05-18 22:25:07 +00:00
|
|
|
return new Promise((resolve) => new Immediate(resolve, [value]));
|
2016-09-18 01:28:20 +00:00
|
|
|
};
|
|
|
|
|
2017-04-14 19:42:47 +00:00
|
|
|
exports.setImmediate = setImmediate;
|
|
|
|
|
2012-08-08 02:12:01 +00:00
|
|
|
|
2018-04-29 08:49:06 +00:00
|
|
|
exports.clearImmediate = function clearImmediate(immediate) {
|
2018-01-13 21:51:28 +00:00
|
|
|
if (!immediate || immediate._destroyed)
|
|
|
|
return;
|
2012-08-08 02:12:01 +00:00
|
|
|
|
2018-01-13 21:51:28 +00:00
|
|
|
immediateInfo[kCount]--;
|
|
|
|
immediate._destroyed = true;
|
2017-11-15 23:43:12 +00:00
|
|
|
|
2018-01-13 21:51:28 +00:00
|
|
|
if (immediate[kRefed] && --immediateInfo[kRefCount] === 0)
|
|
|
|
toggleImmediateRef(false);
|
2018-05-22 22:01:53 +00:00
|
|
|
immediate[kRefed] = null;
|
2018-01-13 21:51:28 +00:00
|
|
|
|
2018-02-11 21:35:59 +00:00
|
|
|
if (destroyHooksExist()) {
|
2018-01-13 21:51:28 +00:00
|
|
|
emitDestroy(immediate[async_id_symbol]);
|
2017-03-10 13:17:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|