-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
process: add process.ref() and process.unref() methods
The `process.ref(...)` and `process.unref(...)` methods are intended to replace the use of `ref()` and `unref()` methods defined directly on individual API objects. The existing `ref()` and `unref()` methods will be marked as legacy and won't be removed but new APIs should use `process.ref()` and `process.unref()` instead. Refs: #53266 PR-URL: #56400 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Chemi Atlow <[email protected]>
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const { | ||
describe, | ||
it, | ||
} = require('node:test'); | ||
|
||
const { | ||
strictEqual, | ||
} = require('node:assert'); | ||
|
||
class Foo { | ||
refCalled = 0; | ||
unrefCalled = 0; | ||
ref() { | ||
this.refCalled++; | ||
} | ||
unref() { | ||
this.unrefCalled++; | ||
} | ||
} | ||
|
||
class Foo2 { | ||
refCalled = 0; | ||
unrefCalled = 0; | ||
[Symbol.for('node:ref')]() { | ||
this.refCalled++; | ||
} | ||
[Symbol.for('node:unref')]() { | ||
this.unrefCalled++; | ||
} | ||
} | ||
|
||
describe('process.ref/unref work as expected', () => { | ||
it('refs...', () => { | ||
// Objects that implement the new Symbol-based API | ||
// just work. | ||
const foo1 = new Foo(); | ||
const foo2 = new Foo2(); | ||
process.ref(foo1); | ||
process.unref(foo1); | ||
process.ref(foo2); | ||
process.unref(foo2); | ||
strictEqual(foo1.refCalled, 1); | ||
strictEqual(foo1.unrefCalled, 1); | ||
strictEqual(foo2.refCalled, 1); | ||
strictEqual(foo2.unrefCalled, 1); | ||
|
||
// Objects that implement the legacy API also just work. | ||
const i = setInterval(() => {}, 1000); | ||
strictEqual(i.hasRef(), true); | ||
process.unref(i); | ||
strictEqual(i.hasRef(), false); | ||
process.ref(i); | ||
strictEqual(i.hasRef(), true); | ||
clearInterval(i); | ||
}); | ||
}); |