Skip to content

Commit e2442bb

Browse files
MoLowjuanarbol
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 4e08160 commit e2442bb

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
@@ -24,6 +24,7 @@
2424
const {
2525
MathTrunc,
2626
ObjectDefineProperty,
27+
SymbolDispose,
2728
SymbolToPrimitive,
2829
} = primordials;
2930

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

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

346+
Immediate.prototype[SymbolDispose] = function() {
347+
clearImmediate(this);
348+
};
349+
341350
module.exports = {
342351
setTimeout,
343352
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)