| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d33eaf2 commit d35333a
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3221,6 +3221,23 @@ const { ppid } = require('node:process'); | |||
| 3221 | 3221 | console.log(`The parent process is pid ${ppid}`); | |
| 3222 | 3222 | ``` | |
| 3223 | 3223 | ||
| 3224 | + ## `process.ref(maybeRefable)` | ||
| 3225 | + | ||
| 3226 | + <!-- YAML | ||
| 3227 | + added: REPLACEME | ||
| 3228 | + --> | ||
| 3229 | + | ||
| 3230 | + * `maybeRefable` {any} An object that may be "refable". | ||
| 3231 | + | ||
| 3232 | + An object is "refable" if it implements the Node.js "Refable protocol". | ||
| 3233 | + Specifically, this means that the object implements the `Symbol.for('node:ref')` | ||
| 3234 | + and `Symbol.for('node:unref')` methods. "Ref'd" objects will keep the Node.js | ||
| 3235 | + event loop alive, while "unref'd" objects will not. Historically, this was | ||
| 3236 | + implemented by using `ref()` and `unref()` methods directly on the objects. | ||
| 3237 | + This pattern, however, is being deprecated in favor of the "Refable protocol" | ||
| 3238 | + in order to better support Web Platform API types whose APIs cannot be modified | ||
| 3239 | + to add `ref()` and `unref()` methods but still need to support that behavior. | ||
| 3240 | + | ||
| 3224 | 3241 | ## `process.release` | |
| 3225 | 3242 | ||
| 3226 | 3243 | <!-- YAML | |
@@ -4261,6 +4278,23 @@ console.log( | |||
| 4261 | 4278 | ||
| 4262 | 4279 | In [`Worker`][] threads, `process.umask(mask)` will throw an exception. | |
| 4263 | 4280 | ||
| 4281 | + ## `process.unref(maybeRefable)` | ||
| 4282 | + | ||
| 4283 | + <!-- YAML | ||
| 4284 | + added: REPLACEME | ||
| 4285 | + --> | ||
| 4286 | + | ||
| 4287 | + * `maybeUnfefable` {any} An object that may be "unref'd". | ||
| 4288 | + | ||
| 4289 | + An object is "unrefable" if it implements the Node.js "Refable protocol". | ||
| 4290 | + Specifically, this means that the object implements the `Symbol.for('node:ref')` | ||
| 4291 | + and `Symbol.for('node:unref')` methods. "Ref'd" objects will keep the Node.js | ||
| 4292 | + event loop alive, while "unref'd" objects will not. Historically, this was | ||
| 4293 | + implemented by using `ref()` and `unref()` methods directly on the objects. | ||
| 4294 | + This pattern, however, is being deprecated in favor of the "Refable protocol" | ||
| 4295 | + in order to better support Web Platform API types whose APIs cannot be modified | ||
| 4296 | + to add `ref()` and `unref()` methods but still need to support that behavior. | ||
| 4297 | + | ||
| 4264 | 4298 | ## `process.uptime()` | |
| 4265 | 4299 | ||
| 4266 | 4300 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -181,6 +181,8 @@ const rawMethods = internalBinding('process_methods'); | |||
| 181 | 181 | process.availableMemory = rawMethods.availableMemory; | |
| 182 | 182 | process.kill = wrapped.kill; | |
| 183 | 183 | process.exit = wrapped.exit; | |
| 184 | + process.ref = perThreadSetup.ref; | ||
| 185 | + process.unref = perThreadSetup.unref; | ||
| 184 | 186 | ||
| 185 | 187 | let finalizationMod; | |
| 186 | 188 | ObjectDefineProperty(process, 'finalization', { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,7 @@ const { | |||
| 13 | 13 | ArrayPrototypeSplice, | |
| 14 | 14 | BigUint64Array, | |
| 15 | 15 | Float64Array, | |
| 16 | + FunctionPrototypeCall, | ||
| 16 | 17 | NumberMAX_SAFE_INTEGER, | |
| 17 | 18 | ObjectDefineProperty, | |
| 18 | 19 | ObjectFreeze, | |
@@ -26,6 +27,7 @@ const { | |||
| 26 | 27 | StringPrototypeReplace, | |
| 27 | 28 | StringPrototypeSlice, | |
| 28 | 29 | Symbol, | |
| 30 | + SymbolFor, | ||
| 29 | 31 | SymbolIterator, | |
| 30 | 32 | } = primordials; | |
| 31 | 33 | ||
@@ -422,6 +424,16 @@ function toggleTraceCategoryState(asyncHooksEnabled) { | |||
| 422 | 424 | ||
| 423 | 425 | const { arch, platform, version } = process; | |
| 424 | 426 | ||
| 427 | + function ref(maybeRefable) { | ||
| 428 | + const fn = maybeRefable?.[SymbolFor('node:ref')] || maybeRefable?.ref; | ||
| 429 | + if (typeof fn === 'function') FunctionPrototypeCall(fn, maybeRefable); | ||
| 430 | + } | ||
| 431 | + | ||
| 432 | + function unref(maybeRefable) { | ||
| 433 | + const fn = maybeRefable?.[SymbolFor('node:unref')] || maybeRefable?.unref; | ||
| 434 | + if (typeof fn === 'function') FunctionPrototypeCall(fn, maybeRefable); | ||
| 435 | + } | ||
| 436 | + | ||
| 425 | 437 | module.exports = { | |
| 426 | 438 | toggleTraceCategoryState, | |
| 427 | 439 | assert, | |
@@ -432,4 +444,6 @@ module.exports = { | |||
| 432 | 444 | arch, | |
| 433 | 445 | platform, | |
| 434 | 446 | version, | |
| 447 | + ref, | ||
| 448 | + unref, | ||
| 435 | 449 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,60 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + require('../common'); | ||
| 4 | + | ||
| 5 | + const { | ||
| 6 | + describe, | ||
| 7 | + it, | ||
| 8 | + } = require('node:test'); | ||
| 9 | + | ||
| 10 | + const { | ||
| 11 | + strictEqual, | ||
| 12 | + } = require('node:assert'); | ||
| 13 | + | ||
| 14 | + class Foo { | ||
| 15 | + refCalled = 0; | ||
| 16 | + unrefCalled = 0; | ||
| 17 | + ref() { | ||
| 18 | + this.refCalled++; | ||
| 19 | + } | ||
| 20 | + unref() { | ||
| 21 | + this.unrefCalled++; | ||
| 22 | + } | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + class Foo2 { | ||
| 26 | + refCalled = 0; | ||
| 27 | + unrefCalled = 0; | ||
| 28 | + [Symbol.for('node:ref')]() { | ||
| 29 | + this.refCalled++; | ||
| 30 | + } | ||
| 31 | + [Symbol.for('node:unref')]() { | ||
| 32 | + this.unrefCalled++; | ||
| 33 | + } | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + describe('process.ref/unref work as expected', () => { | ||
| 37 | + it('refs...', () => { | ||
| 38 | + // Objects that implement the new Symbol-based API | ||
| 39 | + // just work. | ||
| 40 | + const foo1 = new Foo(); | ||
| 41 | + const foo2 = new Foo2(); | ||
| 42 | + process.ref(foo1); | ||
| 43 | + process.unref(foo1); | ||
| 44 | + process.ref(foo2); | ||
| 45 | + process.unref(foo2); | ||
| 46 | + strictEqual(foo1.refCalled, 1); | ||
| 47 | + strictEqual(foo1.unrefCalled, 1); | ||
| 48 | + strictEqual(foo2.refCalled, 1); | ||
| 49 | + strictEqual(foo2.unrefCalled, 1); | ||
| 50 | + | ||
| 51 | + // Objects that implement the legacy API also just work. | ||
| 52 | + const i = setInterval(() => {}, 1000); | ||
| 53 | + strictEqual(i.hasRef(), true); | ||
| 54 | + process.unref(i); | ||
| 55 | + strictEqual(i.hasRef(), false); | ||
| 56 | + process.ref(i); | ||
| 57 | + strictEqual(i.hasRef(), true); | ||
| 58 | + clearInterval(i); | ||
| 59 | + }); | ||
| 60 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments