* Wait for an event to be emitted * @param event - Event name to wait for * @param timeout - Optional timeout in milliseconds * @returns Promise that resolves with the event payload
(
event: T,
timeout?: number,
)
| 64 | * @returns Promise that resolves with the event payload |
| 65 | */ |
| 66 | waitFor<T extends keyof TEvents>( |
| 67 | event: T, |
| 68 | timeout?: number, |
| 69 | ): Promise<TEvents[T]> { |
| 70 | return new Promise((resolve, reject) => { |
| 71 | let timeoutId: NodeJS.Timeout | undefined |
| 72 | const unsubscribe = this.on(event, (eventPayload) => { |
| 73 | if (timeoutId) { |
| 74 | clearTimeout(timeoutId) |
| 75 | timeoutId = undefined |
| 76 | } |
| 77 | resolve(eventPayload) |
| 78 | unsubscribe() |
| 79 | }) |
| 80 | if (timeout) { |
| 81 | timeoutId = setTimeout(() => { |
| 82 | timeoutId = undefined |
| 83 | unsubscribe() |
| 84 | reject(new Error(`Timeout waiting for event ${String(event)}`)) |
| 85 | }, timeout) |
| 86 | } |
| 87 | }) |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Emit an event to all listeners |
no test coverage detected