summaryrefslogtreecommitdiff
path: root/ext/web
diff options
context:
space:
mode:
authorSatya Rohith <me@satyarohith.com>2024-04-02 17:06:09 +0530
committerGitHub <noreply@github.com>2024-04-02 17:06:09 +0530
commit4d66ec91c1ca23134dc25f58f41da52a99615a38 (patch)
tree96b9de0cf5ac664f14ac8fc35f93d2d3da65a87f /ext/web
parent7ad76fd453972e9262985c61840c77b8b8a6dbb7 (diff)
fix(ext/node): MessagePort works (#22999)
Closes https://github.com/denoland/deno/issues/22951 Closes https://github.com/denoland/deno/issues/23001 Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
Diffstat (limited to 'ext/web')
-rw-r--r--ext/web/13_message_port.js29
1 files changed, 28 insertions, 1 deletions
diff --git a/ext/web/13_message_port.js b/ext/web/13_message_port.js
index d953c52ed..83470c895 100644
--- a/ext/web/13_message_port.js
+++ b/ext/web/13_message_port.js
@@ -18,6 +18,7 @@ const {
ArrayPrototypeIncludes,
ArrayPrototypePush,
ObjectPrototypeIsPrototypeOf,
+ ObjectDefineProperty,
Symbol,
SymbolFor,
SymbolIterator,
@@ -85,6 +86,8 @@ const MessageChannelPrototype = MessageChannel.prototype;
const _id = Symbol("id");
const MessagePortIdSymbol = _id;
const _enabled = Symbol("enabled");
+const nodeWorkerThreadCloseCb = Symbol("nodeWorkerThreadCloseCb");
+const nodeWorkerThreadCloseCbInvoked = Symbol("nodeWorkerThreadCloseCbInvoked");
/**
* @param {number} id
@@ -98,6 +101,16 @@ function createMessagePort(id) {
return port;
}
+function nodeWorkerThreadMaybeInvokeCloseCb(port) {
+ if (
+ typeof port[nodeWorkerThreadCloseCb] == "function" &&
+ !port[nodeWorkerThreadCloseCbInvoked]
+ ) {
+ port[nodeWorkerThreadCloseCb]();
+ port[nodeWorkerThreadCloseCbInvoked] = true;
+ }
+}
+
class MessagePort extends EventTarget {
/** @type {number | null} */
[_id] = null;
@@ -106,6 +119,14 @@ class MessagePort extends EventTarget {
constructor() {
super();
+ ObjectDefineProperty(this, nodeWorkerThreadCloseCb, {
+ value: null,
+ enumerable: false,
+ });
+ ObjectDefineProperty(this, nodeWorkerThreadCloseCbInvoked, {
+ value: false,
+ enumerable: false,
+ });
webidl.illegalConstructor();
}
@@ -160,9 +181,13 @@ class MessagePort extends EventTarget {
);
} catch (err) {
if (ObjectPrototypeIsPrototypeOf(InterruptedPrototype, err)) break;
+ nodeWorkerThreadMaybeInvokeCloseCb(this);
throw err;
}
- if (data === null) break;
+ if (data === null) {
+ nodeWorkerThreadMaybeInvokeCloseCb(this);
+ break;
+ }
let message, transferables;
try {
const v = deserializeJsMessageData(data);
@@ -193,6 +218,7 @@ class MessagePort extends EventTarget {
if (this[_id] !== null) {
core.close(this[_id]);
this[_id] = null;
+ nodeWorkerThreadMaybeInvokeCloseCb(this);
}
}
@@ -383,6 +409,7 @@ export {
MessagePort,
MessagePortIdSymbol,
MessagePortPrototype,
+ nodeWorkerThreadCloseCb,
serializeJsMessageData,
structuredClone,
};