Skip to content

Commit 7b6df4a

Browse files
committed
process: fix symbol key and mark experimental new node:process methods
PR-URL: #56517 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent dd3f948 commit 7b6df4a

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

doc/api/process.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3234,11 +3234,13 @@ console.log(`The parent process is pid ${ppid}`);
32343234
added: v23.6.0
32353235
-->
32363236
3237+
> Stability: 1 - Experimental
3238+
32373239
* `maybeRefable` {any} An object that may be "refable".
32383240
32393241
An object is "refable" if it implements the Node.js "Refable protocol".
3240-
Specifically, this means that the object implements the `Symbol.for('node:ref')`
3241-
and `Symbol.for('node:unref')` methods. "Ref'd" objects will keep the Node.js
3242+
Specifically, this means that the object implements the `Symbol.for('nodejs.ref')`
3243+
and `Symbol.for('nodejs.unref')` methods. "Ref'd" objects will keep the Node.js
32423244
event loop alive, while "unref'd" objects will not. Historically, this was
32433245
implemented by using `ref()` and `unref()` methods directly on the objects.
32443246
This pattern, however, is being deprecated in favor of the "Refable protocol"
@@ -4291,11 +4293,13 @@ In [`Worker`][] threads, `process.umask(mask)` will throw an exception.
42914293
added: v23.6.0
42924294
-->
42934295
4296+
> Stability: 1 - Experimental
4297+
42944298
* `maybeUnfefable` {any} An object that may be "unref'd".
42954299
42964300
An object is "unrefable" if it implements the Node.js "Refable protocol".
4297-
Specifically, this means that the object implements the `Symbol.for('node:ref')`
4298-
and `Symbol.for('node:unref')` methods. "Ref'd" objects will keep the Node.js
4301+
Specifically, this means that the object implements the `Symbol.for('nodejs.ref')`
4302+
and `Symbol.for('nodejs.unref')` methods. "Ref'd" objects will keep the Node.js
42994303
event loop alive, while "unref'd" objects will not. Historically, this was
43004304
implemented by using `ref()` and `unref()` methods directly on the objects.
43014305
This pattern, however, is being deprecated in favor of the "Refable protocol"

lib/internal/process/per_thread.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,14 @@ function toggleTraceCategoryState(asyncHooksEnabled) {
421421
const { arch, platform, version } = process;
422422

423423
function ref(maybeRefable) {
424-
const fn = maybeRefable?.[SymbolFor('node:ref')] || maybeRefable?.ref;
424+
const fn = maybeRefable?.[SymbolFor('nodejs.ref')] || maybeRefable?.[SymbolFor('node:ref')] || maybeRefable?.ref;
425425
if (typeof fn === 'function') FunctionPrototypeCall(fn, maybeRefable);
426426
}
427427

428428
function unref(maybeRefable) {
429-
const fn = maybeRefable?.[SymbolFor('node:unref')] || maybeRefable?.unref;
429+
const fn = maybeRefable?.[SymbolFor('nodejs.unref')] ||
430+
maybeRefable?.[SymbolFor('node:unref')] ||
431+
maybeRefable?.unref;
430432
if (typeof fn === 'function') FunctionPrototypeCall(fn, maybeRefable);
431433
}
432434

test/parallel/test-process-ref-unref.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ class Foo {
2323
}
2424

2525
class Foo2 {
26+
refCalled = 0;
27+
unrefCalled = 0;
28+
[Symbol.for('nodejs.ref')]() {
29+
this.refCalled++;
30+
}
31+
[Symbol.for('nodejs.unref')]() {
32+
this.unrefCalled++;
33+
}
34+
}
35+
36+
// TODO(aduh95): remove support for undocumented symbol
37+
class Foo3 {
2638
refCalled = 0;
2739
unrefCalled = 0;
2840
[Symbol.for('node:ref')]() {
@@ -39,14 +51,19 @@ describe('process.ref/unref work as expected', () => {
3951
// just work.
4052
const foo1 = new Foo();
4153
const foo2 = new Foo2();
54+
const foo3 = new Foo3();
4255
process.ref(foo1);
4356
process.unref(foo1);
4457
process.ref(foo2);
4558
process.unref(foo2);
59+
process.ref(foo3);
60+
process.unref(foo3);
4661
strictEqual(foo1.refCalled, 1);
4762
strictEqual(foo1.unrefCalled, 1);
4863
strictEqual(foo2.refCalled, 1);
4964
strictEqual(foo2.unrefCalled, 1);
65+
strictEqual(foo3.refCalled, 1);
66+
strictEqual(foo3.unrefCalled, 1);
5067

5168
// Objects that implement the legacy API also just work.
5269
const i = setInterval(() => {}, 1000);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy