timers: refactor to use module.exports
PR-URL: https://github.com/nodejs/node/pull/26583 Refs: https://github.com/nodejs/node/issues/26546 Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
84156cf10e
commit
4980cc7d9b
@ -185,16 +185,15 @@ function decRefCount() {
|
|||||||
|
|
||||||
// Schedule or re-schedule a timer.
|
// Schedule or re-schedule a timer.
|
||||||
// The item must have been enroll()'d first.
|
// The item must have been enroll()'d first.
|
||||||
const active = exports.active = function(item) {
|
function active(item) {
|
||||||
insert(item, true, getLibuvNow());
|
insert(item, true, getLibuvNow());
|
||||||
};
|
}
|
||||||
|
|
||||||
// Internal APIs that need timeouts should use `_unrefActive()` instead of
|
// Internal APIs that need timeouts should use `_unrefActive()` instead of
|
||||||
// `active()` so that they do not unnecessarily keep the process open.
|
// `active()` so that they do not unnecessarily keep the process open.
|
||||||
exports._unrefActive = function(item) {
|
function _unrefActive(item) {
|
||||||
insert(item, false, getLibuvNow());
|
insert(item, false, getLibuvNow());
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
// The underlying logic for scheduling or re-scheduling a timer.
|
// The underlying logic for scheduling or re-scheduling a timer.
|
||||||
//
|
//
|
||||||
@ -406,12 +405,6 @@ function unenroll(item) {
|
|||||||
item._idleTimeout = -1;
|
item._idleTimeout = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.unenroll = util.deprecate(unenroll,
|
|
||||||
'timers.unenroll() is deprecated. ' +
|
|
||||||
'Please use clearTimeout instead.',
|
|
||||||
'DEP0096');
|
|
||||||
|
|
||||||
|
|
||||||
// Make a regular object able to act as a timer by setting some properties.
|
// Make a regular object able to act as a timer by setting some properties.
|
||||||
// This function does not start the timer, see `active()`.
|
// This function does not start the timer, see `active()`.
|
||||||
// Using existing objects as timers slightly reduces object overhead.
|
// Using existing objects as timers slightly reduces object overhead.
|
||||||
@ -426,11 +419,6 @@ function enroll(item, msecs) {
|
|||||||
item._idleTimeout = msecs;
|
item._idleTimeout = msecs;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.enroll = util.deprecate(enroll,
|
|
||||||
'timers.enroll() is deprecated. ' +
|
|
||||||
'Please use setTimeout instead.',
|
|
||||||
'DEP0095');
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* DOM-style timers
|
* DOM-style timers
|
||||||
@ -476,18 +464,14 @@ setTimeout[internalUtil.promisify.custom] = function(after, value) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.setTimeout = setTimeout;
|
function clearTimeout(timer) {
|
||||||
|
|
||||||
|
|
||||||
const clearTimeout = exports.clearTimeout = function clearTimeout(timer) {
|
|
||||||
if (timer && timer._onTimeout) {
|
if (timer && timer._onTimeout) {
|
||||||
timer._onTimeout = null;
|
timer._onTimeout = null;
|
||||||
unenroll(timer);
|
unenroll(timer);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
function setInterval(callback, repeat, arg1, arg2, arg3) {
|
||||||
exports.setInterval = function setInterval(callback, repeat, arg1, arg2, arg3) {
|
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
throw new ERR_INVALID_CALLBACK();
|
throw new ERR_INVALID_CALLBACK();
|
||||||
}
|
}
|
||||||
@ -517,14 +501,14 @@ exports.setInterval = function setInterval(callback, repeat, arg1, arg2, arg3) {
|
|||||||
active(timeout);
|
active(timeout);
|
||||||
|
|
||||||
return timeout;
|
return timeout;
|
||||||
};
|
}
|
||||||
|
|
||||||
exports.clearInterval = function clearInterval(timer) {
|
function clearInterval(timer) {
|
||||||
// clearTimeout and clearInterval can be used to clear timers created from
|
// clearTimeout and clearInterval can be used to clear timers created from
|
||||||
// both setTimeout and setInterval, as specified by HTML Living Standard:
|
// both setTimeout and setInterval, as specified by HTML Living Standard:
|
||||||
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
|
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
Timeout.prototype.unref = function() {
|
Timeout.prototype.unref = function() {
|
||||||
@ -739,10 +723,7 @@ setImmediate[internalUtil.promisify.custom] = function(value) {
|
|||||||
return new Promise((resolve) => new Immediate(resolve, [value]));
|
return new Promise((resolve) => new Immediate(resolve, [value]));
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.setImmediate = setImmediate;
|
function clearImmediate(immediate) {
|
||||||
|
|
||||||
|
|
||||||
exports.clearImmediate = function clearImmediate(immediate) {
|
|
||||||
if (!immediate || immediate._destroyed)
|
if (!immediate || immediate._destroyed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -760,4 +741,23 @@ exports.clearImmediate = function clearImmediate(immediate) {
|
|||||||
immediate._onImmediate = null;
|
immediate._onImmediate = null;
|
||||||
|
|
||||||
immediateQueue.remove(immediate);
|
immediateQueue.remove(immediate);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
_unrefActive,
|
||||||
|
active,
|
||||||
|
setTimeout,
|
||||||
|
clearTimeout,
|
||||||
|
setImmediate,
|
||||||
|
clearImmediate,
|
||||||
|
setInterval,
|
||||||
|
clearInterval,
|
||||||
|
unenroll: util.deprecate(
|
||||||
|
unenroll,
|
||||||
|
'timers.unenroll() is deprecated. Please use clearTimeout instead.',
|
||||||
|
'DEP0096'),
|
||||||
|
enroll: util.deprecate(
|
||||||
|
enroll,
|
||||||
|
'timers.enroll() is deprecated. Please use setTimeout instead.',
|
||||||
|
'DEP0095')
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user