chore(core): clean up addEventListener() and removeEventListener() in ViewCommon

This commit is contained in:
shirakaba
2024-05-01 16:31:34 +09:00
parent dab9898030
commit 02e4d812c2
2 changed files with 42 additions and 48 deletions

View File

@ -89,7 +89,7 @@ const _globalEventHandlers: {
}; };
} = {}; } = {};
const eventNamesRegex = /\s*,\s*/; export const eventNamesRegex = /\s*,\s*/;
/** /**
* Observable is used when you want to be notified when a change occurs. Use on/off methods to add/remove listener. * Observable is used when you want to be notified when a change occurs. Use on/off methods to add/remove listener.

View File

@ -8,7 +8,7 @@ import { isObject } from '../../../utils/types';
import { sanitizeModuleName } from '../../../utils/common'; import { sanitizeModuleName } from '../../../utils/common';
import { Color } from '../../../color'; import { Color } from '../../../color';
import { Property, InheritedProperty } from '../properties'; import { Property, InheritedProperty } from '../properties';
import { EventData } from '../../../data/observable'; import { EventData, eventNamesRegex } from '../../../data/observable';
import { Trace } from '../../../trace'; import { Trace } from '../../../trace';
import { CoreTypes } from '../../../core-types'; import { CoreTypes } from '../../../core-types';
import { ViewHelper } from './view-helper'; import { ViewHelper } from './view-helper';
@ -291,56 +291,50 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
} }
public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) { public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) {
if (typeof arg === 'string') { if (typeof arg === 'number') {
arg = getEventOrGestureName(arg); this._observe(arg, callback as unknown as (data: GestureEventData) => void, thisArg);
return;
}
const gesture = gestureFromString(arg); // Normalize "ontap" -> "tap"
if (gesture && !this._isEvent(arg)) { const normalizedName = getEventOrGestureName(arg);
// Coerce "tap" -> GestureTypes.tap
// Coerce "loaded" -> undefined
const gesture: GestureTypes | undefined = gestureFromString(normalizedName);
// If it's a gesture (and this Observable declares e.g. `static tapEvent`)
if (gesture && !this._isEvent(normalizedName)) {
this._observe(gesture, callback as unknown as (data: GestureEventData) => void, thisArg); this._observe(gesture, callback as unknown as (data: GestureEventData) => void, thisArg);
} else { return;
const events = arg.split(',');
if (events.length > 0) {
for (let i = 0; i < events.length; i++) {
const evt = events[i].trim();
const gst = gestureFromString(evt);
if (gst && !this._isEvent(arg)) {
this._observe(gst, callback as unknown as (data: GestureEventData) => void, thisArg);
} else {
super.addEventListener(evt, callback, thisArg);
} }
}
} else { for (const eventName of normalizedName.trim().split(eventNamesRegex)) {
super.addEventListener(arg, callback, thisArg); super.addEventListener(eventName, callback, thisArg);
}
}
} else if (typeof arg === 'number') {
this._observe(<GestureTypes>arg, callback as unknown as (data: GestureEventData) => void, thisArg);
} }
} }
public removeEventListener(arg: string | GestureTypes, callback?: (data: EventData) => void, thisArg?: any) { public removeEventListener(arg: string | GestureTypes, callback?: (data: EventData) => void, thisArg?: any) {
if (typeof arg === 'string') { if (typeof arg === 'number') {
const gesture = gestureFromString(arg); this._disconnectGestureObservers(arg);
if (gesture && !this._isEvent(arg)) { return;
}
// Normalize "ontap" -> "tap"
const normalizedName = getEventOrGestureName(arg);
// Coerce "tap" -> GestureTypes.tap
// Coerce "loaded" -> undefined
const gesture: GestureTypes | undefined = gestureFromString(normalizedName);
// If it's a gesture (and this Observable declares e.g. `static tapEvent`)
if (gesture && !this._isEvent(normalizedName)) {
this._disconnectGestureObservers(gesture); this._disconnectGestureObservers(gesture);
} else { return;
const events = arg.split(',');
if (events.length > 0) {
for (let i = 0; i < events.length; i++) {
const evt = events[i].trim();
const gst = gestureFromString(evt);
if (gst && !this._isEvent(arg)) {
this._disconnectGestureObservers(gst);
} else {
super.removeEventListener(evt, callback, thisArg);
} }
}
} else { for (const eventName of normalizedName.trim().split(eventNamesRegex)) {
super.removeEventListener(arg, callback, thisArg); super.removeEventListener(eventName, callback, thisArg);
}
}
} else if (typeof arg === 'number') {
this._disconnectGestureObservers(<GestureTypes>arg);
} }
} }