diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index ba0676f1b84da5..1cebecf5884436 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -2065,28 +2065,34 @@ method. -Type: Runtime +Type: End-of-Life -`timers.enroll()` is deprecated. Please use the publicly documented +`timers.enroll()` has been removed. Please use the publicly documented [`setTimeout()`][] or [`setInterval()`][] instead. ### DEP0096: `timers.unenroll()` -Type: Runtime +Type: End-of-Life -`timers.unenroll()` is deprecated. Please use the publicly documented +`timers.unenroll()` has been removed. Please use the publicly documented [`clearTimeout()`][] or [`clearInterval()`][] instead. ### DEP0097: `MakeCallback` with `domain` property @@ -2613,14 +2619,17 @@ The `node:_stream_wrap` module is deprecated. -Type: Runtime +Type: End-of-Life -The previously undocumented `timers.active()` is deprecated. +The previously undocumented `timers.active()` has been removed. Please use the publicly documented [`timeout.refresh()`][] instead. If re-referencing the timeout is necessary, [`timeout.ref()`][] can be used with no performance impact since Node.js 10. @@ -2629,14 +2638,17 @@ with no performance impact since Node.js 10. -Type: Runtime +Type: End-of-Life -The previously undocumented and "private" `timers._unrefActive()` is deprecated. +The previously undocumented and "private" `timers._unrefActive()` has been removed. Please use the publicly documented [`timeout.refresh()`][] instead. If unreferencing the timeout is necessary, [`timeout.unref()`][] can be used with no performance impact since Node.js 10. diff --git a/lib/timers.js b/lib/timers.js index 1f801c734d575e..8884137a808174 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -46,18 +46,14 @@ const { }, kRefed, kHasPrimitive, - getTimerDuration, timerListMap, timerListQueue, immediateQueue, - active, - unrefActive, insert, knownTimersById, } = require('internal/timers'); const { promisify: { custom: customPromisify }, - deprecate, } = require('internal/util'); let debug = require('internal/util/debuglog').debuglog('timer', (fn) => { debug = fn; @@ -111,20 +107,6 @@ function unenroll(item) { item._idleTimeout = -1; } -// 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. -function enroll(item, msecs) { - msecs = getTimerDuration(msecs, 'msecs'); - - // If this item was already in a list somewhere - // then we should unenroll it from that - if (item._idleNext) unenroll(item); - - L.init(item); - item._idleTimeout = msecs; -} - /** * Schedules the execution of a one-time `callback` @@ -351,23 +333,6 @@ module.exports = timers = { clearImmediate, setInterval, clearInterval, - _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'), - unenroll: deprecate( - unenroll, - 'timers.unenroll() is deprecated. Please use clearTimeout instead.', - 'DEP0096'), - enroll: deprecate( - enroll, - 'timers.enroll() is deprecated. Please use setTimeout instead.', - 'DEP0095'), }; ObjectDefineProperties(timers, { diff --git a/test/parallel/test-timers-active.js b/test/parallel/test-timers-active.js deleted file mode 100644 index ac79c6fc1e1ca0..00000000000000 --- a/test/parallel/test-timers-active.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict'; -require('../common'); -const assert = require('assert'); -const active = require('timers').active; - -// active() should create timers for these -const legitTimers = [ - { _idleTimeout: 0 }, - { _idleTimeout: 1 }, -]; - -legitTimers.forEach(function(legit) { - const savedTimeout = legit._idleTimeout; - active(legit); - // active() should mutate these objects - assert.strictEqual(legit._idleTimeout, savedTimeout); - assert(Number.isInteger(legit._idleStart)); - assert(legit._idleNext); - assert(legit._idlePrev); -}); - - -// active() should not create a timer for these -const bogusTimers = [ - { _idleTimeout: -1 }, - { _idleTimeout: undefined }, -]; - -bogusTimers.forEach(function(bogus) { - const savedTimeout = bogus._idleTimeout; - active(bogus); - // active() should not mutate these objects - assert.deepStrictEqual(bogus, { _idleTimeout: savedTimeout }); -}); diff --git a/test/parallel/test-timers-enroll-invalid-msecs.js b/test/parallel/test-timers-enroll-invalid-msecs.js deleted file mode 100644 index 466f1a2c4a8872..00000000000000 --- a/test/parallel/test-timers-enroll-invalid-msecs.js +++ /dev/null @@ -1,38 +0,0 @@ -'use strict'; - -require('../common'); -const assert = require('assert'); -const timers = require('timers'); - -[ - {}, - [], - 'foo', - () => { }, - Symbol('foo'), -].forEach((val) => { - assert.throws( - () => timers.enroll({}, val), - { - code: 'ERR_INVALID_ARG_TYPE', - name: 'TypeError' - } - ); -}); - -[ - -1, - Infinity, - NaN, -].forEach((val) => { - assert.throws( - () => timers.enroll({}, val), - { - code: 'ERR_OUT_OF_RANGE', - name: 'RangeError', - message: 'The value of "msecs" is out of range. ' + - 'It must be a non-negative finite number. ' + - `Received ${val}` - } - ); -}); diff --git a/test/parallel/test-timers-enroll-second-time.js b/test/parallel/test-timers-enroll-second-time.js deleted file mode 100644 index a914e6c4a18a98..00000000000000 --- a/test/parallel/test-timers-enroll-second-time.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict'; - -const common = require('../common'); - -const assert = require('assert'); -const timers = require('timers'); - -const enrollObj = { - _onTimeout: common.mustCall(), -}; - -timers.enroll(enrollObj, 1); -assert.strictEqual(enrollObj._idleTimeout, 1); -timers.enroll(enrollObj, 10); -assert.strictEqual(enrollObj._idleTimeout, 10); -timers.active(enrollObj); diff --git a/test/parallel/test-timers-max-duration-warning.js b/test/parallel/test-timers-max-duration-warning.js index a104c1700db81d..cc9595ff9ccef0 100644 --- a/test/parallel/test-timers-max-duration-warning.js +++ b/test/parallel/test-timers-max-duration-warning.js @@ -2,7 +2,6 @@ const common = require('../common'); const assert = require('assert'); -const timers = require('timers'); const OVERFLOW = Math.pow(2, 31); // TIMEOUT_MAX is 2^31-1 @@ -19,7 +18,7 @@ process.on('warning', common.mustCall((warning) => { assert.strictEqual(lines[0], `${OVERFLOW} does not fit into a 32-bit signed` + ' integer.'); assert.strictEqual(lines.length, 2); -}, 6)); +}, 2)); { @@ -31,12 +30,3 @@ process.on('warning', common.mustCall((warning) => { const interval = setInterval(timerNotCanceled, OVERFLOW); clearInterval(interval); } - -{ - const timer = { - _onTimeout: timerNotCanceled - }; - timers.enroll(timer, OVERFLOW); - timers.active(timer); - timers.unenroll(timer); -} diff --git a/test/parallel/test-timers-unenroll-unref-interval.js b/test/parallel/test-timers-unenroll-unref-interval.js index 8d9203a04ac96d..f0e965296f94b4 100644 --- a/test/parallel/test-timers-unenroll-unref-interval.js +++ b/test/parallel/test-timers-unenroll-unref-interval.js @@ -1,7 +1,6 @@ 'use strict'; const common = require('../common'); -const timers = require('timers'); { const interval = setInterval(common.mustCall(() => { @@ -17,7 +16,7 @@ const timers = require('timers'); { const interval = setInterval(common.mustCall(() => { - timers.unenroll(interval); + clearInterval(interval); }), 1).unref(); } diff --git a/test/parallel/test-timers-unref-active.js b/test/parallel/test-timers-unref-active.js deleted file mode 100644 index a620304089e500..00000000000000 --- a/test/parallel/test-timers-unref-active.js +++ /dev/null @@ -1,47 +0,0 @@ -'use strict'; - -// This test is aimed at making sure that unref timers queued with -// timers._unrefActive work correctly. -// -// Basically, it queues one timer in the unref queue, and then queues -// it again each time its timeout callback is fired until the callback -// has been called ten times. -// -// At that point, it unenrolls the unref timer so that its timeout callback -// is not fired ever again. -// -// Finally, a ref timeout is used with a delay large enough to make sure that -// all 10 timeouts had the time to expire. - -require('../common'); -const timers = require('timers'); -const assert = require('assert'); - -const someObject = {}; -let nbTimeouts = 0; - -// libuv 0.10.x uses GetTickCount on Windows to implement timers, which uses -// system's timers whose resolution is between 10 and 16ms. See -// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408.aspx -// for more information. That's the lowest resolution for timers across all -// supported platforms. We're using it as the lowest common denominator, -// and thus expect 5 timers to be able to fire in under 100 ms. -const N = 5; -const TEST_DURATION = 1000; - -timers.unenroll(someObject); -timers.enroll(someObject, 1); - -someObject._onTimeout = function _onTimeout() { - ++nbTimeouts; - - if (nbTimeouts === N) timers.unenroll(someObject); - - timers._unrefActive(someObject); -}; - -timers._unrefActive(someObject); - -setTimeout(function() { - assert.strictEqual(nbTimeouts, N); -}, TEST_DURATION); diff --git a/test/parallel/test-timers-unref-remove-other-unref-timers-only-one-fires.js b/test/parallel/test-timers-unref-remove-other-unref-timers-only-one-fires.js deleted file mode 100644 index 07c2f4398278b7..00000000000000 --- a/test/parallel/test-timers-unref-remove-other-unref-timers-only-one-fires.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict'; - - -// The goal of this test is to make sure that, after the regression introduced -// by 934bfe23a16556d05bfb1844ef4d53e8c9887c3d, the fix preserves the following -// behavior of unref timers: if two timers are scheduled to fire at the same -// time, if one unenrolls the other one in its _onTimeout callback, the other -// one will *not* fire. - -// This behavior is a private implementation detail and should not be -// considered public interface. - -require('../common'); -const timers = require('timers'); -const assert = require('assert'); - -let nbTimersFired = 0; - -const foo = { - _onTimeout: function() { - ++nbTimersFired; - timers.unenroll(bar); - } -}; - -const bar = { - _onTimeout: function() { - ++nbTimersFired; - timers.unenroll(foo); - } -}; - -timers.enroll(bar, 1); -timers._unrefActive(bar); - -timers.enroll(foo, 1); -timers._unrefActive(foo); - -setTimeout(function() { - assert.notStrictEqual(nbTimersFired, 2); -}, 20); diff --git a/test/parallel/test-timers-unref-remove-other-unref-timers.js b/test/parallel/test-timers-unref-remove-other-unref-timers.js deleted file mode 100644 index 0926bd308c1efa..00000000000000 --- a/test/parallel/test-timers-unref-remove-other-unref-timers.js +++ /dev/null @@ -1,30 +0,0 @@ -'use strict'; - -// Regression test for https://github.com/nodejs/node-v0.x-archive/issues/8897. - -// Test some private implementation details that should not be -// considered public interface. -const common = require('../common'); -const timers = require('timers'); - -const foo = { - _onTimeout: common.mustNotCall('_onTimeout should not be called') -}; - -const bar = { - _onTimeout: common.mustCall(function() { - timers.unenroll(foo); - }) -}; - -// We use timers with expiration times that are sufficiently apart to make -// sure that they're not fired at the same time on platforms where the timer -// resolution is a bit coarse (e.g Windows with a default resolution of ~15ms). -timers.enroll(bar, 1); -timers._unrefActive(bar); - -timers.enroll(foo, 50); -timers._unrefActive(foo); - -// Keep the process open. -setTimeout(() => {}, 100);