Skip to content

Commit

Permalink
events: simplify event target agnostic logic in on and once
Browse files Browse the repository at this point in the history
PR-URL: #34997
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
  • Loading branch information
lundibundi committed Sep 11, 2020
1 parent c66e647 commit b1831fe
Showing 1 changed file with 10 additions and 36 deletions.
46 changes: 10 additions & 36 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,11 +642,7 @@ async function once(emitter, name, options = {}) {
const errorListener = (err) => {
emitter.removeListener(name, resolver);
if (signal != null) {
eventTargetAgnosticRemoveListener(
signal,
'abort',
abortListener,
{ once: true });
eventTargetAgnosticRemoveListener(signal, 'abort', abortListener);
}
reject(err);
};
Expand All @@ -655,38 +651,22 @@ async function once(emitter, name, options = {}) {
emitter.removeListener('error', errorListener);
}
if (signal != null) {
eventTargetAgnosticRemoveListener(
signal,
'abort',
abortListener,
{ once: true });
eventTargetAgnosticRemoveListener(signal, 'abort', abortListener);
}
resolve(args);
};
eventTargetAgnosticAddListener(emitter, name, resolver, { once: true });
if (name !== 'error') {
addErrorHandlerIfEventEmitter(emitter, errorListener, { once: true });
if (name !== 'error' && typeof emitter.once === 'function') {
emitter.once('error', errorListener);
}
function abortListener() {
if (typeof emitter.removeListener === 'function') {
emitter.removeListener(name, resolver);
emitter.removeListener('error', errorListener);
} else {
eventTargetAgnosticRemoveListener(
emitter,
name,
resolver,
{ once: true });
eventTargetAgnosticRemoveListener(
emitter,
'error',
errorListener,
{ once: true });
}
eventTargetAgnosticRemoveListener(emitter, name, resolver);
eventTargetAgnosticRemoveListener(emitter, 'error', resolver);
reject(lazyDOMException('The operation was aborted', 'AbortError'));
}
if (signal != null) {
signal.addEventListener('abort', abortListener, { once: true });
eventTargetAgnosticAddListener(
signal, 'abort', abortListener, { once: true });
}
});
}
Expand All @@ -698,12 +678,6 @@ function createIterResult(value, done) {
return { value, done };
}

function addErrorHandlerIfEventEmitter(emitter, handler, flags) {
if (typeof emitter.on === 'function') {
eventTargetAgnosticAddListener(emitter, 'error', handler, flags);
}
}

function eventTargetAgnosticRemoveListener(emitter, name, listener, flags) {
if (typeof emitter.removeListener === 'function') {
emitter.removeListener(name, listener);
Expand Down Expand Up @@ -808,8 +782,8 @@ function on(emitter, event, options) {
}, AsyncIteratorPrototype);

eventTargetAgnosticAddListener(emitter, event, eventHandler);
if (event !== 'error') {
addErrorHandlerIfEventEmitter(emitter, errorHandler);
if (event !== 'error' && typeof emitter.on === 'function') {
emitter.on('error', errorHandler);
}

if (signal) {
Expand Down

0 comments on commit b1831fe

Please sign in to comment.