Skip to content
Open
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
8 changes: 8 additions & 0 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -2453,6 +2453,14 @@ target.removeEventListener('foo', handler, { capture: true });

<!-- YAML
added: v14.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/67765
description: Keep the event in its dispatching state across all listeners.
Re-dispatching the same event or calling `initEvent()` from any
listener while it is being dispatched now throws
`ERR_EVENT_RECURSION` or is ignored, respectively, not only in
the first listener.
-->

* `event` {Event}
Expand Down
103 changes: 51 additions & 52 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,64 +834,63 @@ class EventTarget {
let handler = root.next;
let next;

while (handler !== undefined &&
(root.resistStopPropagation || handler.passive || event?.[kStop] !== true)) {
// Cache the next item in case this iteration removes the current one
next = handler.next;

if (handler.removed || (event?.[kStop] === true && !handler.resistStopPropagation)) {
// Deal with the case an event is removed while event handlers are
// Being processed (removeEventListener called from a listener)
// And the case of event.stopImmediatePropagation() being called
// For events not flagged as resistStopPropagation
handler = next;
continue;
}
if (handler.once) {
handler.remove();
root.size--;
const { listener, capture } = handler;
this[kRemoveListener](root.size, type, listener, capture);
}

let arg;
try {
if (handler.isNodeStyleListener) {
arg = nodeValue;
} else {
if (event === undefined) {
event = this[kCreateEvent](nodeValue, type);
event[kTarget] = this;
event[kIsBeingDispatched] = true;
}
arg = event;
try {
while (handler !== undefined &&
(root.resistStopPropagation || handler.passive || event?.[kStop] !== true)) {
// Cache the next item in case this iteration removes the current one
next = handler.next;

if (handler.removed || (event?.[kStop] === true && !handler.resistStopPropagation)) {
// Deal with the case an event is removed while event handlers are
// Being processed (removeEventListener called from a listener)
// And the case of event.stopImmediatePropagation() being called
// For events not flagged as resistStopPropagation
handler = next;
continue;
}
const callback = handler.weak ?
handler.callback.deref() : handler.callback;
let result;
if (callback) {
if (handler.passive && !handler.isNodeStyleListener) {
arg[kInPassiveListener] = true;
if (handler.once) {
handler.remove();
root.size--;
const { listener, capture } = handler;
this[kRemoveListener](root.size, type, listener, capture);
}

let arg;
try {
if (handler.isNodeStyleListener) {
arg = nodeValue;
} else {
if (event === undefined) {
event = this[kCreateEvent](nodeValue, type);
event[kTarget] = this;
event[kIsBeingDispatched] = true;
}
arg = event;
}
result = FunctionPrototypeCall(callback, this, arg);
if (!handler.isNodeStyleListener) {
arg[kIsBeingDispatched] = false;
const callback = handler.weak ?
handler.callback.deref() : handler.callback;
let result;
if (callback) {
if (handler.passive && !handler.isNodeStyleListener) {
arg[kInPassiveListener] = true;
}
result = FunctionPrototypeCall(callback, this, arg);
}
if (result !== undefined && result !== null)
addCatch(result);
} catch (err) {
emitUncaughtException(err);
} finally {
if (arg?.[kInPassiveListener])
arg[kInPassiveListener] = false;
}
if (result !== undefined && result !== null)
addCatch(result);
} catch (err) {
emitUncaughtException(err);
} finally {
if (arg?.[kInPassiveListener])
arg[kInPassiveListener] = false;
}

handler = next;
handler = next;
}
} finally {
if (event !== undefined)
event[kIsBeingDispatched] = false;
}

if (event !== undefined)
event[kIsBeingDispatched] = false;
}

[kCreateEvent](nodeValue, type) {
Expand Down
82 changes: 82 additions & 0 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

const common = require('../common');
const {
NodeEventTarget,
defineEventHandler,
kRemoveListener,
kWeakHandler,
} = require('internal/event_target');

Expand Down Expand Up @@ -810,3 +812,83 @@ let asyncTest = Promise.resolve();
assert.strictEqual(get.call(value), false);
}
}

{
// Multiple listeners should all observe the event in its dispatching state.
const et = new EventTarget();
const checkListener = common.mustCall((e) => {
assert.strictEqual(e.currentTarget, et);
assert.strictEqual(e.eventPhase, Event.AT_TARGET);
assert.deepStrictEqual(e.composedPath(), [et]);
e.initEvent('bar', true, true);
assert.strictEqual(e.type, 'foo');
assert.strictEqual(e.bubbles, false);
assert.strictEqual(e.cancelable, false);
assert.throws(() => et.dispatchEvent(e), {
code: 'ERR_EVENT_RECURSION',
});
}, 2);

et.addEventListener('foo', checkListener);
et.addEventListener('foo', checkListener, { capture: true });

const event = new Event('foo');
et.dispatchEvent(event);
assert.strictEqual(event.currentTarget, null);
assert.strictEqual(event.eventPhase, Event.NONE);
assert.deepStrictEqual(event.composedPath(), []);
}

{
// NodeEventTarget mixed with standard, passive, and subsequent once listeners.
const net = new NodeEventTarget();
let capturedEvent;
const checkStandard = common.mustCall((e) => {
capturedEvent = e;
assert.strictEqual(e.currentTarget, net);
assert.strictEqual(e.eventPhase, Event.AT_TARGET);
assert.deepStrictEqual(e.composedPath(), [net]);
assert.strictEqual(e.detail, 'payload');
assert.throws(() => net.dispatchEvent(e), {
code: 'ERR_EVENT_RECURSION',
});
}, 3);

net.on('foo', common.mustCall((detail) => {
assert.strictEqual(detail, 'payload');
}));
net.addEventListener('foo', checkStandard);
net.addEventListener('foo', checkStandard, { passive: true, capture: true });
net.addEventListener('foo', common.mustCall((e) => {
checkStandard(e);
}), { once: true });

assert.strictEqual(net.emit('foo', 'payload'), true);
assert.strictEqual(capturedEvent.currentTarget, null);
assert.strictEqual(capturedEvent.eventPhase, Event.NONE);
assert.deepStrictEqual(capturedEvent.composedPath(), []);
}

{
// Throwing inside [kRemoveListener] for a once listener must not leave
// the event stuck in its dispatching state.
class ThrowingRemoveTarget extends EventTarget {
[kRemoveListener]() {
throw new Error('remove-boom');
}
}

const et = new ThrowingRemoveTarget();
et.addEventListener('foo', common.mustNotCall(), { once: true });
et.addEventListener('foo', common.mustNotCall());

const event = new Event('foo');
assert.throws(() => et.dispatchEvent(event), /remove-boom/);
assert.strictEqual(event.currentTarget, null);
assert.strictEqual(event.eventPhase, Event.NONE);
assert.deepStrictEqual(event.composedPath(), []);
// Can be dispatched again without ERR_EVENT_RECURSION.
const et2 = new EventTarget();
et2.addEventListener('foo', common.mustCall());
assert.strictEqual(et2.dispatchEvent(event), true);
}
Loading