* Wait for the ORPC subscription to be fully established. * This waits for the "caught-up" event from the server, which is emitted * after the event subscription is set up and history replay is complete. * Call this after start() and before sending messages to avoid race conditions.
(timeoutMs: number = 5000)
| 75 | * Call this after start() and before sending messages to avoid race conditions. |
| 76 | */ |
| 77 | async waitForSubscription(timeoutMs: number = 5000): Promise<void> { |
| 78 | if (!this.started) { |
| 79 | throw new Error("StreamCollector not started. Call start() first."); |
| 80 | } |
| 81 | if (this.subscriptionReady) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | return new Promise<void>((resolve, reject) => { |
| 86 | const timer = setTimeout(() => { |
| 87 | reject(new Error(`Subscription setup timed out after ${timeoutMs}ms`)); |
| 88 | }, timeoutMs); |
| 89 | |
| 90 | this.subscriptionReadyResolve = () => { |
| 91 | clearTimeout(timer); |
| 92 | resolve(); |
| 93 | }; |
| 94 | |
| 95 | // If already ready (race condition), resolve immediately |
| 96 | if (this.subscriptionReady) { |
| 97 | clearTimeout(timer); |
| 98 | resolve(); |
| 99 | } |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Stop collecting and cleanup. |
no test coverage detected