(
arg0: K | EventRegistryObject<EventSpec>,
handler?: TEventCallback<E>
)
| 26 | ): VoidFunction; |
| 27 | on(handlers: EventRegistryObject<EventSpec>): VoidFunction; |
| 28 | on<K extends keyof EventSpec, E extends EventSpec[K]>( |
| 29 | arg0: K | EventRegistryObject<EventSpec>, |
| 30 | handler?: TEventCallback<E> |
| 31 | ): VoidFunction { |
| 32 | if (!this.__eventListeners) { |
| 33 | this.__eventListeners = {} as Record<keyof EventSpec, TEventCallback[]>; |
| 34 | } |
| 35 | if (typeof arg0 === 'object') { |
| 36 | // one object with key/value pairs was passed |
| 37 | Object.entries(arg0).forEach(([eventName, handler]) => { |
| 38 | this.on(eventName as K, handler as TEventCallback); |
| 39 | }); |
| 40 | return () => this.off(arg0); |
| 41 | } else if (handler) { |
| 42 | const eventName = arg0; |
| 43 | if (!this.__eventListeners[eventName]) { |
| 44 | this.__eventListeners[eventName] = []; |
| 45 | } |
| 46 | this.__eventListeners[eventName].push(handler); |
| 47 | return () => this.off(eventName, handler); |
| 48 | } else { |
| 49 | // noop |
| 50 | return () => false; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Observes specified event **once** |
no test coverage detected