-
Notifications
You must be signed in to change notification settings - Fork 5k
MessagePort alignment with Nodejs #19941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
df78820
fd01ae8
64752be
09c8fad
298a91e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |
| #include <wtf/TZoneMallocInlines.h> | ||
| #include <wtf/Lock.h> | ||
| #include <wtf/Scope.h> | ||
| #include "Event.h" | ||
|
|
||
| extern "C" void Bun__eventLoop__incrementRefConcurrently(void* bunVM, int delta); | ||
|
|
||
|
|
@@ -104,6 +105,7 @@ void MessagePort::notifyMessageAvailable(const MessagePortIdentifier& identifier | |
| Ref<MessagePort> MessagePort::create(ScriptExecutionContext& scriptExecutionContext, const MessagePortIdentifier& local, const MessagePortIdentifier& remote) | ||
| { | ||
| auto messagePort = adoptRef(*new MessagePort(scriptExecutionContext, local, remote)); | ||
| messagePort->m_strongRef = messagePort.ptr(); | ||
| // messagePort->suspendIfNeeded(); | ||
| return messagePort; | ||
| } | ||
|
|
@@ -229,18 +231,30 @@ void MessagePort::start() | |
| return; | ||
|
|
||
| m_started = true; | ||
| scriptExecutionContext()->processMessageWithMessagePortsSoon([pendingActivity = Ref { *this }] {}); | ||
| auto* context = scriptExecutionContext(); | ||
| context->processMessageWithMessagePortsSoon([pendingActivity = Ref { *this }] {}); | ||
| context->refEventLoop(); | ||
| } | ||
|
|
||
| void MessagePort::close() | ||
| { | ||
| if (m_isDetached) | ||
| if (m_isDetached || m_messagesInFight > 0) | ||
| return; | ||
| m_isDetached = true; | ||
|
|
||
| dispatchCloseEvent(); | ||
| MessagePortChannelProvider::singleton().messagePortClosed(m_identifier); | ||
|
|
||
| removeAllEventListeners(); | ||
|
|
||
| // message port is closed, we can safely mark it for GC | ||
| this->m_strongRef = nullptr; | ||
|
|
||
| scriptExecutionContext()->unrefEventLoop(); | ||
| } | ||
|
|
||
| void MessagePort::dispatchCloseEvent() | ||
| { | ||
| dispatchEvent(Event::create(eventNames().closeEvent, Event::CanBubble::No, Event::IsCancelable::No)); | ||
| } | ||
|
|
||
| void MessagePort::dispatchMessages() | ||
|
|
@@ -266,6 +280,7 @@ void MessagePort::dispatchMessages() | |
| auto* globalObject = defaultGlobalObject(context->globalObject()); | ||
| Ref vm = globalObject->vm(); | ||
| auto scope = DECLARE_CATCH_SCOPE(vm); | ||
| protectedThis->m_messagesInFight += messages.size(); | ||
|
|
||
| for (auto& message : messages) { | ||
| // close() in Worker onmessage handler should prevent next message from dispatching. | ||
|
|
@@ -287,6 +302,8 @@ void MessagePort::dispatchMessages() | |
| ScriptExecutionContext::postTaskTo(context->identifier(), [protectedThis = Ref { *this }, ports = WTFMove(ports), message = WTFMove(message)](ScriptExecutionContext& context) mutable { | ||
| auto event = MessageEvent::create(*context.jsGlobalObject(), message.message.releaseNonNull(), {}, {}, {}, WTFMove(ports)); | ||
| protectedThis->dispatchEvent(event.event); | ||
| protectedThis->m_messagesInFight -= 1; | ||
| protectedThis->close(); | ||
| }); | ||
| } | ||
| }; | ||
|
|
@@ -427,8 +444,7 @@ Ref<MessagePort> MessagePort::entangle(ScriptExecutionContext& context, Transfer | |
| bool MessagePort::addEventListener(const AtomString& eventType, Ref<EventListener>&& listener, const AddEventListenerOptions& options) | ||
| { | ||
| if (eventType == eventNames().messageEvent) { | ||
| if (listener->isAttribute()) | ||
| start(); | ||
|
Comment on lines
-430
to
-431
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please see my comment on actual issue. #19863 (comment) I will emphasis that we should make And until then just having documentation for such points would be suffice. |
||
| start(); | ||
| m_hasMessageEventListener = true; | ||
| } | ||
| return EventTarget::addEventListener(eventType, WTFMove(listener), options); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5779,6 +5779,7 @@ ExceptionOr<Ref<SerializedScriptValue>> SerializedScriptValue::create(JSGlobalOb | |||||
| if (auto port = JSMessagePort::toWrapped(vm, transferable.get())) { | ||||||
| if (port->isDetached()) | ||||||
| return Exception { DataCloneError, "MessagePort in transfer list is already detached"_s }; | ||||||
| port->dispatchCloseEvent(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
Spotted by Diamond |
||||||
| messagePorts.append(WTFMove(port)); | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.