Repository: NativeScript/NativeScript
File: packages/core/ui/core/view/view-common.ts (TypeScript)

Code before:
	}

	public getGestureObservers(type: GestureTypes): Array<GesturesObserver> | undefined {
		return this._gestureObservers[type];
	}

	public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) {
		if (typeof arg === 'number') {
			this._observe(arg, callback as unknown as (data: GestureEventData) => void, thisArg);
			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._observe(gesture, callback as unknown as (data: GestureEventData) => void, thisArg);
			return;
		}

		for (const eventName of normalizedName.trim().split(eventNamesRegex)) {
			super.addEventListener(eventName, callback, thisArg);
		}
	}

	public removeEventListener(arg: string | GestureTypes, callback?: (data: EventData) => void, thisArg?: any) {
		if (typeof arg === 'number') {
			this._disconnectGestureObservers(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);
			return;
		}

		for (const eventName of normalizedName.trim().split(eventNamesRegex)) {
			super.removeEventListener(eventName, callback, thisArg);

Review comment (suggestion):
Yes, this is redundant because Observable will do the same thing all over again. I've focused just on cleanup here, and in a follow-up PR it would be nice to investigate removing event-name splitting in Observable altogether (in which case we'd actually want to keep this line in place). Hence, I've decided not to remove it just yet.

Code after:
	public getGestureObservers(type: GestureTypes): Array<GesturesObserver> | undefined {
		return this._gestureObservers[type];
	}

	public addEventListener(arg: string | GestureTypes, callback: (data: EventData) => void, thisArg?: any) {
		if (typeof arg === 'number') {
			this._observe(arg, callback as unknown as (data: GestureEventData) => void, thisArg);
			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._observe(gesture, callback as unknown as (data: GestureEventData) => void, thisArg);
			return;
		}

		super.addEventListener(normalizedName, callback, thisArg);
	}

	public removeEventListener(arg: string | GestureTypes, callback?: (data: EventData) => void, thisArg?: any) {
		if (typeof arg === 'number') {
			this._disconnectGestureObservers(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);
			return;
		}

		super.removeEventListener(normalizedName, callback, thisArg);
	}

	public onBackPressed(): boolean {
		return false;
	}