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 dispatching event from event #33624

Closed
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
9 changes: 2 additions & 7 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const {
Error,
Map,
Object,
Set,
Symbol,
NumberIsNaN,
SymbolToStringTag,
Expand Down Expand Up @@ -186,7 +185,6 @@ class Listener {

class EventTarget {
[kEvents] = new Map();
#emitting = new Set();

[kNewListener](size, type, listener, once, capture, passive) {}
[kRemoveListener](size, type, listener, capture) {}
Expand Down Expand Up @@ -257,17 +255,15 @@ class EventTarget {
throw new ERR_INVALID_ARG_TYPE('event', 'Event', event);
}

if (this.#emitting.has(event.type) ||
event[kTarget] !== null) {
throw new ERR_EVENT_RECURSION(event.type);
if (event[kTarget] !== undefined) {
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
throw new ERR_EVENT_RECURSION(event);
}

const root = this[kEvents].get(event.type);
if (root === undefined || root.next === undefined)
return true;

event[kTarget] = this;
this.#emitting.add(event.type);

let handler = root.next;
let next;
Expand All @@ -293,7 +289,6 @@ class EventTarget {
handler = next;
}

this.#emitting.delete(event.type);
event[kTarget] = undefined;

return event.defaultPrevented === true ? false : true;
Expand Down
70 changes: 65 additions & 5 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
throws,
} = require('assert');

const { once } = require('events');
const { once, on } = require('events');

// The globals are defined.
ok(Event);
Expand Down Expand Up @@ -69,6 +69,24 @@ ok(EventTarget);
const ev = new Event('foo', {}, {});
strictEqual(ev.type, 'foo');
}
{
const ev = new Event('foo');
strictEqual(ev.cancelBubble, false);
ev.cancelBubble = true;
strictEqual(ev.cancelBubble, true);
}
{
const ev = new Event('foo');
strictEqual(ev.cancelBubble, false);
ev.stopPropagation();
strictEqual(ev.cancelBubble, true);
}
{
const ev = new Event('foo');
strictEqual(ev.cancelBubble, false);
ev.cancelBubble = 'some-truthy-value';
strictEqual(ev.cancelBubble, true);
}
{
const ev = new Event('foo', { cancelable: true });
strictEqual(ev.type, 'foo');
Expand Down Expand Up @@ -271,18 +289,18 @@ ok(EventTarget);

{
const eventTarget = new EventTarget();

const event = new Event('foo');
// Once handler only invoked once
const ev = common.mustCall((event) => {
throws(() => eventTarget.dispatchEvent(new Event('foo')), {
const ev = common.mustCall(() => {
throws(() => eventTarget.dispatchEvent(event), {
code: 'ERR_EVENT_RECURSION'
});
});

// Errors in a handler won't stop calling the others.
eventTarget.addEventListener('foo', ev);

eventTarget.dispatchEvent(new Event('foo'));
eventTarget.dispatchEvent(event);
}

{
Expand Down Expand Up @@ -439,3 +457,45 @@ ok(EventTarget);
const event = new Event('');
strictEqual(event.toString(), '[object Event]');
}
{
const target = new EventTarget();
const ev = new Event('toString');
const fn = common.mustCall((event) => strictEqual(event.type, 'toString'));
target.addEventListener('toString', fn);
target.dispatchEvent(ev);
}
{
const target = new EventTarget();
const ev = new Event('__proto__');
const fn = common.mustCall((event) => strictEqual(event.type, '__proto__'));
target.addEventListener('__proto__', fn);
target.dispatchEvent(ev);
}

(async () => {
// test NodeEventTarget async-iterability
const emitter = new NodeEventTarget();
const event = new Event('foo');
const interval = setInterval(() => emitter.dispatchEvent(event), 0);
let count = 0;
for await (const [ item ] of on(emitter, 'foo')) {
count++;
strictEqual(item.type, 'foo');
if (count > 5) {
break;
}
}
clearInterval(interval);
})().then(common.mustCall());
{
const target = new EventTarget();
const event = new Event('foo');
let callCount = 0;
target.addEventListener('foo', common.mustCall(() => {
callCount++;
if (callCount < 5) {
target.dispatchEvent(new Event('foo'));
}
}, 5));
target.dispatchEvent(event);
}