Files
bun.sh/src/bun.js/bindings/BunWorkerGlobalScope.cpp
dave caruso 78defe7a87 Fix worker event loop ref/unref + leak (#4114)
* make more tests pass

* worker changes

* fix some bugs

* remove this

* progress

* uh

* okay

* remove console log

* a

* comment assert for later

* mergable state

* remove test

* remove test
2023-08-12 13:51:03 -07:00

44 lines
1.4 KiB
C++

#include "config.h"
#include "BunWorkerGlobalScope.h"
#include "MessagePortChannelProviderImpl.h"
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(GlobalScope);
MessagePortChannelProvider& GlobalScope::messagePortChannelProvider()
{
return *reinterpret_cast<MessagePortChannelProvider*>(&MessagePortChannelProviderImpl::singleton());
}
void GlobalScope::onDidChangeListenerImpl(EventTarget& self, const AtomString& eventType, OnDidChangeListenerKind kind)
{
if (eventType == eventNames().messageEvent) {
auto& global = static_cast<GlobalScope&>(self);
switch (kind) {
case Add:
if (global.m_messageEventCount == 0) {
global.scriptExecutionContext()->refEventLoop();
}
global.m_messageEventCount++;
break;
case Remove:
global.m_messageEventCount--;
if (global.m_messageEventCount == 0) {
global.scriptExecutionContext()->unrefEventLoop();
}
break;
// I dont think clear in this context is ever called. If it is (search OnDidChangeListenerKind::Clear for the impl),
// it may actually call once per event, in a way the Remove code above would suffice.
case Clear:
if (global.m_messageEventCount > 0) {
global.scriptExecutionContext()->unrefEventLoop();
}
global.m_messageEventCount = 0;
break;
}
}
};
}