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

events: support emit on nodeeventtarget #35851

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 20 additions & 0 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ ObjectDefineProperty(Event.prototype, SymbolToStringTag, {
value: 'Event',
});

class NodeCustomEvent extends Event {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name bikeshedding - welcome

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 from me

constructor(type, options) {
super(type, options);
if (options && options.detail) {
this.detail = options.detail;
}
}
}
// The listeners for an EventTarget are maintained as a linked list.
// Unfortunately, the way EventTarget is defined, listeners are accounted
// using the tuple [handler,capture], and even if we don't actually make
Expand Down Expand Up @@ -377,6 +385,9 @@ class EventTarget {
event[kTarget] = undefined;
}

[kCreateEvent](nodeValue, type) {
return new NodeCustomEvent(type, { detail: nodeValue });
}
[customInspectSymbol](depth, options) {
const name = this.constructor.name;
if (depth < 0)
Expand Down Expand Up @@ -474,6 +485,14 @@ class NodeEventTarget extends EventTarget {
this.addEventListener(type, listener, { [kIsNodeStyleListener]: true });
return this;
}
emit(type, arg) {
if (typeof type !== 'string') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor suggestion: use validateString from /internal/validators.

Suggested change
if (typeof type !== 'string') {
validateString(type, 'type')

throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
}
const hadListeners = this.listenerCount(type) > 0;
this[kHybridDispatch](arg, type);
return hadListeners;
}

once(type, listener) {
this.addEventListener(type, listener,
Expand Down Expand Up @@ -502,6 +521,7 @@ ObjectDefineProperties(NodeEventTarget.prototype, {
on: { enumerable: true },
addListener: { enumerable: true },
once: { enumerable: true },
emit: { enumerable: true },
removeAllListeners: { enumerable: true },
});

Expand Down
4 changes: 4 additions & 0 deletions lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,15 @@ ObjectDefineProperties(MessageEvent.prototype, {
},
});

const originalCreateEvent = EventTarget.prototype[kCreateEvent];
ObjectDefineProperty(
MessagePort.prototype,
kCreateEvent,
{
value: function(data, type) {
if (type !== 'message' && type !== 'messageerror') {
benjamingr marked this conversation as resolved.
Show resolved Hide resolved
return originalCreateEvent.call(this, data, type);
}
return new MessageEvent(type, { data });
},
configurable: false,
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-nodeeventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
deepStrictEqual,
ok,
strictEqual,
throws,
} = require('assert');

const { on } = require('events');
Expand Down Expand Up @@ -145,6 +146,27 @@ const { on } = require('events');
target.on('foo', () => {});
target.on('foo', () => {});
}
{
// Test NodeEventTarget emit
const emitter = new NodeEventTarget();
emitter.addEventListener('foo', common.mustCall((e) => {
strictEqual(e.type, 'foo');
strictEqual(e.detail, 'bar');
ok(e instanceof Event);
}), { once: true });
emitter.once('foo', common.mustCall((e, droppedAdditionalArgument) => {
strictEqual(e, 'bar');
strictEqual(droppedAdditionalArgument, undefined);
}));
emitter.emit('foo', 'bar', 'baz');
}
{
// Test NodeEventTarget emit unsupported usage
const emitter = new NodeEventTarget();
throws(() => {
emitter.emit();
}, /ERR_INVALID_ARG_TYPE/);
}

(async () => {
// test NodeEventTarget async-iterability
Expand Down
13 changes: 12 additions & 1 deletion test/parallel/test-worker-message-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ const { MessageChannel, MessagePort } = require('worker_threads');
port2.close(common.mustCall());
}));
}

{
// Test emitting non-message events on a port
const { port2 } = new MessageChannel();
port2.addEventListener('foo', common.mustCall((received) => {
assert.strictEqual(received.type, 'foo');
assert.strictEqual(received.detail, 'bar');
}));
port2.on('foo', common.mustCall((received) => {
assert.strictEqual(received, 'bar');
}));
port2.emit('foo', 'bar');
}
{
const { port1, port2 } = new MessageChannel();

Expand Down