2017-01-03 22:16:48 +01: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-03-31 13:30:12 +02:00
|
|
|
const { Math } = primordials;
|
|
|
|
|
2017-12-18 13:43:53 +01:00
|
|
|
const {
|
2018-05-13 17:42:22 +02:00
|
|
|
immediateInfo,
|
|
|
|
toggleImmediateRef
|
|
|
|
} = internalBinding('timers');
|
2015-09-26 23:27:36 +02:00
|
|
|
const L = require('internal/linkedlist');
|
2018-02-11 22:35:59 +01:00
|
|
|
const {
|
|
|
|
async_id_symbol,
|
|
|
|
Timeout,
|
2019-03-11 12:08:35 +01:00
|
|
|
decRefCount,
|
|
|
|
immediateInfoFields: {
|
|
|
|
kCount,
|
|
|
|
kRefCount
|
|
|
|
},
|
2018-05-05 19:50:21 +02:00
|
|
|
kRefed,
|
2018-02-12 01:20:42 +01:00
|
|
|
initAsyncResource,
|
2019-03-20 12:12:48 +01:00
|
|
|
getTimerDuration,
|
2019-03-11 12:08:35 +01:00
|
|
|
timerListMap,
|
|
|
|
timerListQueue,
|
|
|
|
immediateQueue,
|
|
|
|
active,
|
|
|
|
unrefActive
|
2018-02-11 22:35:59 +01:00
|
|
|
} = require('internal/timers');
|
2019-03-11 08:19:47 +01:00
|
|
|
const {
|
|
|
|
promisify: { custom: customPromisify },
|
|
|
|
deprecate
|
|
|
|
} = require('internal/util');
|
2018-02-27 14:55:32 +01:00
|
|
|
const { ERR_INVALID_CALLBACK } = require('internal/errors').codes;
|
2019-04-17 17:45:53 +02:00
|
|
|
const debug = require('internal/util/debuglog').debuglog('timer');
|
2019-03-06 12:54:12 +01:00
|
|
|
|
2017-10-07 16:50:42 +02:00
|
|
|
const {
|
2018-02-11 22:35:59 +01:00
|
|
|
destroyHooksExist,
|
2017-10-07 16:50:42 +02:00
|
|
|
// The needed emit*() functions.
|
|
|
|
emitDestroy
|
2017-11-12 18:46:55 +01:00
|
|
|
} = require('internal/async_hooks');
|
2013-08-15 19:23:36 +02:00
|
|
|
|
2016-02-26 20:19:51 +01:00
|
|
|
// Remove a timer. Cancels the timeout and resets the relevant timer properties.
|
2018-01-09 19:25:20 +01:00
|
|
|
function unenroll(item) {
|
2017-03-10 14:17:42 +01:00
|
|
|
// Fewer checks may be possible, but these cover everything.
|
2018-02-11 22:35:59 +01:00
|
|
|
if (destroyHooksExist() &&
|
2018-05-05 19:50:21 +02:00
|
|
|
item[async_id_symbol] !== undefined &&
|
2017-03-10 14:17:42 +01:00
|
|
|
!item._destroyed) {
|
|
|
|
emitDestroy(item[async_id_symbol]);
|
|
|
|
}
|
2019-09-18 01:20:09 +02:00
|
|
|
item._destroyed = true;
|
2017-03-10 14:17:42 +01:00
|
|
|
|
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-04 02:41:58 +01:00
|
|
|
// Compliment truncation during insert().
|
|
|
|
const msecs = Math.trunc(item._idleTimeout);
|
2019-03-11 12:08:35 +01: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 12:08:35 +01:00
|
|
|
timerListQueue.removeAt(list.priorityQueuePosition);
|
|
|
|
delete timerListMap[list.msecs];
|
2018-05-05 19:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
decRefCount();
|
2010-10-26 20:56:32 +02:00
|
|
|
}
|
2018-05-05 19:50:21 +02:00
|
|
|
item[kRefed] = null;
|
|
|
|
|
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 19:25:20 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 20:19:51 +01: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 19:25:20 +01:00
|
|
|
function enroll(item, msecs) {
|
2019-03-20 12:12:48 +01:00
|
|
|
msecs = getTimerDuration(msecs, 'msecs');
|
2014-12-16 23:17:28 +01:00
|
|
|
|
2019-01-21 01:22:27 +01:00
|
|
|
// If this item was already in a list somewhere
|
2010-10-26 20:56:32 +02:00
|
|
|
// then we should unenroll it from that
|
2010-10-26 21:14:17 +02:00
|
|
|
if (item._idleNext) unenroll(item);
|
2010-10-26 20:56:32 +02:00
|
|
|
|
2011-01-18 23:26:32 +01:00
|
|
|
L.init(item);
|
2018-04-11 10:43:21 +02:00
|
|
|
item._idleTimeout = msecs;
|
2018-01-09 19:25:20 +01:00
|
|
|
}
|
|
|
|
|
2011-01-13 11:22:09 +01:00
|
|
|
|
2010-10-26 21:52:31 +02:00
|
|
|
/*
|
|
|
|
* DOM-style timers
|
|
|
|
*/
|
2010-10-26 21:14:17 +02:00
|
|
|
|
|
|
|
|
2017-04-14 21:42:47 +02:00
|
|
|
function setTimeout(callback, after, arg1, arg2, arg3) {
|
2015-12-20 12:30:04 +01:00
|
|
|
if (typeof callback !== 'function') {
|
2019-04-02 03:46:17 +02:00
|
|
|
throw new ERR_INVALID_CALLBACK(callback);
|
2015-12-20 12:30:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-25 14:07:26 +01: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 17:15:45 +01:00
|
|
|
// Extend array dynamically, makes .apply run much faster in v6.0.0
|
2017-11-25 14:07:26 +01:00
|
|
|
args[i - 2] = arguments[i];
|
|
|
|
}
|
|
|
|
break;
|
2011-07-12 00:30:24 +02:00
|
|
|
}
|
2011-06-08 04:35:50 +02:00
|
|
|
|
2018-05-05 19:50:21 +02:00
|
|
|
const timeout = new Timeout(callback, after, args, false);
|
2017-02-03 22:05:59 +01:00
|
|
|
active(timeout);
|
|
|
|
|
|
|
|
return timeout;
|
2017-04-14 21:42:47 +02:00
|
|
|
}
|
|
|
|
|
2019-03-11 08:19:47 +01:00
|
|
|
setTimeout[customPromisify] = function(after, value) {
|
2019-03-10 01:26:17 +01:00
|
|
|
const args = value !== undefined ? [value] : value;
|
2018-05-19 00:25:07 +02:00
|
|
|
return new Promise((resolve) => {
|
2019-03-10 01:26:17 +01:00
|
|
|
active(new Timeout(resolve, after, args, false));
|
2018-05-19 00:25:07 +02:00
|
|
|
});
|
2016-09-20 08:10:50 +02:00
|
|
|
};
|
|
|
|
|
2019-03-11 08:17:47 +01:00
|
|
|
function clearTimeout(timer) {
|
2018-02-05 19:09:08 +01:00
|
|
|
if (timer && timer._onTimeout) {
|
|
|
|
timer._onTimeout = null;
|
2018-05-05 19:50:21 +02:00
|
|
|
unenroll(timer);
|
2010-10-29 09:00:43 +02:00
|
|
|
}
|
2019-03-11 08:17:47 +01:00
|
|
|
}
|
2010-10-26 21:52:31 +02:00
|
|
|
|
2019-03-11 08:17:47 +01:00
|
|
|
function setInterval(callback, repeat, arg1, arg2, arg3) {
|
2015-12-20 12:30:04 +01:00
|
|
|
if (typeof callback !== 'function') {
|
2019-04-02 03:46:17 +02:00
|
|
|
throw new ERR_INVALID_CALLBACK(callback);
|
2015-12-20 12:30:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-25 14:07:26 +01: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 17:15:45 +01:00
|
|
|
// Extend array dynamically, makes .apply run much faster in v6.0.0
|
2017-11-25 14:07:26 +01:00
|
|
|
args[i - 2] = arguments[i];
|
|
|
|
}
|
|
|
|
break;
|
2016-09-20 08:10:50 +02:00
|
|
|
}
|
2012-07-01 22:58:22 +02:00
|
|
|
|
2018-05-05 19:50:21 +02:00
|
|
|
const timeout = new Timeout(callback, repeat, args, true);
|
2017-02-03 22:05:59 +01:00
|
|
|
active(timeout);
|
|
|
|
|
|
|
|
return timeout;
|
2019-03-11 08:17:47 +01:00
|
|
|
}
|
2016-09-20 08:10:50 +02:00
|
|
|
|
2019-03-11 08:17:47 +01: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 08:17:47 +01:00
|
|
|
}
|
2012-07-13 04:19:01 +02:00
|
|
|
|
|
|
|
Timeout.prototype.close = function() {
|
2018-05-05 19:50:21 +02:00
|
|
|
clearTimeout(this);
|
2015-09-13 17:21:51 +02:00
|
|
|
return this;
|
2012-07-13 04:19:01 +02:00
|
|
|
};
|
2012-08-08 04:12:01 +02:00
|
|
|
|
2018-01-13 22:51:28 +01:00
|
|
|
const Immediate = class Immediate {
|
|
|
|
constructor(callback, args) {
|
|
|
|
this._idleNext = null;
|
|
|
|
this._idlePrev = null;
|
|
|
|
this._onImmediate = callback;
|
|
|
|
this._argv = args;
|
|
|
|
this._destroyed = false;
|
|
|
|
this[kRefed] = false;
|
|
|
|
|
2018-02-12 01:20:42 +01:00
|
|
|
initAsyncResource(this, 'Immediate');
|
2018-01-13 22:51:28 +01:00
|
|
|
|
|
|
|
this.ref();
|
|
|
|
immediateInfo[kCount]++;
|
2017-11-25 14:07:26 +01:00
|
|
|
|
2018-01-13 22:51:28 +01:00
|
|
|
immediateQueue.append(this);
|
2017-11-25 14:07:26 +01:00
|
|
|
}
|
|
|
|
|
2018-01-13 22:51:28 +01:00
|
|
|
ref() {
|
|
|
|
if (this[kRefed] === false) {
|
|
|
|
this[kRefed] = true;
|
|
|
|
if (immediateInfo[kRefCount]++ === 0)
|
|
|
|
toggleImmediateRef(true);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2017-11-25 14:07:26 +01:00
|
|
|
|
2018-01-13 22:51:28 +01:00
|
|
|
unref() {
|
|
|
|
if (this[kRefed] === true) {
|
|
|
|
this[kRefed] = false;
|
|
|
|
if (--immediateInfo[kRefCount] === 0)
|
|
|
|
toggleImmediateRef(false);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2018-05-23 00:01:53 +02:00
|
|
|
|
|
|
|
hasRef() {
|
|
|
|
return !!this[kRefed];
|
|
|
|
}
|
2018-01-13 22:51:28 +01:00
|
|
|
};
|
2013-10-07 21:39:52 +02:00
|
|
|
|
2017-04-14 21:42:47 +02:00
|
|
|
function setImmediate(callback, arg1, arg2, arg3) {
|
2015-12-20 12:30:04 +01:00
|
|
|
if (typeof callback !== 'function') {
|
2019-04-02 03:46:17 +02:00
|
|
|
throw new ERR_INVALID_CALLBACK(callback);
|
2015-12-20 12:30:04 +01:00
|
|
|
}
|
|
|
|
|
2015-01-10 16:49:21 +01:00
|
|
|
var i, args;
|
2016-04-28 02:31:59 +02:00
|
|
|
switch (arguments.length) {
|
2015-01-10 16:49:21 +01:00
|
|
|
// fast cases
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
case 2:
|
2016-04-28 02:31:59 +02:00
|
|
|
args = [arg1];
|
2015-01-10 16:49:21 +01:00
|
|
|
break;
|
|
|
|
case 3:
|
2016-04-28 02:31:59 +02:00
|
|
|
args = [arg1, arg2];
|
2015-01-10 16:49:21 +01:00
|
|
|
break;
|
|
|
|
default:
|
2016-04-28 02:31:59 +02:00
|
|
|
args = [arg1, arg2, arg3];
|
2017-11-25 14:07:26 +01:00
|
|
|
for (i = 4; i < arguments.length; i++) {
|
2018-12-03 17:15:45 +01:00
|
|
|
// Extend array dynamically, makes .apply run much faster in v6.0.0
|
2015-01-10 16:49:21 +01:00
|
|
|
args[i - 1] = arguments[i];
|
2017-11-25 14:07:26 +01:00
|
|
|
}
|
2015-01-10 16:49:21 +01:00
|
|
|
break;
|
2012-08-08 04:12:01 +02:00
|
|
|
}
|
2017-11-25 14:07:26 +01:00
|
|
|
|
|
|
|
return new Immediate(callback, args);
|
2017-04-14 21:42:47 +02:00
|
|
|
}
|
|
|
|
|
2019-03-11 08:19:47 +01:00
|
|
|
setImmediate[customPromisify] = function(value) {
|
2018-05-19 00:25:07 +02:00
|
|
|
return new Promise((resolve) => new Immediate(resolve, [value]));
|
2016-09-18 03:28:20 +02:00
|
|
|
};
|
|
|
|
|
2019-03-11 08:17:47 +01:00
|
|
|
function clearImmediate(immediate) {
|
2018-01-13 22:51:28 +01:00
|
|
|
if (!immediate || immediate._destroyed)
|
|
|
|
return;
|
2012-08-08 04:12:01 +02:00
|
|
|
|
2018-01-13 22:51:28 +01:00
|
|
|
immediateInfo[kCount]--;
|
|
|
|
immediate._destroyed = true;
|
2017-11-16 00:43:12 +01:00
|
|
|
|
2018-01-13 22:51:28 +01:00
|
|
|
if (immediate[kRefed] && --immediateInfo[kRefCount] === 0)
|
|
|
|
toggleImmediateRef(false);
|
2018-05-23 00:01:53 +02:00
|
|
|
immediate[kRefed] = null;
|
2018-01-13 22:51:28 +01:00
|
|
|
|
2018-02-11 22:35:59 +01:00
|
|
|
if (destroyHooksExist()) {
|
2018-01-13 22:51:28 +01:00
|
|
|
emitDestroy(immediate[async_id_symbol]);
|
2017-03-10 14:17:42 +01:00
|
|
|
}
|
|
|
|
|
2016-09-18 03:28:20 +02:00
|
|
|
immediate._onImmediate = null;
|
2012-08-08 04:12:01 +02:00
|
|
|
|
2016-09-18 03:28:20 +02:00
|
|
|
immediateQueue.remove(immediate);
|
2019-03-11 08:17:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
setTimeout,
|
|
|
|
clearTimeout,
|
|
|
|
setImmediate,
|
|
|
|
clearImmediate,
|
|
|
|
setInterval,
|
|
|
|
clearInterval,
|
2019-03-18 23:56:49 +01: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 08:19:47 +01:00
|
|
|
unenroll: deprecate(
|
2019-03-11 08:17:47 +01:00
|
|
|
unenroll,
|
|
|
|
'timers.unenroll() is deprecated. Please use clearTimeout instead.',
|
|
|
|
'DEP0096'),
|
2019-03-11 08:19:47 +01:00
|
|
|
enroll: deprecate(
|
2019-03-11 08:17:47 +01:00
|
|
|
enroll,
|
|
|
|
'timers.enroll() is deprecated. Please use setTimeout instead.',
|
|
|
|
'DEP0095')
|
2012-08-08 04:12:01 +02:00
|
|
|
};
|