Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: refactor to use more primordials #36304

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
fixup! events: refactor to use more primordials
This reverts commit e3d0bb1.
  • Loading branch information
aduh95 committed Dec 18, 2020
commit 7a158a243d8ac30ca242c0f76796fb9ff56d3356
23 changes: 16 additions & 7 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,22 @@ EventEmitter.prototype.prependListener =
return _addListener(this, type, listener, true);
};

function _onceWrap(target, type, listener) {
return function onceWrapper() {
target.removeListener(type, onceWrapper);
function onceWrapper() {
if (!this.fired) {
this.target.removeListener(this.type, this.wrapFn);
this.fired = true;
if (arguments.length === 0)
return FunctionPrototypeCall(listener, target);
return ReflectApply(listener, target, arguments);
};
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;
}

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

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