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';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
|
|
|
MathTrunc,
|
2020-09-27 15:39:01 +00:00
|
|
|
ObjectCreate,
|
|
|
|
ObjectDefineProperty,
|
2020-06-19 18:31:21 +00:00
|
|
|
SymbolToPrimitive
|
2019-11-22 17:04:46 +00:00
|
|
|
} = primordials;
|
2019-03-31 11:30:12 +00:00
|
|
|
|
2017-12-18 12:43:53 +00:00
|
|
|
const {
|
2018-05-13 15:42:22 +00:00
|
|
|
immediateInfo,
|
|
|
|
toggleImmediateRef
|
|
|
|
} = internalBinding('timers');
|
2015-09-26 21:27:36 +00:00
|
|
|
const L = require('internal/linkedlist');
|
2018-02-11 21:35:59 +00:00
|
|
|
const {
|
|
|
|
async_id_symbol,
|
|
|
|
Timeout,
|
2020-06-18 20:22:17 +00:00
|
|
|
Immediate,
|
2019-03-11 11:08:35 +00:00
|
|
|
decRefCount,
|
|
|
|
immediateInfoFields: {
|
|
|
|
kCount,
|
|
|
|
kRefCount
|
|
|
|
},
|
2018-05-05 17:50:21 +00:00
|
|
|
kRefed,
|
2020-06-19 18:31:21 +00:00
|
|
|
kHasPrimitive,
|
2019-03-20 11:12:48 +00:00
|
|
|
getTimerDuration,
|
2019-03-11 11:08:35 +00:00
|
|
|
timerListMap,
|
|
|
|
timerListQueue,
|
|
|
|
immediateQueue,
|
|
|
|
active,
|
2019-04-22 17:30:39 +00:00
|
|
|
unrefActive,
|
|
|
|
insert
|
2018-02-11 21:35:59 +00:00
|
|
|
} = require('internal/timers');
|
2019-03-11 07:19:47 +00:00
|
|
|
const {
|
|
|
|
promisify: { custom: customPromisify },
|
|
|
|
deprecate
|
|
|
|
} = require('internal/util');
|
2020-03-14 11:55:44 +00:00
|
|
|
let debug = require('internal/util/debuglog').debuglog('timer', (fn) => {
|
|
|
|
debug = fn;
|
|
|
|
});
|
2020-04-05 09:35:52 +00:00
|
|
|
const { validateCallback } = require('internal/validators');
|
2019-03-06 11:54:12 +00:00
|
|
|
|
2020-06-18 20:22:17 +00:00
|
|
|
let timersPromises;
|
|
|
|
|
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.
|
|
|
|
emitDestroy
|
2017-11-12 17:46:55 +00:00
|
|
|
} = require('internal/async_hooks');
|
2013-08-15 17:23:36 +00:00
|
|
|
|
2020-06-19 18:31:21 +00:00
|
|
|
// This stores all the known timer async ids to allow users to clearTimeout and
|
|
|
|
// clearInterval using those ids, to match the spec and the rest of the web
|
|
|
|
// platform.
|
|
|
|
const knownTimersById = ObjectCreate(null);
|
|
|
|
|
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) {
|
2019-04-22 20:52:17 +00:00
|
|
|
if (item._destroyed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
item._destroyed = true;
|
|
|
|
|
2020-06-19 18:31:21 +00:00
|
|
|
if (item[kHasPrimitive])
|
|
|
|
delete knownTimersById[item[async_id_symbol]];
|
|
|
|
|
2017-03-10 13:17:42 +00:00
|
|
|
// Fewer checks may be possible, but these cover everything.
|
2019-04-22 20:52:17 +00:00
|
|
|
if (destroyHooksExist() && item[async_id_symbol] !== undefined)
|
2017-03-10 13:17:42 +00:00
|
|
|
emitDestroy(item[async_id_symbol]);
|
|
|
|
|
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().
|
2019-11-22 17:04:46 +00:00
|
|
|
const msecs = MathTrunc(item._idleTimeout);
|
2019-03-11 11:08:35 +00:00
|
|
|
const list = timerListMap[msecs];
|
2018-05-05 17:50:21 +00:00
|
|
|
if (list !== undefined && L.isEmpty(list)) {
|
|
|
|
debug('unenroll: list empty');
|
2019-03-11 11:08:35 +00:00
|
|
|
timerListQueue.removeAt(list.priorityQueuePosition);
|
|
|
|
delete timerListMap[list.msecs];
|
2018-05-05 17:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decRefCount();
|
2010-10-26 18:56:32 +00:00
|
|
|
}
|
2018-05-05 17:50:21 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-03-20 11:12:48 +00:00
|
|
|
msecs = getTimerDuration(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
|
|
|
}
|
|
|
|
|
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) {
|
2020-04-05 09:35:52 +00:00
|
|
|
validateCallback(callback);
|
2015-12-20 11:30:04 +00:00
|
|
|
|
2019-11-12 15:23:40 +00:00
|
|
|
let i, args;
|
2017-11-25 13:07:26 +00:00
|
|
|
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
|
|
|
|
2019-04-22 17:30:39 +00:00
|
|
|
const timeout = new Timeout(callback, after, args, false, true);
|
|
|
|
insert(timeout, timeout._idleTimeout);
|
2017-02-03 21:05:59 +00:00
|
|
|
|
|
|
|
return timeout;
|
2017-04-14 19:42:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-27 15:39:01 +00:00
|
|
|
ObjectDefineProperty(setTimeout, customPromisify, {
|
2020-06-18 20:22:17 +00:00
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
if (!timersPromises)
|
|
|
|
timersPromises = require('timers/promises');
|
|
|
|
return timersPromises.setTimeout;
|
2020-06-10 21:14:23 +00:00
|
|
|
}
|
2020-06-18 20:22:17 +00:00
|
|
|
});
|
2016-09-20 06:10:50 +00:00
|
|
|
|
2019-03-11 07:17:47 +00:00
|
|
|
function clearTimeout(timer) {
|
2021-01-04 10:39:35 +00:00
|
|
|
if (timer?._onTimeout) {
|
2018-02-05 18:09:08 +00:00
|
|
|
timer._onTimeout = null;
|
2018-05-05 17:50:21 +00:00
|
|
|
unenroll(timer);
|
2020-06-19 18:31:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof timer === 'number' || typeof timer === 'string') {
|
|
|
|
const timerInstance = knownTimersById[timer];
|
|
|
|
if (timerInstance !== undefined) {
|
|
|
|
timerInstance._onTimeout = null;
|
|
|
|
unenroll(timerInstance);
|
|
|
|
}
|
2010-10-29 07:00:43 +00:00
|
|
|
}
|
2019-03-11 07:17:47 +00:00
|
|
|
}
|
2010-10-26 19:52:31 +00:00
|
|
|
|
2019-03-11 07:17:47 +00:00
|
|
|
function setInterval(callback, repeat, arg1, arg2, arg3) {
|
2020-04-05 09:35:52 +00:00
|
|
|
validateCallback(callback);
|
2015-12-20 11:30:04 +00:00
|
|
|
|
2019-11-12 15:23:40 +00:00
|
|
|
let i, args;
|
2017-11-25 13:07:26 +00:00
|
|
|
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
|
|
|
|
2019-04-22 17:30:39 +00:00
|
|
|
const timeout = new Timeout(callback, repeat, args, true, true);
|
|
|
|
insert(timeout, timeout._idleTimeout);
|
2017-02-03 21:05:59 +00:00
|
|
|
|
|
|
|
return timeout;
|
2019-03-11 07:17:47 +00:00
|
|
|
}
|
2016-09-20 06:10:50 +00:00
|
|
|
|
2019-03-11 07:17:47 +00:00
|
|
|
function clearInterval(timer) {
|
2018-04-11 15:49:07 +00:00
|
|
|
// 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);
|
2019-03-11 07:17:47 +00:00
|
|
|
}
|
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
|
|
|
|
2020-06-19 18:31:21 +00:00
|
|
|
Timeout.prototype[SymbolToPrimitive] = function() {
|
|
|
|
const id = this[async_id_symbol];
|
|
|
|
if (!this[kHasPrimitive]) {
|
|
|
|
this[kHasPrimitive] = true;
|
|
|
|
knownTimersById[id] = this;
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
2017-04-14 19:42:47 +00:00
|
|
|
function setImmediate(callback, arg1, arg2, arg3) {
|
2020-04-05 09:35:52 +00:00
|
|
|
validateCallback(callback);
|
2015-12-20 11:30:04 +00:00
|
|
|
|
2019-11-12 15:23:40 +00:00
|
|
|
let 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
|
|
|
}
|
|
|
|
|
2020-09-27 15:39:01 +00:00
|
|
|
ObjectDefineProperty(setImmediate, customPromisify, {
|
2020-06-18 20:22:17 +00:00
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
if (!timersPromises)
|
|
|
|
timersPromises = require('timers/promises');
|
|
|
|
return timersPromises.setImmediate;
|
2020-06-10 21:14:23 +00:00
|
|
|
}
|
2020-06-18 20:22:17 +00:00
|
|
|
});
|
|
|
|
|
2016-09-18 01:28:20 +00:00
|
|
|
|
2019-03-11 07:17:47 +00:00
|
|
|
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);
|
2019-03-11 07:17:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
setTimeout,
|
|
|
|
clearTimeout,
|
|
|
|
setImmediate,
|
|
|
|
clearImmediate,
|
|
|
|
setInterval,
|
|
|
|
clearInterval,
|
2019-03-18 22:56:49 +00:00
|
|
|
_unrefActive: deprecate(
|
|
|
|
unrefActive,
|
|
|
|
'timers._unrefActive() is deprecated.' +
|
|
|
|
' Please use timeout.refresh() instead.',
|
|
|
|
'DEP0127'),
|
|
|
|
active: deprecate(
|
|
|
|
active,
|
|
|
|
'timers.active() is deprecated. Please use timeout.refresh() instead.',
|
|
|
|
'DEP0126'),
|
2019-03-11 07:19:47 +00:00
|
|
|
unenroll: deprecate(
|
2019-03-11 07:17:47 +00:00
|
|
|
unenroll,
|
|
|
|
'timers.unenroll() is deprecated. Please use clearTimeout instead.',
|
|
|
|
'DEP0096'),
|
2019-03-11 07:19:47 +00:00
|
|
|
enroll: deprecate(
|
2019-03-11 07:17:47 +00:00
|
|
|
enroll,
|
|
|
|
'timers.enroll() is deprecated. Please use setTimeout instead.',
|
|
|
|
'DEP0095')
|
2012-08-08 02:12:01 +00:00
|
|
|
};
|