Skip to content
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

emit newListener event before new listener is added #10993

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/bun.js/bindings/webcore/JSEventEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,9 @@ inline JSC::EncodedJSValue JSEventEmitter::addListener(JSC::JSGlobalObject* lexi
auto eventType = argument0.value().toPropertyKey(lexicalGlobalObject);
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
EnsureStillAliveScope argument1 = callFrame->uncheckedArgument(1);
auto listener = convert<IDLNullable<IDLEventListener<JSEventListener>>>(*lexicalGlobalObject, argument1.value(), *castedThis, [](JSC::JSGlobalObject& lexicalGlobalObject, JSC::ThrowScope& scope) { throwArgumentMustBeObjectError(lexicalGlobalObject, scope, 1, "listener", "EventEmitter", "addListener"); });
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
JSValue::encode(toJS<IDLUndefined>(*lexicalGlobalObject, throwScope, [&]() -> decltype(auto) { return impl.addListenerForBindings(WTFMove(eventType), WTFMove(listener), once, prepend); }));
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());

// see EventEmitterPrototype.addListener in events.ts
// first, emit the newListener event
JSC::Identifier newListenerEventType = JSC::Identifier::fromString(vm, "newListener"_s);
JSC::MarkedArgumentBuffer args;
args.append(argument0.value());
Expand All @@ -247,6 +245,12 @@ inline JSC::EncodedJSValue JSEventEmitter::addListener(JSC::JSGlobalObject* lexi
JSValue::encode(toJS<IDLBoolean>(*lexicalGlobalObject, throwScope, [&]() -> decltype(auto) { return impl.emitForBindings(WTFMove(newListenerEventType), WTFMove(args)); }));
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());

// then, add the listener
auto listener = convert<IDLNullable<IDLEventListener<JSEventListener>>>(*lexicalGlobalObject, argument1.value(), *castedThis, [](JSC::JSGlobalObject& lexicalGlobalObject, JSC::ThrowScope& scope) { throwArgumentMustBeObjectError(lexicalGlobalObject, scope, 1, "listener", "EventEmitter", "addListener"); });
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());
JSValue::encode(toJS<IDLUndefined>(*lexicalGlobalObject, throwScope, [&]() -> decltype(auto) { return impl.addListenerForBindings(WTFMove(eventType), WTFMove(listener), once, prepend); }));
RETURN_IF_EXCEPTION(throwScope, encodedJSValue());

vm.writeBarrier(&static_cast<JSObject&>(*castedThis), argument1.value());
impl.setThisObject(actualThis);
RELEASE_AND_RETURN(throwScope, JSValue::encode(actualThis));
Expand Down
12 changes: 11 additions & 1 deletion test/js/node/stream/node-stream.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, describe, it } from "bun:test";
import { expect, describe, it, jest } from "bun:test";
import { Stream, Readable, Writable, Duplex, Transform, PassThrough } from "node:stream";
import { createReadStream } from "node:fs";
import { join } from "path";
Expand Down Expand Up @@ -588,3 +588,13 @@ it("should send Readable events in the right order", async () => {
``,
]);
});

it("emits newListener event _before_ adding the listener", () => {
const cb = jest.fn(event => {
expect(stream.listenerCount(event)).toBe(0);
});
const stream = new Stream();
stream.on("newListener", cb);
stream.on("foo", () => {});
expect(cb).toHaveBeenCalled();
});