Skip to content

Commit

Permalink
fixup! events: refactor to use more primordials
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Dec 18, 2020
1 parent 69ff9a3 commit e3d0bb1
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,22 +486,13 @@ EventEmitter.prototype.prependListener =
return _addListener(this, type, listener, true);
};

function onceWrapper() {
if (!this.fired) {
this.target.removeListener(this.type, this.wrapFn);
this.fired = true;
if (arguments.length === 0)
return FunctionPrototypeCall(this.listener, this.target);
return ReflectApply(this.listener, this.target, arguments);
}
}

function _onceWrap(target, type, listener) {
const state = { fired: false, wrapFn: undefined, target, type, listener };
const wrapped = FunctionPrototypeBind(onceWrapper, state);
wrapped.listener = listener;
state.wrapFn = wrapped;
return wrapped;
return function onceWrapper() {
target.removeListener(type, onceWrapper);
if (arguments.length === 0)
return FunctionPrototypeCall(listener, target);
return ReflectApply(listener, target, arguments);
};
}

EventEmitter.prototype.once = function once(type, listener) {
Expand Down Expand Up @@ -789,7 +780,7 @@ function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
}

function on(emitter, event, options) {
const { signal } = { ...options };
const signal = options?.signal;
validateAbortSignal(signal, 'options.signal');
if (signal && signal.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
Expand Down

0 comments on commit e3d0bb1

Please sign in to comment.