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);
this._observe(gesture, callback as unknown as (data: GestureEventData) => void, thisArg);
} else { // Coerce "tap" -> GestureTypes.tap
const events = arg.split(','); // Coerce "loaded" -> undefined
if (events.length > 0) { const gesture: GestureTypes | undefined = gestureFromString(normalizedName);
for (let i = 0; i < events.length; i++) {
const evt = events[i].trim(); // If it's a gesture (and this Observable declares e.g. `static tapEvent`)
const gst = gestureFromString(evt); if (gesture && !this._isEvent(normalizedName)) {
if (gst && !this._isEvent(arg)) { this._observe(gesture, callback as unknown as (data: GestureEventData) => void, thisArg);
this._observe(gst, callback as unknown as (data: GestureEventData) => void, thisArg); return;
} else { }
super.addEventListener(evt, callback, thisArg);
} for (const eventName of normalizedName.trim().split(eventNamesRegex)) {
} super.addEventListener(eventName, callback, thisArg);
} else {
super.addEventListener(arg, 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;
this._disconnectGestureObservers(gesture); }
} else {
const events = arg.split(','); // Normalize "ontap" -> "tap"
if (events.length > 0) { const normalizedName = getEventOrGestureName(arg);
for (let i = 0; i < events.length; i++) {
const evt = events[i].trim(); // Coerce "tap" -> GestureTypes.tap
const gst = gestureFromString(evt); // Coerce "loaded" -> undefined
if (gst && !this._isEvent(arg)) { const gesture: GestureTypes | undefined = gestureFromString(normalizedName);
this._disconnectGestureObservers(gst);
} else { // If it's a gesture (and this Observable declares e.g. `static tapEvent`)
super.removeEventListener(evt, callback, thisArg); if (gesture && !this._isEvent(normalizedName)) {
} this._disconnectGestureObservers(gesture);
} return;
} else { }
super.removeEventListener(arg, callback, thisArg);
} for (const eventName of normalizedName.trim().split(eventNamesRegex)) {
} super.removeEventListener(eventName, callback, thisArg);
} else if (typeof arg === 'number') {
this._disconnectGestureObservers(<GestureTypes>arg);
} }
} }
@ -379,7 +373,7 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
const firstArgument = args[0]; const firstArgument = args[0];
const view = firstArgument instanceof ViewCommon ? firstArgument : <ViewCommon>Builder.createViewFromEntry({ const view = firstArgument instanceof ViewCommon ? firstArgument : <ViewCommon>Builder.createViewFromEntry({
moduleName: firstArgument, moduleName: firstArgument,
}); });
return { view, options }; return { view, options };
} }