From 9a1a3c750b755a1226522cb1e3634aa514f5e365 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Mon, 2 May 2022 10:09:39 +0530 Subject: [PATCH] worker: add hasRef() to MessagePort Since we were removing the hasRef() method before exposing the MessagePort object, the only way of knowing if the handle was keeping the event loop active was to parse the string returned by util.inspect(port), which is inconvenient and inconsistent with most of the other async resources. So this change stops removing hasRef() from the MessagePort prototype. The reason why this is also being documented is that while reporting active resources, async_hooks returns the same MessagePort object as the one that is accessible by users. Refs: https://github.com/nodejs/node/issues/42091#issuecomment-1104793189 Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/42849 Reviewed-By: Anna Henningsen --- doc/api/worker_threads.md | 12 ++++ lib/internal/worker/io.js | 5 +- test/parallel/test-messageport-hasref.js | 57 +++++++++++++++++++ test/parallel/test-worker-message-port.js | 4 +- .../test-worker-messageport-hasref.js | 45 +++++++++++++++ 5 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-messageport-hasref.js create mode 100644 test/parallel/test-worker-messageport-hasref.js diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 5cc4050a9a9c84..35a122df2c470a 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -757,6 +757,18 @@ port2.postMessage(new URL('https://example.org')); // Prints: { } ``` +### `port.hasRef()` + + + +> Stability: 1 - Experimental + +* Returns: {boolean} + +If true, the `MessagePort` object will keep the Node.js event loop active. + ### `port.ref()`