| 43 | * @internal |
| 44 | */ |
| 45 | export class EventEmitter implements IEventEmitter { |
| 46 | private _listeners: (() => void)[] = []; |
| 47 | private readonly _fireOnRegister: (() => boolean) | undefined; |
| 48 | |
| 49 | public constructor(fireOnRegister: (() => boolean) | undefined = undefined) { |
| 50 | this._fireOnRegister = fireOnRegister; |
| 51 | } |
| 52 | |
| 53 | public on(value: () => void): () => void { |
| 54 | this._listeners.push(value); |
| 55 | if (this._fireOnRegister?.()) { |
| 56 | value(); |
| 57 | } |
| 58 | return () => { |
| 59 | this.off(value); |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | public off(value: () => void): void { |
| 64 | this._listeners = this._listeners.filter(l => l !== value); |
| 65 | } |
| 66 | |
| 67 | public trigger(): void { |
| 68 | for (const l of this._listeners) { |
| 69 | l(); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @partial |
no outgoing calls
no test coverage detected