Skip to content

Commit 68783ae

Browse files
committed
timers: runtime-deprecate {un}enroll()
This was never a Very Good API, and generally just left so many open ends for inconsistent behavior. The "optimization" benefit of this API is little to none. Makes a starting step towards removing it so that in the future timers, especially in their async_hooks interactions, can be simplified. PR-URL: #18066 Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 42258d7 commit 68783ae

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

doc/api/deprecations.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,11 +856,27 @@ Using `assert.fail()` with more than one argument has no benefit over writing an
856856
individual error message. Either use `assert.fail()` with one argument or switch
857857
to one of the other assert methods.
858858
859+
<a id="DEP00XX"></a>
860+
### DEP00XX: timers.enroll()
861+
862+
Type: Runtime
863+
864+
`timers.enroll()` is deprecated. Please use the publicly documented [`setTimeout()`][] or [`setInterval()`][] instead.
865+
866+
<a id="DEP00XX"></a>
867+
### DEP00XX: timers.unenroll()
868+
869+
Type: Runtime
870+
871+
`timers.unenroll()` is deprecated. Please use the publicly documented [`clearTimeout()`][] or [`clearInterval()`][] instead.
872+
859873
[`--pending-deprecation`]: cli.html#cli_pending_deprecation
860874
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
861875
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
862876
[`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer
863877
[`Buffer.isBuffer()`]: buffer.html#buffer_class_method_buffer_isbuffer_obj
878+
[`clearInterval()`]: timers.html#timers_clearinterval_timeout
879+
[`clearTimeout()`]: timers.html#timers_cleartimeout_timeout
864880
[`EventEmitter.listenerCount(emitter, eventName)`]: events.html#events_eventemitter_listenercount_emitter_eventname
865881
[`Server.connections`]: net.html#net_server_connections
866882
[`Server.getConnections()`]: net.html#net_server_getconnections_callback
@@ -890,6 +906,8 @@ to one of the other assert methods.
890906
[`os.tmpdir()`]: os.html#os_os_tmpdir
891907
[`punycode`]: punycode.html
892908
[`require.extensions`]: modules.html#modules_require_extensions
909+
[`setInterval()`]: timers.html#timers_setinterval_callback_delay_args
910+
[`setTimeout()`]: timers.html#timers_settimeout_callback_delay_args
893911
[`tls.CryptoStream`]: tls.html#tls_class_cryptostream
894912
[`tls.SecureContext`]: tls.html#tls_tls_createsecurecontext_options
895913
[`tls.SecurePair`]: tls.html#tls_class_securepair

lib/timers.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ function reuse(item) {
367367

368368

369369
// Remove a timer. Cancels the timeout and resets the relevant timer properties.
370-
const unenroll = exports.unenroll = function(item) {
370+
function unenroll(item) {
371371
// Fewer checks may be possible, but these cover everything.
372372
if (async_hook_fields[kDestroy] > 0 &&
373373
item &&
@@ -384,21 +384,31 @@ const unenroll = exports.unenroll = function(item) {
384384
}
385385
// if active is called later, then we want to make sure not to insert again
386386
item._idleTimeout = -1;
387-
};
387+
}
388+
389+
exports.unenroll = util.deprecate(unenroll,
390+
'timers.unenroll() is deprecated. ' +
391+
'Please use clearTimeout instead.',
392+
'DEP00XX');
388393

389394

390395
// Make a regular object able to act as a timer by setting some properties.
391396
// This function does not start the timer, see `active()`.
392397
// Using existing objects as timers slightly reduces object overhead.
393-
exports.enroll = function(item, msecs) {
398+
function enroll(item, msecs) {
394399
item._idleTimeout = timerInternals.validateTimerDuration(msecs);
395400

396401
// if this item was already in a list somewhere
397402
// then we should unenroll it from that
398403
if (item._idleNext) unenroll(item);
399404

400405
L.init(item);
401-
};
406+
}
407+
408+
exports.enroll = util.deprecate(enroll,
409+
'timers.unenroll() is deprecated. ' +
410+
'Please use clearTimeout instead.',
411+
'DEP00XX');
402412

403413

404414
/*

test/parallel/test-timers-max-duration-warning.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ function timerNotCanceled() {
1111
}
1212

1313
process.on('warning', common.mustCall((warning) => {
14+
if (warning.name === 'DeprecationWarning') return;
15+
1416
const lines = warning.message.split('\n');
1517

1618
assert.strictEqual(warning.name, 'TimeoutOverflowWarning');
1719
assert.strictEqual(lines[0], `${OVERFLOW} does not fit into a 32-bit signed` +
1820
' integer.');
1921
assert.strictEqual(lines.length, 2);
20-
}, 3));
22+
}, 4));
2123

2224

2325
{

0 commit comments

Comments
 (0)