feat(core): implement EventListenerOptions for addEventListener and removeEventListener

This commit is contained in:
shirakaba
2024-05-08 12:19:49 +09:00
parent 5d08e44b79
commit 05a6be2e3d
9 changed files with 83 additions and 58 deletions

View File

@@ -297,11 +297,11 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
return this._gestureObservers[type];
}
public addEventListener(eventNames: string, callback: (data: EventData) => void, thisArg?: any) {
public addEventListener(eventName: string, callback: (data: EventData) => void, options?: AddEventListenerOptions | boolean, thisArg?: any) {
thisArg = thisArg || undefined;
// Normalize "ontap" -> "tap"
const normalizedName = getEventOrGestureName(eventNames);
const normalizedName = getEventOrGestureName(eventName);
// Coerce "tap" -> GestureTypes.tap
// Coerce "loaded" -> undefined
@@ -313,14 +313,14 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
return;
}
super.addEventListener(normalizedName, callback, thisArg);
super.addEventListener(normalizedName, callback, options, thisArg);
}
public removeEventListener(eventNames: string, callback?: (data: EventData) => void, thisArg?: any) {
public removeEventListener(eventName: string, callback?: (data: EventData) => void, options?: EventListenerOptions | boolean, thisArg?: any) {
thisArg = thisArg || undefined;
// Normalize "ontap" -> "tap"
const normalizedName = getEventOrGestureName(eventNames);
const normalizedName = getEventOrGestureName(eventName);
// Coerce "tap" -> GestureTypes.tap
// Coerce "loaded" -> undefined
@@ -332,7 +332,7 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
return;
}
super.removeEventListener(normalizedName, callback, thisArg);
super.removeEventListener(normalizedName, callback, options, thisArg);
}
public onBackPressed(): boolean {

View File

@@ -21,7 +21,7 @@ function getHandlerForEventName(eventName: string): (eventData: EventData) => vo
const sourceEventMap = sourcesMap.get(source);
if (!sourceEventMap) {
// There is no event map for this source - it is safe to detach the listener;
source.removeEventListener(eventName, handlersForEventName.get(eventName));
source.off(eventName, handlersForEventName.get(eventName));
return;
}
@@ -46,7 +46,7 @@ function getHandlerForEventName(eventName: string): (eventData: EventData) => vo
if (deadPairsIndexes.length === targetHandlerPairList.length) {
// There are no alive targets for this event - unsubscribe
source.removeEventListener(eventName, handlersForEventName.get(eventName));
source.off(eventName, handlersForEventName.get(eventName));
sourceEventMap.delete(eventName);
} else {
for (let j = deadPairsIndexes.length - 1; j >= 0; j--) {

View File

@@ -17,10 +17,10 @@ export abstract class ScrollViewBase extends ContentView implements ScrollViewDe
private _addedScrollEvent = false;
public addEventListener(arg: string, callback: (data: EventData) => void, thisArg?: any): void {
public addEventListener(arg: string, callback: (data: EventData) => void, options?: AddEventListenerOptions | boolean, thisArg?: any): void {
const hasExistingScrollListeners: boolean = this.hasListeners(ScrollViewBase.scrollEvent);
super.addEventListener(arg, callback, thisArg);
super.addEventListener(arg, callback, options, thisArg);
// This indicates that a scroll listener was added for first time
if (!hasExistingScrollListeners && this.hasListeners(ScrollViewBase.scrollEvent)) {
@@ -32,10 +32,10 @@ export abstract class ScrollViewBase extends ContentView implements ScrollViewDe
}
}
public removeEventListener(arg: string, callback?: (data: EventData) => void, thisArg?: any): void {
public removeEventListener(arg: string, callback?: (data: EventData) => void, options?: EventListenerOptions | boolean, thisArg?: any): void {
const hasExistingScrollListeners: boolean = this.hasListeners(ScrollViewBase.scrollEvent);
super.removeEventListener(arg, callback, thisArg);
super.removeEventListener(arg, callback, options, thisArg);
// This indicates that the final scroll listener was removed
if (hasExistingScrollListeners && !this.hasListeners(ScrollViewBase.scrollEvent)) {

View File

@@ -14,7 +14,7 @@ export class FormattedString extends ViewBase implements FormattedStringDefiniti
constructor() {
super();
this._spans = new ObservableArray<Span>();
this._spans.addEventListener(ObservableArray.changeEvent, this.onSpansCollectionChanged, this);
this._spans.addEventListener(ObservableArray.changeEvent, this.onSpansCollectionChanged, false, this);
}
get fontFamily(): string {

View File

@@ -109,13 +109,13 @@ export class Span extends ViewBase implements SpanDefinition {
return this._tappable;
}
addEventListener(arg: string, callback: (data: EventData) => void, thisArg?: any): void {
super.addEventListener(arg, callback, thisArg);
addEventListener(arg: string, callback: (data: EventData) => void, options?: AddEventListenerOptions | boolean, thisArg?: any): void {
super.addEventListener(arg, callback, options, thisArg);
this._setTappable(this.hasListeners(Span.linkTapEvent));
}
removeEventListener(arg: string, callback?: (data: EventData) => void, thisArg?: any): void {
super.removeEventListener(arg, callback, thisArg);
removeEventListener(arg: string, callback?: (data: EventData) => void, options?: EventListenerOptions | boolean, thisArg?: any): void {
super.removeEventListener(arg, callback, options, thisArg);
this._setTappable(this.hasListeners(Span.linkTapEvent));
}