Skip to content

Commit

Permalink
events: support emit on nodeeventtarget
Browse files Browse the repository at this point in the history
PR-URL: #35851
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
  • Loading branch information
benjamingr authored and nodejs-github-bot committed Nov 6, 2020
1 parent 2a1273c commit c5477be
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
20 changes: 20 additions & 0 deletions lib/internal/event_target.js
Expand Up @@ -172,6 +172,14 @@ ObjectDefineProperty(Event.prototype, SymbolToStringTag, {
value: 'Event',
});

class NodeCustomEvent extends Event {
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') {
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
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') {
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
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
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

0 comments on commit c5477be

Please sign in to comment.