|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { assert } from '@firebase/util'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Base class to be used if you want to emit events. Call the constructor with |
| 22 | + * the set of allowed event names. |
| 23 | + */ |
| 24 | +export abstract class EventEmitter { |
| 25 | + private listeners_: { |
| 26 | + [eventType: string]: Array<{ |
| 27 | + callback(...args: unknown[]): void; |
| 28 | + context: unknown; |
| 29 | + }>; |
| 30 | + } = {}; |
| 31 | + |
| 32 | + constructor(private allowedEvents_: string[]) { |
| 33 | + assert( |
| 34 | + Array.isArray(allowedEvents_) && allowedEvents_.length > 0, |
| 35 | + 'Requires a non-empty array' |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * To be overridden by derived classes in order to fire an initial event when |
| 41 | + * somebody subscribes for data. |
| 42 | + * |
| 43 | + * @returns {Array.<*>} Array of parameters to trigger initial event with. |
| 44 | + */ |
| 45 | + abstract getInitialEvent(eventType: string): unknown[]; |
| 46 | + |
| 47 | + /** |
| 48 | + * To be called by derived classes to trigger events. |
| 49 | + */ |
| 50 | + protected trigger(eventType: string, ...varArgs: unknown[]) { |
| 51 | + if (Array.isArray(this.listeners_[eventType])) { |
| 52 | + // Clone the list, since callbacks could add/remove listeners. |
| 53 | + const listeners = [...this.listeners_[eventType]]; |
| 54 | + |
| 55 | + for (let i = 0; i < listeners.length; i++) { |
| 56 | + listeners[i].callback.apply(listeners[i].context, varArgs); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + on(eventType: string, callback: (a: unknown) => void, context: unknown) { |
| 62 | + this.validateEventType_(eventType); |
| 63 | + this.listeners_[eventType] = this.listeners_[eventType] || []; |
| 64 | + this.listeners_[eventType].push({ callback, context }); |
| 65 | + |
| 66 | + const eventData = this.getInitialEvent(eventType); |
| 67 | + if (eventData) { |
| 68 | + callback.apply(context, eventData); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + off(eventType: string, callback: (a: unknown) => void, context: unknown) { |
| 73 | + this.validateEventType_(eventType); |
| 74 | + const listeners = this.listeners_[eventType] || []; |
| 75 | + for (let i = 0; i < listeners.length; i++) { |
| 76 | + if ( |
| 77 | + listeners[i].callback === callback && |
| 78 | + (!context || context === listeners[i].context) |
| 79 | + ) { |
| 80 | + listeners.splice(i, 1); |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private validateEventType_(eventType: string) { |
| 87 | + assert( |
| 88 | + this.allowedEvents_.find(et => { |
| 89 | + return et === eventType; |
| 90 | + }), |
| 91 | + 'Unknown event: ' + eventType |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
0 commit comments