Skip to content

Commit

Permalink
change order for listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Feb 27, 2021
1 parent 133faed commit 0f82983
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
14 changes: 7 additions & 7 deletions events.js
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,13 @@ function addErrorHandlerIfEventEmitter(emitter, handler, flags) {
}

function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
if (typeof emitter.addEventListener === 'function') {
if (typeof emitter.on === 'function') {
if (flags.once) {
emitter.once(name, listener);
} else {
emitter.on(name, listener);
}
} else if (typeof emitter.addEventListener === 'function') {
// EventTarget does not have `error` event semantics like Node
// EventEmitters, we do not listen for `error` events here.
emitter.addEventListener(name, function wrapListener(arg) {
Expand All @@ -485,12 +491,6 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
}
listener(arg);
});
} else if (typeof emitter.on === 'function') {
if (flags.once) {
emitter.once(name, listener);
} else {
emitter.on(name, listener);
}
} else {
throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type ' + typeof emitter);
}
Expand Down
13 changes: 12 additions & 1 deletion tests/events-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,23 @@ function onceWithEventTargetError() {
});
}

function prioritizesEventEmitter() {
var ee = new EventEmitter();
ee.addEventListener = assert.fail;
ee.removeAllListeners = assert.fail;
process.nextTick(function () {
ee.emit('foo');
});
return once(ee, 'foo');
}

module.exports = Promise.all([
onceAnEvent(),
onceAnEventWithTwoArgs(),
catchesErrors(),
stopListeningAfterCatchingError(),
onceError(),
onceWithEventTarget(),
onceWithEventTargetError()
onceWithEventTargetError(),
prioritizesEventEmitter()
]);

0 comments on commit 0f82983

Please sign in to comment.