2017-01-03 13:16:48 -08: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 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
const {
|
|
|
|
MathTrunc,
|
2023-12-23 11:23:43 +01:00
|
|
|
ObjectDefineProperties,
|
2024-04-21 18:53:08 +02:00
|
|
|
ObjectDefineProperty,
|
2024-10-07 11:47:44 +02:00
|
|
|
SymbolDispose,
|
2023-02-12 19:26:21 +01:00
|
|
|
SymbolToPrimitive,
|
2019-11-22 18:04:46 +01:00
|
|
|
} = primordials;
|
2019-03-31 13:30:12 +02:00
|
|
|
|
2023-02-28 10:37:24 +01:00
|
|
|
const binding = internalBinding('timers');
|
2017-12-18 13:43:53 +01:00
|
|
|
const {
|
2018-05-13 17:42:22 +02:00
|
|
|
immediateInfo,
|
2023-02-28 10:37:24 +01:00
|
|
|
} = binding;
|
2015-09-26 14:27:36 -07:00
|
|
|
const L = require('internal/linkedlist');
|
2018-02-11 16:35:59 -05:00
|
|
|
const {
|
|
|
|
async_id_symbol,
|
|
|
|
Timeout,
|
2020-06-18 13:22:17 -07:00
|
|
|
Immediate,
|
2019-03-11 19:08:35 +08:00
|
|
|
decRefCount,
|
|
|
|
immediateInfoFields: {
|
|
|
|
kCount,
|
2023-02-12 19:26:21 +01:00
|
|
|
kRefCount,
|
2019-03-11 19:08:35 +08:00
|
|
|
},
|
2018-05-05 19:50:21 +02:00
|
|
|
kRefed,
|
2020-06-19 21:31:21 +03:00
|
|
|
kHasPrimitive,
|
2019-03-11 19:08:35 +08:00
|
|
|
timerListMap,
|
|
|
|
timerListQueue,
|
|
|
|
immediateQueue,
|
2023-02-12 19:26:21 +01:00
|
|
|
insert,
|
2024-06-07 23:51:44 +08:00
|
|
|
knownTimersById,
|
2018-02-11 16:35:59 -05:00
|
|
|
} = require('internal/timers');
|
2019-03-11 10:19:47 +03:00
|
|
|
const {
|
|
|
|
promisify: { custom: customPromisify },
|
|
|
|
} = require('internal/util');
|
2020-03-14 07:55:44 -04:00
|
|
|
let debug = require('internal/util/debuglog').debuglog('timer', (fn) => {
|
|
|
|
debug = fn;
|
|
|
|
});
|
2022-01-24 19:39:16 +03:30
|
|
|
const { validateFunction } = require('internal/validators');
|
2019-03-06 12:54:12 +01:00
|
|
|
|
2020-06-18 13:22:17 -07:00
|
|
|
let timersPromises;
|
2023-12-23 11:23:43 +01:00
|
|
|
let timers;
|
2020-06-18 13:22:17 -07:00
|
|
|
|
2017-10-07 22:50:42 +08:00
|
|
|
const {
|
2018-02-11 16:35:59 -05:00
|
|
|
destroyHooksExist,
|
2017-10-07 22:50:42 +08:00
|
|
|
// The needed emit*() functions.
|
2023-02-12 19:26:21 +01:00
|
|
|
emitDestroy,
|
2017-11-12 18:46:55 +01:00
|
|
|
} = require('internal/async_hooks');
|
2013-08-15 19:23:36 +02:00
|
|
|
|
2016-02-26 14:19:51 -05:00
|
|
|
// Remove a timer. Cancels the timeout and resets the relevant timer properties.
|
2018-01-09 13:25:20 -05:00
|
|
|
function unenroll(item) {
|
2019-04-22 13:52:17 -07:00
|
|
|
if (item._destroyed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
item._destroyed = true;
|
|
|
|
|
2020-06-19 21:31:21 +03:00
|
|
|
if (item[kHasPrimitive])
|
|
|
|
delete knownTimersById[item[async_id_symbol]];
|
|
|
|
|
2017-03-10 06:17:42 -07:00
|
|
|
// Fewer checks may be possible, but these cover everything.
|
2019-04-22 13:52:17 -07:00
|
|
|
if (destroyHooksExist() && item[async_id_symbol] !== undefined)
|
2017-03-10 06:17:42 -07:00
|
|
|
emitDestroy(item[async_id_symbol]);
|
|
|
|
|
2018-05-05 19:50:21 +02: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-03 17:41:58 -08:00
|
|
|
// Compliment truncation during insert().
|
2019-11-22 18:04:46 +01:00
|
|
|
const msecs = MathTrunc(item._idleTimeout);
|
2019-03-11 19:08:35 +08:00
|
|
|
const list = timerListMap[msecs];
|
2018-05-05 19:50:21 +02:00
|
|
|
if (list !== undefined && L.isEmpty(list)) {
|
|
|
|
debug('unenroll: list empty');
|
2019-03-11 19:08:35 +08:00
|
|
|
timerListQueue.removeAt(list.priorityQueuePosition);
|
|
|
|
delete timerListMap[list.msecs];
|
2018-05-05 19:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
decRefCount();
|
2010-10-26 11:56:32 -07:00
|
|
|
}
|
2018-05-05 19:50:21 +02:00
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// If active is called later, then we want to make sure not to insert again
|
2011-12-22 14:42:20 +01:00
|
|
|
item._idleTimeout = -1;
|
2018-01-09 13:25:20 -05:00
|
|
|
}
|
|
|
|
|
2011-01-13 02:22:09 -08:00
|
|
|
|
2021-05-29 01:08:48 +04:30
|
|
|
/**
|
|
|
|
* Schedules the execution of a one-time `callback`
|
|
|
|
* after `after` milliseconds.
|
|
|
|
* @param {Function} callback
|
|
|
|
* @param {number} [after]
|
2025-03-24 21:30:52 +01:00
|
|
|
* @param {...any} [args]
|
2021-05-29 01:08:48 +04:30
|
|
|
* @returns {Timeout}
|
2010-10-26 12:52:31 -07:00
|
|
|
*/
|
2025-03-24 21:30:52 +01:00
|
|
|
function setTimeout(callback, after, ...args) {
|
2022-01-24 19:39:16 +03:30
|
|
|
validateFunction(callback, 'callback');
|
2025-03-24 21:30:52 +01:00
|
|
|
const timeout = new Timeout(callback, after, args.length ? args : undefined, false, true);
|
2019-04-22 10:30:39 -07:00
|
|
|
insert(timeout, timeout._idleTimeout);
|
2017-02-03 16:05:59 -05:00
|
|
|
return timeout;
|
2017-04-14 21:42:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-27 17:39:01 +02:00
|
|
|
ObjectDefineProperty(setTimeout, customPromisify, {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-06-18 13:22:17 -07:00
|
|
|
enumerable: true,
|
|
|
|
get() {
|
2024-10-09 02:42:16 -04:00
|
|
|
timersPromises ??= require('timers/promises');
|
2020-06-18 13:22:17 -07:00
|
|
|
return timersPromises.setTimeout;
|
2023-02-12 19:26:21 +01:00
|
|
|
},
|
2020-06-18 13:22:17 -07:00
|
|
|
});
|
2016-09-20 02:10:50 -04:00
|
|
|
|
2021-05-29 01:08:48 +04:30
|
|
|
/**
|
|
|
|
* Cancels a timeout.
|
|
|
|
* @param {Timeout | string | number} timer
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-03-11 10:17:47 +03:00
|
|
|
function clearTimeout(timer) {
|
2024-09-24 15:48:15 -04:00
|
|
|
if (timer?._onTimeout) {
|
2018-02-05 13:09:08 -05:00
|
|
|
timer._onTimeout = null;
|
2018-05-05 19:50:21 +02:00
|
|
|
unenroll(timer);
|
2020-06-19 21:31:21 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof timer === 'number' || typeof timer === 'string') {
|
|
|
|
const timerInstance = knownTimersById[timer];
|
|
|
|
if (timerInstance !== undefined) {
|
|
|
|
timerInstance._onTimeout = null;
|
|
|
|
unenroll(timerInstance);
|
|
|
|
}
|
2010-10-29 00:00:43 -07:00
|
|
|
}
|
2019-03-11 10:17:47 +03:00
|
|
|
}
|
2010-10-26 12:52:31 -07:00
|
|
|
|
2021-05-29 01:08:48 +04:30
|
|
|
/**
|
|
|
|
* Schedules repeated execution of `callback`
|
|
|
|
* every `repeat` milliseconds.
|
|
|
|
* @param {Function} callback
|
|
|
|
* @param {number} [repeat]
|
2025-03-24 21:30:52 +01:00
|
|
|
* @param {...any} [args]
|
2021-05-29 01:08:48 +04:30
|
|
|
* @returns {Timeout}
|
|
|
|
*/
|
2025-03-24 21:30:52 +01:00
|
|
|
function setInterval(callback, repeat, ...args) {
|
2022-01-24 19:39:16 +03:30
|
|
|
validateFunction(callback, 'callback');
|
2025-03-24 21:30:52 +01:00
|
|
|
const timeout = new Timeout(callback, repeat, args.length ? args : undefined, true, true);
|
2019-04-22 10:30:39 -07:00
|
|
|
insert(timeout, timeout._idleTimeout);
|
2017-02-03 16:05:59 -05:00
|
|
|
return timeout;
|
2019-03-11 10:17:47 +03:00
|
|
|
}
|
2016-09-20 02:10:50 -04:00
|
|
|
|
2021-05-29 01:08:48 +04:30
|
|
|
/**
|
|
|
|
* Cancels an interval.
|
|
|
|
* @param {Timeout | string | number} timer
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-03-11 10:17:47 +03:00
|
|
|
function clearInterval(timer) {
|
2018-04-11 17:49:07 +02: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 10:17:47 +03:00
|
|
|
}
|
2012-07-12 22:19:01 -04:00
|
|
|
|
|
|
|
Timeout.prototype.close = function() {
|
2018-05-05 19:50:21 +02:00
|
|
|
clearTimeout(this);
|
2015-09-13 16:21:51 +01:00
|
|
|
return this;
|
2012-07-12 22:19:01 -04:00
|
|
|
};
|
2012-08-07 22:12:01 -04:00
|
|
|
|
2023-07-05 16:39:38 +03:00
|
|
|
Timeout.prototype[SymbolDispose] = function() {
|
|
|
|
clearTimeout(this);
|
|
|
|
};
|
|
|
|
|
2021-05-29 01:08:48 +04:30
|
|
|
/**
|
|
|
|
* Coerces a `Timeout` to a primitive.
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2020-06-19 21:31:21 +03:00
|
|
|
Timeout.prototype[SymbolToPrimitive] = function() {
|
|
|
|
const id = this[async_id_symbol];
|
|
|
|
if (!this[kHasPrimitive]) {
|
|
|
|
this[kHasPrimitive] = true;
|
|
|
|
knownTimersById[id] = this;
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
2021-05-29 01:08:48 +04:30
|
|
|
/**
|
|
|
|
* Schedules the immediate execution of `callback`
|
|
|
|
* after I/O events' callbacks.
|
|
|
|
* @param {Function} callback
|
2025-03-24 21:30:52 +01:00
|
|
|
* @param {...any} [args]
|
2021-05-29 01:08:48 +04:30
|
|
|
* @returns {Immediate}
|
|
|
|
*/
|
2025-03-24 21:30:52 +01:00
|
|
|
function setImmediate(callback, ...args) {
|
2022-01-24 19:39:16 +03:30
|
|
|
validateFunction(callback, 'callback');
|
2025-03-24 21:30:52 +01:00
|
|
|
return new Immediate(callback, args.length ? args : undefined);
|
2017-04-14 21:42:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-27 17:39:01 +02:00
|
|
|
ObjectDefineProperty(setImmediate, customPromisify, {
|
2022-06-03 10:23:58 +02:00
|
|
|
__proto__: null,
|
2020-06-18 13:22:17 -07:00
|
|
|
enumerable: true,
|
|
|
|
get() {
|
2024-10-09 02:42:16 -04:00
|
|
|
timersPromises ??= require('timers/promises');
|
2020-06-18 13:22:17 -07:00
|
|
|
return timersPromises.setImmediate;
|
2023-02-12 19:26:21 +01:00
|
|
|
},
|
2020-06-18 13:22:17 -07:00
|
|
|
});
|
|
|
|
|
2021-05-29 01:08:48 +04:30
|
|
|
/**
|
|
|
|
* Cancels an immediate.
|
|
|
|
* @param {Immediate} immediate
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-03-11 10:17:47 +03:00
|
|
|
function clearImmediate(immediate) {
|
2025-03-02 00:54:52 +01:00
|
|
|
if (!immediate?._onImmediate || immediate._destroyed)
|
2018-01-13 16:51:28 -05:00
|
|
|
return;
|
2012-08-07 22:12:01 -04:00
|
|
|
|
2018-01-13 16:51:28 -05:00
|
|
|
immediateInfo[kCount]--;
|
|
|
|
immediate._destroyed = true;
|
2017-11-16 00:43:12 +01:00
|
|
|
|
2023-02-28 10:37:24 +01:00
|
|
|
if (immediate[kRefed] && --immediateInfo[kRefCount] === 0) {
|
|
|
|
// We need to use the binding as the receiver for fast API calls.
|
|
|
|
binding.toggleImmediateRef(false);
|
|
|
|
}
|
2018-05-23 02:01:53 +04:00
|
|
|
immediate[kRefed] = null;
|
2018-01-13 16:51:28 -05:00
|
|
|
|
2021-03-20 01:50:31 +02:00
|
|
|
if (destroyHooksExist() && immediate[async_id_symbol] !== undefined) {
|
2018-01-13 16:51:28 -05:00
|
|
|
emitDestroy(immediate[async_id_symbol]);
|
2017-03-10 06:17:42 -07:00
|
|
|
}
|
|
|
|
|
2016-09-17 21:28:20 -04:00
|
|
|
immediate._onImmediate = null;
|
2012-08-07 22:12:01 -04:00
|
|
|
|
2016-09-17 21:28:20 -04:00
|
|
|
immediateQueue.remove(immediate);
|
2019-03-11 10:17:47 +03:00
|
|
|
}
|
|
|
|
|
2023-07-05 16:39:38 +03:00
|
|
|
Immediate.prototype[SymbolDispose] = function() {
|
|
|
|
clearImmediate(this);
|
|
|
|
};
|
|
|
|
|
2023-12-23 11:23:43 +01:00
|
|
|
module.exports = timers = {
|
2019-03-11 10:17:47 +03:00
|
|
|
setTimeout,
|
|
|
|
clearTimeout,
|
|
|
|
setImmediate,
|
|
|
|
clearImmediate,
|
|
|
|
setInterval,
|
|
|
|
clearInterval,
|
2012-08-07 22:12:01 -04:00
|
|
|
};
|
2023-12-23 11:23:43 +01:00
|
|
|
|
|
|
|
ObjectDefineProperties(timers, {
|
|
|
|
promises: {
|
|
|
|
__proto__: null,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
timersPromises ??= require('timers/promises');
|
|
|
|
return timersPromises;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|