Skip to content

Commit

Permalink
fixup! fix error listener handler case
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamingr committed Jun 6, 2020
1 parent 0f2315c commit a62ff6d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,9 @@ function once(emitter, name) {
resolve(args);
};
eventTargetAgnosticAddListener(emitter, name, resolver, { once: true });
addErrorHandlerIfEventEmitter(emitter, errorListener, { once: true });
if (name !== 'error') {
addErrorHandlerIfEventEmitter(emitter, errorListener, { once: true });
}
});
}

Expand Down Expand Up @@ -733,7 +735,9 @@ function on(emitter, event) {
}, AsyncIteratorPrototype);

eventTargetAgnosticAddListener(emitter, event, eventHandler);
addErrorHandlerIfEventEmitter(emitter, errorHandler);
if (event !== 'error') {
addErrorHandlerIfEventEmitter(emitter, errorHandler);
}


return iterator;
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-event-on-async-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ async function eventTarget() {
clearInterval(interval);
}

async function errorListenerCount() {
const et = new EventEmitter();
on(et, 'foo');
assert.strictEqual(et.listenerCount('error'), 1);
}

async function nodeEventTarget() {
const et = new NodeEventTarget();
const tick = () => et.dispatchEvent(new Event('tick'));
Expand Down Expand Up @@ -252,6 +258,7 @@ async function run() {
nextError,
iterableThrow,
eventTarget,
errorListenerCount,
nodeEventTarget
];

Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-events-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ async function onceError() {
ee.emit('error', expected);
});

const [err] = await once(ee, 'error');
const promise = once(ee, 'error');
strictEqual(ee.listenerCount('error'), 1);
const [ err ] = await promise;
strictEqual(err, expected);
strictEqual(ee.listenerCount('error'), 0);
strictEqual(ee.listenerCount('myevent'), 0);
Expand Down

0 comments on commit a62ff6d

Please sign in to comment.