Skip to content

Commit

Permalink
listener removed and re-added during event dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
kobiburnley committed Feb 27, 2024
1 parent 2f8a730 commit 59f62af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/jsdom/living/events/EventTarget-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ function innerInvokeEventListeners(eventImpl, listeners, phase, itemInShadowTree
const { capture, once, passive } = listener.options;

// Check if the event listener has been removed since the listeners has been cloned.
if (!listeners[type].includes(listener)) {
if (!listeners[type].some(l => listener.callback.objectReference === l.callback.objectReference)) {
continue;
}

Expand Down
20 changes: 20 additions & 0 deletions test/to-port-to-wpts/level2/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ describe("level2/events", () => {
this.doc.dispatchEvent(event);
assert.equal(es.length, 1, 'should only be handled by one EventListener');
});

// Two listeners are registered on the same target, first listener removes and immediately adds the second listener, and an event is dispatched. Both listeners should see the event.
specify('listener removed and re-added during event dispatch', () => {
let h1 = 0, h2 = 0;
const handler1 = () => {
h1++;
this.doc.removeEventListener("foo", handler2, false);
this.doc.addEventListener("foo", handler2, false);
};
const handler2 = () => {
h2++;
};
this.doc.addEventListener("foo", handler1, false);
this.doc.addEventListener("foo", handler2, false);
var event = this.doc.createEvent("Events");
event.initEvent("foo",true,false);
this.doc.dispatchEvent(event);
assert.equal(h1, 1, "handler1 must be called once");
assert.equal(h2, 1, "handler2 must be called once");
})
})

// The Event.initEvent method is called for event returned by DocumentEvent.createEvent("Events")
Expand Down

0 comments on commit 59f62af

Please sign in to comment.