From e0807fd3718bc7b559c2c8e92588e240ba0a58b7 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 0fa741c6ed5325..0770edd28f3e41 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()`