diff --git a/ext/web/13_message_port.js b/ext/web/13_message_port.js index 83470c8957..24982a982c 100644 --- a/ext/web/13_message_port.js +++ b/ext/web/13_message_port.js @@ -22,6 +22,7 @@ const { Symbol, SymbolFor, SymbolIterator, + SafeArrayIterator, TypeError, } = primordials; const { @@ -40,6 +41,8 @@ import { import { isDetachedBuffer } from "./06_streams.js"; import { DOMException } from "./01_dom_exception.js"; +let messageEventListenerCount = 0; + class MessageChannel { /** @type {MessagePort} */ #port1; @@ -222,6 +225,20 @@ class MessagePort extends EventTarget { } } + removeEventListener(...args) { + if (args[0] == "message") { + messageEventListenerCount--; + } + super.removeEventListener(...new SafeArrayIterator(args)); + } + + addEventListener(...args) { + if (args[0] == "message") { + messageEventListenerCount++; + } + super.addEventListener(...new SafeArrayIterator(args)); + } + [SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) { return inspect( createFilteredInspectProxy({ @@ -406,6 +423,7 @@ function structuredClone(value, options) { export { deserializeJsMessageData, MessageChannel, + messageEventListenerCount, MessagePort, MessagePortIdSymbol, MessagePortPrototype, diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index f65bd2b017..cbba284342 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -282,7 +282,8 @@ let isClosing = false; let globalDispatchEvent; function hasMessageEventListener() { - return event.listenerCount(globalThis, "message") > 0; + return event.listenerCount(globalThis, "message") > 0 || + messagePort.messageEventListenerCount > 0; } async function pollForMessages() {