From 35742a2d0b3ac1a1eff5ab333b5ae2fef009f00b Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 28 Dec 2024 16:06:52 -0800 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/53266 PR-URL: https://github.com/nodejs/node/pull/56400 Reviewed-By: Matteo Collina Reviewed-By: Yagiz Nizipli Reviewed-By: Chemi Atlow --- doc/api/process.md | 34 ++++++++++++++ lib/internal/bootstrap/node.js | 2 + lib/internal/process/per_thread.js | 14 ++++++ test/parallel/test-process-ref-unref.js | 60 +++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 test/parallel/test-process-ref-unref.js diff --git a/doc/api/process.md b/doc/api/process.md index 5eb05bfe48604a..a962ce594f2d36 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -3232,6 +3232,23 @@ const { ppid } = require('node:process'); console.log(`The parent process is pid ${ppid}`); ``` +## `process.ref(maybeRefable)` + + + +* `maybeRefable` {any} An object that may be "refable". + +An object is "refable" if it implements the Node.js "Refable protocol". +Specifically, this means that the object implements the `Symbol.for('node:ref')` +and `Symbol.for('node:unref')` methods. "Ref'd" objects will keep the Node.js +event loop alive, while "unref'd" objects will not. Historically, this was +implemented by using `ref()` and `unref()` methods directly on the objects. +This pattern, however, is being deprecated in favor of the "Refable protocol" +in order to better support Web Platform API types whose APIs cannot be modified +to add `ref()` and `unref()` methods but still need to support that behavior. + ## `process.release` + +* `maybeUnfefable` {any} An object that may be "unref'd". + +An object is "unrefable" if it implements the Node.js "Refable protocol". +Specifically, this means that the object implements the `Symbol.for('node:ref')` +and `Symbol.for('node:unref')` methods. "Ref'd" objects will keep the Node.js +event loop alive, while "unref'd" objects will not. Historically, this was +implemented by using `ref()` and `unref()` methods directly on the objects. +This pattern, however, is being deprecated in favor of the "Refable protocol" +in order to better support Web Platform API types whose APIs cannot be modified +to add `ref()` and `unref()` methods but still need to support that behavior. + ## `process.uptime()`