| 8 | * the callback as an arrow function. |
| 9 | */ |
| 10 | export class Event<T> { |
| 11 | /** |
| 12 | * Add a callback to this event instance. |
| 13 | * @param handler - the callback to be added to this event. |
| 14 | */ |
| 15 | add(handler: T extends void ? { (): void } : { (data: T): void }): void { |
| 16 | this.handlers.push(handler); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Removes a callback from this event instance. |
| 21 | * @param handler - the callback to be removed from this event. |
| 22 | */ |
| 23 | remove(handler: T extends void ? { (): void } : { (data: T): void }): void { |
| 24 | this.handlers = this.handlers.filter((h) => h !== handler); |
| 25 | } |
| 26 | |
| 27 | /** Triggers all the callbacks assigned to this event. */ |
| 28 | trigger = async (data?: T) => { |
| 29 | const handlers = this.handlers.slice(0); |
| 30 | for (const handler of handlers) { |
| 31 | await handler(data as any); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | /** Gets rid of all the suscribed events. */ |
| 36 | reset() { |
| 37 | this.handlers.length = 0; |
| 38 | } |
| 39 | |
| 40 | private handlers: (T extends void ? { (): void } : { (data: T): void })[] = |
| 41 | []; |
| 42 | } |
nothing calls this directly
no outgoing calls
no test coverage detected