From 2ebb629f8ec2d7eda99202cecb10756841b89e2b Mon Sep 17 00:00:00 2001 From: Tom Van Laerhoven Date: Tue, 17 Dec 2024 12:53:34 +0100 Subject: [PATCH] Properly support passing an array of events for a listener --- .../adapter/event/DefaultEventDispatcher.ts | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/internal/adapter/event/DefaultEventDispatcher.ts b/src/internal/adapter/event/DefaultEventDispatcher.ts index 56ffba07c..96c0dab94 100644 --- a/src/internal/adapter/event/DefaultEventDispatcher.ts +++ b/src/internal/adapter/event/DefaultEventDispatcher.ts @@ -1,17 +1,25 @@ -import type { EventDispatcher, EventMap, StringKeyOf } from '../../../api/event/EventDispatcher'; -import type { EventListener } from '../../../api/event/EventListener'; +import type { EventDispatcher, EventMap, StringKeyOf } from 'react-native-theoplayer'; +import type { EventListener } from 'react-native-theoplayer'; import { arrayRemoveElement } from '../../utils/arrayUtil'; export class DefaultEventDispatcher>> implements EventDispatcher { readonly _eventListeners: Map, EventListener]>[]> = new Map(); - addEventListener>(type: K, listener: EventListener): void { + addEventListener>(type: K | readonly K[], listener: EventListener): void { + if (typeof listener !== 'function') { + return; + } else if (Array.isArray(type)) { + type.forEach((t) => this.addSingleEventListener(t, listener)); + } else { + this.addSingleEventListener(type as K, listener); + } + } + + private addSingleEventListener>(type: K, listener: EventListener): void { if (!this._eventListeners.has(type)) { - // @ts-ignore - this._eventListeners.set(type, [listener]); + this._eventListeners.set(type, [listener as EventListener]>]); } else { - // @ts-ignore - this._eventListeners.get(type)?.push(listener); + this._eventListeners.get(type)?.push(listener as EventListener]>); } } @@ -26,7 +34,17 @@ export class DefaultEventDispatcher>> im } }; - removeEventListener>(type: K, listener: EventListener): void { + removeEventListener>(type: K | readonly K[], listener: EventListener): void { + if (typeof listener !== 'function') { + return; + } else if (Array.isArray(type)) { + type.forEach((t) => this.removeSingleEventListener(t, listener)); + } else { + this.removeSingleEventListener(type as K, listener); + } + } + + private removeSingleEventListener>(type: K, listener: EventListener): void { const listeners = this._eventListeners.get(type); if (listeners) { arrayRemoveElement(listeners, listener);