Skip to content

Commit 7822a54

Browse files
MoLowruyadorno
authored andcommitted
timers: support Symbol.dispose
PR-URL: #48633 Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com>
1 parent ea23870 commit 7822a54

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

doc/api/timers.md

+20
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ loop to remain active. If there is no other activity keeping the event loop
6363
running, the process may exit before the `Immediate` object's callback is
6464
invoked. Calling `immediate.unref()` multiple times will have no effect.
6565

66+
### `immediate[Symbol.dispose]()`
67+
68+
<!-- YAML
69+
added: REPLACEME
70+
-->
71+
72+
> Stability: 1 - Experimental
73+
74+
Cancels the immediate. This is similar to calling `clearImmediate()`.
75+
6676
## Class: `Timeout`
6777

6878
This object is created internally and is returned from [`setTimeout()`][] and
@@ -157,6 +167,16 @@ across [`worker_threads`][] it must first be passed to the correct
157167
thread. This allows enhanced compatibility with browser
158168
`setTimeout()` and `setInterval()` implementations.
159169

170+
### `timeout[Symbol.dispose]()`
171+
172+
<!-- YAML
173+
added: REPLACEME
174+
-->
175+
176+
> Stability: 1 - Experimental
177+
178+
Cancels the timeout.
179+
160180
## Scheduling timers
161181

162182
A timer in Node.js is an internal construct that calls a given function after

lib/internal/per_context/primordials.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function copyPrototype(src, dest, prefix) {
228228
copyPrototype(original.prototype, primordials, `${name}Prototype`);
229229
});
230230

231-
// Define Symbol.Dispose and Symbol.AsyncDispose
231+
// Define Symbol.dispose and Symbol.asyncDispose
232232
// Until these are defined by the environment.
233233
// TODO(MoLow): Remove this polyfill once Symbol.dispose and Symbol.asyncDispose are available in V8.
234234
primordials.SymbolDispose ??= primordials.SymbolFor('nodejs.dispose');

lib/timers.js

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {
2525
MathTrunc,
2626
ObjectCreate,
2727
ObjectDefineProperty,
28+
SymbolDispose,
2829
SymbolToPrimitive,
2930
} = primordials;
3031

@@ -254,6 +255,10 @@ Timeout.prototype.close = function() {
254255
return this;
255256
};
256257

258+
Timeout.prototype[SymbolDispose] = function() {
259+
clearTimeout(this);
260+
};
261+
257262
/**
258263
* Coerces a `Timeout` to a primitive.
259264
* @returns {number}
@@ -337,6 +342,10 @@ function clearImmediate(immediate) {
337342
immediateQueue.remove(immediate);
338343
}
339344

345+
Immediate.prototype[SymbolDispose] = function() {
346+
clearImmediate(this);
347+
};
348+
340349
module.exports = {
341350
setTimeout,
342351
clearTimeout,

test/parallel/test-timers-dispose.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
const timer = setTimeout(common.mustNotCall(), 10);
6+
const interval = setInterval(common.mustNotCall(), 10);
7+
const immediate = setImmediate(common.mustNotCall());
8+
9+
timer[Symbol.dispose]();
10+
interval[Symbol.dispose]();
11+
immediate[Symbol.dispose]();
12+
13+
14+
process.on('exit', () => {
15+
assert.strictEqual(timer._destroyed, true);
16+
assert.strictEqual(interval._destroyed, true);
17+
assert.strictEqual(immediate._destroyed, true);
18+
});

0 commit comments

Comments
 (0)