| 6 | } |
| 7 | |
| 8 | export const waitFor = ( |
| 9 | condition: (() => Promise<boolean>) | (() => boolean), |
| 10 | { timeout = 30_000, interval = 250 }: WaitForOptions = {}, |
| 11 | ) => { |
| 12 | let timeoutId: NodeJS.Timeout | undefined = undefined |
| 13 | |
| 14 | return Promise.race([ |
| 15 | new Promise<void>((resolve) => { |
| 16 | const check = async () => { |
| 17 | const result = condition() |
| 18 | const isSatisfied = result instanceof Promise ? await result : result |
| 19 | |
| 20 | if (isSatisfied) { |
| 21 | if (timeoutId) { |
| 22 | clearTimeout(timeoutId) |
| 23 | timeoutId = undefined |
| 24 | } |
| 25 | |
| 26 | resolve() |
| 27 | } else { |
| 28 | setTimeout(check, interval) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | check() |
| 33 | }), |
| 34 | new Promise((_, reject) => { |
| 35 | timeoutId = setTimeout(() => { |
| 36 | reject(new Error(`Timeout after ${Math.floor(timeout / 1000)}s`)) |
| 37 | }, timeout) |
| 38 | }), |
| 39 | ]) |
| 40 | } |
| 41 | |
| 42 | type WaitUntilAbortedOptions = WaitForOptions & { |
| 43 | api: RooCodeAPI |