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: allow an event to be dispatched multiple times #39395

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
18 changes: 13 additions & 5 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const {
} = EventEmitter;

const kEvents = Symbol('kEvents');
const kIsBeingDispatched = Symbol('kIsBeingDispatched');
const kStop = Symbol('kStop');
const kTarget = Symbol('kTarget');
const kHandlers = Symbol('khandlers');
Expand Down Expand Up @@ -105,6 +106,7 @@ class Event {
configurable: false
});
this[kTarget] = null;
this[kIsBeingDispatched] = false;
}

[customInspectSymbol](depth, options) {
Expand Down Expand Up @@ -151,12 +153,12 @@ class Event {
// These are not supported in Node.js and are provided purely for
// API completeness.

composedPath() { return this[kTarget] ? [this[kTarget]] : []; }
composedPath() { return this[kIsBeingDispatched] ? [this[kTarget]] : []; }
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated to this PR: Chromium and Safari both return an empty array when dispatching an event. Firefox has the same behavior as Node.js. I couldn't find which behavior is spec compliant.

{
  // Same event dispatched multiple times.
  const event = new Event('foo');
  const eventTarget1 = new EventTarget();
  const eventTarget2 = new EventTarget();

  eventTarget1.addEventListener('foo', ((event) => {
    console.log(event.target===eventTarget1, event.eventPhase===Event.AT_TARGET); // true true
    const path = event.composedPath();
    console.log(path.length === 1, path[0]===eventTarget1); // depends on the browser:
    // On Firefox + Node.js: true true
    // On Safari + Chromium : false false
  }));

  eventTarget2.addEventListener('foo', ((event) => {
    console.log(event.target===eventTarget2, event.eventPhase===Event.AT_TARGET); // true true
    const path = event.composedPath();
    console.log(path.length === 1, path[0]===eventTarget2); // depends on the browser
  }));

  eventTarget1.dispatchEvent(event);
  console.log(event.target===eventTarget1, event.eventPhase===Event.NONE); // true true
  console.log(event.composedPath().length === 0); // true

  eventTarget2.dispatchEvent(event);
  console.log(event.target===eventTarget2, event.eventPhase===Event.NONE); // true true
  console.log(event.composedPath().length === 0); // true
}

get returnValue() { return !this.defaultPrevented; }
get bubbles() { return this[kBubbles]; }
get composed() { return this[kComposed]; }
get eventPhase() {
return this[kTarget] ? Event.AT_TARGET : Event.NONE;
return this[kIsBeingDispatched] ? Event.AT_TARGET : Event.NONE;
}
get cancelBubble() { return this[kPropagationStopped]; }
set cancelBubble(value) {
Expand Down Expand Up @@ -397,7 +399,7 @@ class EventTarget {
if (!isEventTarget(this))
throw new ERR_INVALID_THIS('EventTarget');

if (event[kTarget] !== null)
if (event[kIsBeingDispatched])
throw new ERR_EVENT_RECURSION(event.type);

this[kHybridDispatch](event, event.type, event);
Expand All @@ -410,11 +412,14 @@ class EventTarget {
if (event === undefined) {
event = this[kCreateEvent](nodeValue, type);
event[kTarget] = this;
event[kIsBeingDispatched] = true;
}
return event;
};
if (event !== undefined)
if (event !== undefined) {
event[kTarget] = this;
event[kIsBeingDispatched] = true;
}

const root = this[kEvents].get(type);
if (root === undefined || root.next === undefined)
Expand Down Expand Up @@ -453,6 +458,9 @@ class EventTarget {
let result;
if (callback) {
result = FunctionPrototypeCall(callback, this, arg);
if (!handler.isNodeStyleListener) {
arg[kIsBeingDispatched] = false;
}
}
if (result !== undefined && result !== null)
addCatch(result);
Expand All @@ -464,7 +472,7 @@ class EventTarget {
}

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

[kCreateEvent](nodeValue, type) {
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,36 @@ let asyncTest = Promise.resolve();
eventTarget.dispatchEvent(ev);
}

{
// Same event dispatched multiple times.
const event = new Event('foo');
const eventTarget1 = new EventTarget();
const eventTarget2 = new EventTarget();

eventTarget1.addEventListener('foo', common.mustCall((event) => {
strictEqual(event.eventPhase, Event.AT_TARGET);
strictEqual(event.target, eventTarget1);
deepStrictEqual(event.composedPath(), [eventTarget1]);
}));

eventTarget2.addEventListener('foo', common.mustCall((event) => {
strictEqual(event.eventPhase, Event.AT_TARGET);
strictEqual(event.target, eventTarget2);
deepStrictEqual(event.composedPath(), [eventTarget2]);
}));

eventTarget1.dispatchEvent(event);
strictEqual(event.eventPhase, Event.NONE);
strictEqual(event.target, eventTarget1);
deepStrictEqual(event.composedPath(), []);


eventTarget2.dispatchEvent(event);
strictEqual(event.eventPhase, Event.NONE);
strictEqual(event.target, eventTarget2);
deepStrictEqual(event.composedPath(), []);
}

{
const eventTarget = new EventTarget();
const event = new Event('foo', { cancelable: true });
Expand Down