From a739471f57a71fa7cba5b03fe8bb3c7800195c16 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 23 Apr 2022 20:33:11 +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 --- 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 bbb35aa3ca5410..0db2e620f6f239 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -763,6 +763,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()`