From 0c32920a82a00a6f338a021a0e17dd5e1c7809f8 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 25 Jun 2020 23:43:30 +0200 Subject: [PATCH] events: fix add-remove-add case in EventTarget Make sure that listeners are added properly if there has previously been one but currently are none for a given event type. PR-URL: https://github.com/nodejs/node/pull/34056 Reviewed-By: James M Snell Reviewed-By: Rich Trott --- lib/internal/event_target.js | 2 +- test/parallel/test-eventtarget-once-twice.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-eventtarget-once-twice.js diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 35d9e7275af2d0..983e2671b7bb6e 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -240,7 +240,7 @@ class EventTarget { } let handler = root.next; - let previous; + let previous = root; // We have to walk the linked list to see if we have a match while (handler !== undefined && !handler.same(listener, capture)) { diff --git a/test/parallel/test-eventtarget-once-twice.js b/test/parallel/test-eventtarget-once-twice.js new file mode 100644 index 00000000000000..6a38b80e2db75a --- /dev/null +++ b/test/parallel/test-eventtarget-once-twice.js @@ -0,0 +1,19 @@ +// Flags: --expose-internals --no-warnings +'use strict'; +const common = require('../common'); +const { + Event, + EventTarget, +} = require('internal/event_target'); +const { once } = require('events'); + +const et = new EventTarget(); +(async function() { + await once(et, 'foo'); + await once(et, 'foo'); +})().then(common.mustCall()); + +et.dispatchEvent(new Event('foo')); +setImmediate(() => { + et.dispatchEvent(new Event('foo')); +});