Skip to content

Commit 6e8b0c1

Browse files
committed
process: fix symbol key and mark experimental new node:process methods
PR-URL: nodejs#56517 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 3a64f6d commit 6e8b0c1

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
@@ -3221,11 +3221,13 @@ console.log(`The parent process is pid ${ppid}`);
32213221
added: REPLACEME
32223222
-->
32233223
3224+
> Stability: 1 - Experimental
3225+
32243226
* `maybeRefable` {any} An object that may be "refable".
32253227
32263228
An object is "refable" if it implements the Node.js "Refable protocol".
3227-
Specifically, this means that the object implements the `Symbol.for('node:ref')`
3228-
and `Symbol.for('node:unref')` methods. "Ref'd" objects will keep the Node.js
3229+
Specifically, this means that the object implements the `Symbol.for('nodejs.ref')`
3230+
and `Symbol.for('nodejs.unref')` methods. "Ref'd" objects will keep the Node.js
32293231
event loop alive, while "unref'd" objects will not. Historically, this was
32303232
implemented by using `ref()` and `unref()` methods directly on the objects.
32313233
This pattern, however, is being deprecated in favor of the "Refable protocol"
@@ -4278,11 +4280,13 @@ In [`Worker`][] threads, `process.umask(mask)` will throw an exception.
42784280
added: REPLACEME
42794281
-->
42804282
4283+
> Stability: 1 - Experimental
4284+
42814285
* `maybeUnfefable` {any} An object that may be "unref'd".
42824286
42834287
An object is "unrefable" if it implements the Node.js "Refable protocol".
4284-
Specifically, this means that the object implements the `Symbol.for('node:ref')`
4285-
and `Symbol.for('node:unref')` methods. "Ref'd" objects will keep the Node.js
4288+
Specifically, this means that the object implements the `Symbol.for('nodejs.ref')`
4289+
and `Symbol.for('nodejs.unref')` methods. "Ref'd" objects will keep the Node.js
42864290
event loop alive, while "unref'd" objects will not. Historically, this was
42874291
implemented by using `ref()` and `unref()` methods directly on the objects.
42884292
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
@@ -425,12 +425,14 @@ function toggleTraceCategoryState(asyncHooksEnabled) {
425425
const { arch, platform, version } = process;
426426

427427
function ref(maybeRefable) {
428-
const fn = maybeRefable?.[SymbolFor('node:ref')] || maybeRefable?.ref;
428+
const fn = maybeRefable?.[SymbolFor('nodejs.ref')] || maybeRefable?.[SymbolFor('node:ref')] || maybeRefable?.ref;
429429
if (typeof fn === 'function') FunctionPrototypeCall(fn, maybeRefable);
430430
}
431431

432432
function unref(maybeRefable) {
433-
const fn = maybeRefable?.[SymbolFor('node:unref')] || maybeRefable?.unref;
433+
const fn = maybeRefable?.[SymbolFor('nodejs.unref')] ||
434+
maybeRefable?.[SymbolFor('node:unref')] ||
435+
maybeRefable?.unref;
434436
if (typeof fn === 'function') FunctionPrototypeCall(fn, maybeRefable);
435437
}
436438

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)