( actorRef: TActorRef, predicate: (emitted: SnapshotFrom<TActorRef>) => boolean, options?: Partial<WaitForOptions> )
| 40 | * the condition |
| 41 | */ |
| 42 | export function waitFor<TActorRef extends AnyActorRef>( |
| 43 | actorRef: TActorRef, |
| 44 | predicate: (emitted: SnapshotFrom<TActorRef>) => boolean, |
| 45 | options?: Partial<WaitForOptions> |
| 46 | ): Promise<SnapshotFrom<TActorRef>> { |
| 47 | const resolvedOptions: WaitForOptions = { |
| 48 | ...defaultWaitForOptions, |
| 49 | ...options |
| 50 | }; |
| 51 | return new Promise((res, rej) => { |
| 52 | const { signal } = resolvedOptions; |
| 53 | if (signal?.aborted) { |
| 54 | // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors |
| 55 | rej(signal.reason); |
| 56 | return; |
| 57 | } |
| 58 | let done = false; |
| 59 | if (isDevelopment && resolvedOptions.timeout < 0) { |
| 60 | console.error( |
| 61 | '`timeout` passed to `waitFor` is negative and it will reject its internal promise immediately.' |
| 62 | ); |
| 63 | } |
| 64 | const handle = |
| 65 | resolvedOptions.timeout === Infinity |
| 66 | ? undefined |
| 67 | : setTimeout(() => { |
| 68 | dispose(); |
| 69 | rej(new Error(`Timeout of ${resolvedOptions.timeout} ms exceeded`)); |
| 70 | }, resolvedOptions.timeout); |
| 71 | |
| 72 | const dispose = () => { |
| 73 | clearTimeout(handle); |
| 74 | done = true; |
| 75 | sub?.unsubscribe(); |
| 76 | if (abortListener) { |
| 77 | signal!.removeEventListener('abort', abortListener); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | function checkEmitted(emitted: SnapshotFrom<TActorRef>) { |
| 82 | if (predicate(emitted)) { |
| 83 | dispose(); |
| 84 | res(emitted); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * If the `signal` option is provided, this will be the listener for its |
| 90 | * `abort` event |
| 91 | */ |
| 92 | let abortListener: () => void | undefined; |
| 93 | // eslint-disable-next-line prefer-const |
| 94 | let sub: Subscription | undefined; // avoid TDZ when disposing synchronously |
| 95 | |
| 96 | // See if the current snapshot already matches the predicate |
| 97 | checkEmitted(actorRef.getSnapshot()); |
| 98 | if (done) { |
| 99 | return; |
no test coverage detected
searching dependent graphs…