fix: declare reset on prototype

This commit is contained in:
shirakaba
2022-12-20 18:46:50 +09:00
parent 96e53c6a43
commit 13c29db06e

View File

@@ -271,6 +271,20 @@ export class DOMEvent implements Event {
this.listenersLive.onMutation = null; this.listenersLive.onMutation = null;
} }
/**
* Resets any internal state to allow the event to be redispatched. Call
* this before returning from dispatchTo().
*/
// Declaring this on the prototype rather than as an arrow function saves
// 190 nanoseconds per dispatchTo().
private resetForRedispatch() {
this.currentTarget = null;
this.target = null;
this.eventPhase = this.NONE;
this.propagationState = EventPropagationState.resume;
this.listenersLive = emptyArray;
this.listenersLazyCopy = emptyArray;
}
/** /**
* Dispatches a synthetic event event to target and returns true if either * Dispatches a synthetic event event to target and returns true if either
* event's cancelable attribute value is false or its preventDefault() * event's cancelable attribute value is false or its preventDefault()
@@ -288,19 +302,6 @@ export class DOMEvent implements Event {
// completed the breaking changes to migrate fully to DOMEvents. // completed the breaking changes to migrate fully to DOMEvents.
DOMEvent.unstable_currentEvent = this; DOMEvent.unstable_currentEvent = this;
/**
* Resets any internal state to allow the event to be redispatched. Call
* this before returning.
*/
const reset = () => {
this.currentTarget = null;
this.target = null;
this.eventPhase = this.NONE;
this.propagationState = EventPropagationState.resume;
this.listenersLive = emptyArray;
this.listenersLazyCopy = emptyArray;
};
// `Observable.removeEventListener` would likely suffice, but grabbing // `Observable.removeEventListener` would likely suffice, but grabbing
// the static method named `removeEventListener` on the target's class // the static method named `removeEventListener` on the target's class
// allows us to be robust to the possiblity of the case of the target // allows us to be robust to the possiblity of the case of the target
@@ -348,7 +349,7 @@ export class DOMEvent implements Event {
}); });
if (this.propagationState !== EventPropagationState.resume) { if (this.propagationState !== EventPropagationState.resume) {
reset(); this.resetForRedispatch();
return !this.defaultPrevented; return !this.defaultPrevented;
} }
} }
@@ -368,7 +369,7 @@ export class DOMEvent implements Event {
}); });
if (this.propagationState !== EventPropagationState.resume) { if (this.propagationState !== EventPropagationState.resume) {
reset(); this.resetForRedispatch();
return !this.defaultPrevented; return !this.defaultPrevented;
} }
@@ -376,7 +377,7 @@ export class DOMEvent implements Event {
// target (the first iteration of this loop) we don't let it // target (the first iteration of this loop) we don't let it
// propagate any further. // propagate any further.
if (!this.bubbles) { if (!this.bubbles) {
reset(); this.resetForRedispatch();
break; break;
} }
@@ -393,7 +394,7 @@ export class DOMEvent implements Event {
phase: this.BUBBLING_PHASE, phase: this.BUBBLING_PHASE,
}); });
reset(); this.resetForRedispatch();
return !this.defaultPrevented; return !this.defaultPrevented;
} }