diff options
author | Satya Rohith <me@satyarohith.com> | 2024-04-09 23:45:55 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-09 20:15:55 +0200 |
commit | 5a3ee6d9af875af032909489c0bed7db11b608dd (patch) | |
tree | 894744a0811b21179a65ee63858f50fbe4fc64d9 /ext | |
parent | fad12b7c2ebd87a2a11f63998f4c2549fd405eff (diff) |
fix(ext/node): implement MessagePort.unref() (#23278)
Closes https://github.com/denoland/deno/issues/23252
Closes https://github.com/denoland/deno/issues/23264
Diffstat (limited to 'ext')
-rw-r--r-- | ext/node/polyfills/worker_threads.ts | 14 | ||||
-rw-r--r-- | ext/web/13_message_port.js | 17 |
2 files changed, 31 insertions, 0 deletions
diff --git a/ext/node/polyfills/worker_threads.ts b/ext/node/polyfills/worker_threads.ts index 3c8c9d443..323095206 100644 --- a/ext/node/polyfills/worker_threads.ts +++ b/ext/node/polyfills/worker_threads.ts @@ -18,7 +18,9 @@ import { MessagePortIdSymbol, MessagePortPrototype, nodeWorkerThreadCloseCb, + refMessagePort, serializeJsMessageData, + unrefPollForMessages, } from "ext:deno_web/13_message_port.js"; import * as webidl from "ext:deno_webidl/00_webidl.js"; import { notImplemented } from "ext:deno_node/_utils.ts"; @@ -398,6 +400,12 @@ internals.__initWorkerThreads = ( parentPort.addEventListener("offline", () => { parentPort.emit("close"); }); + parentPort.unref = () => { + parentPort[unrefPollForMessages] = true; + }; + parentPort.ref = () => { + parentPort[unrefPollForMessages] = false; + }; } }; @@ -467,6 +475,12 @@ function webMessagePortToNodeMessagePort(port: MessagePort) { port[nodeWorkerThreadCloseCb] = () => { port.dispatchEvent(new Event("close")); }; + port.unref = () => { + port[refMessagePort](false); + }; + port.ref = () => { + port[refMessagePort](true); + }; return port; } diff --git a/ext/web/13_message_port.js b/ext/web/13_message_port.js index 24982a982..62c0328c3 100644 --- a/ext/web/13_message_port.js +++ b/ext/web/13_message_port.js @@ -89,8 +89,13 @@ const MessageChannelPrototype = MessageChannel.prototype; const _id = Symbol("id"); const MessagePortIdSymbol = _id; const _enabled = Symbol("enabled"); +const _refed = Symbol("refed"); const nodeWorkerThreadCloseCb = Symbol("nodeWorkerThreadCloseCb"); const nodeWorkerThreadCloseCbInvoked = Symbol("nodeWorkerThreadCloseCbInvoked"); +export const refMessagePort = Symbol("refMessagePort"); +/** It is used by 99_main.js and worker_threads to + * unref/ref on the global pollForMessages promise. */ +export const unrefPollForMessages = Symbol("unrefPollForMessages"); /** * @param {number} id @@ -119,6 +124,7 @@ class MessagePort extends EventTarget { [_id] = null; /** @type {boolean} */ [_enabled] = false; + [_refed] = false; constructor() { super(); @@ -216,6 +222,16 @@ class MessagePort extends EventTarget { })(); } + [refMessagePort](ref) { + if (ref && !this[_refed]) { + this[_refed] = true; + messageEventListenerCount++; + } else if (!ref && this[_refed]) { + this[_refed] = false; + messageEventListenerCount = 0; + } + } + close() { webidl.assertBranded(this, MessagePortPrototype); if (this[_id] !== null) { @@ -235,6 +251,7 @@ class MessagePort extends EventTarget { addEventListener(...args) { if (args[0] == "message") { messageEventListenerCount++; + if (!this[_refed]) this[_refed] = true; } super.addEventListener(...new SafeArrayIterator(args)); } |