|
1 |
| -import { Act, WaitOptions, AsyncUtils } from '../types' |
| 1 | +import { |
| 2 | + Act, |
| 3 | + WaitOptions, |
| 4 | + WaitForOptions, |
| 5 | + WaitForValueToChangeOptions, |
| 6 | + WaitForNextUpdateOptions, |
| 7 | + AsyncUtils |
| 8 | +} from '../types' |
2 | 9 |
|
3 |
| -import { resolveAfter } from '../helpers/promises' |
| 10 | +import { resolveAfter, callAfter } from '../helpers/promises' |
4 | 11 | import { TimeoutError } from '../helpers/error'
|
5 | 12 |
|
| 13 | +const DEFAULT_INTERVAL = 50 |
| 14 | +const DEFAULT_TIMEOUT = 1000 |
| 15 | + |
6 | 16 | function asyncUtils(act: Act, addResolver: (callback: () => void) => void): AsyncUtils {
|
7 |
| - let nextUpdatePromise: Promise<void> | null = null |
8 |
| - |
9 |
| - const waitForNextUpdate = async ({ timeout }: Pick<WaitOptions, 'timeout'> = {}) => { |
10 |
| - if (nextUpdatePromise) { |
11 |
| - await nextUpdatePromise |
12 |
| - } else { |
13 |
| - nextUpdatePromise = new Promise((resolve, reject) => { |
14 |
| - let timeoutId: ReturnType<typeof setTimeout> |
15 |
| - if (timeout && timeout > 0) { |
16 |
| - timeoutId = setTimeout( |
17 |
| - () => reject(new TimeoutError(waitForNextUpdate, timeout)), |
18 |
| - timeout |
19 |
| - ) |
| 17 | + const wait = async (callback: () => boolean | void, { interval, timeout }: WaitOptions) => { |
| 18 | + const checkResult = () => { |
| 19 | + const callbackResult = callback() |
| 20 | + return callbackResult ?? callbackResult === undefined |
| 21 | + } |
| 22 | + |
| 23 | + const waitForResult = async () => { |
| 24 | + while (true) { |
| 25 | + await Promise.race( |
| 26 | + [ |
| 27 | + new Promise<void>((resolve) => addResolver(resolve)), |
| 28 | + interval && resolveAfter(interval) |
| 29 | + ].filter(Boolean) |
| 30 | + ) |
| 31 | + |
| 32 | + if (checkResult()) { |
| 33 | + return |
20 | 34 | }
|
21 |
| - addResolver(() => { |
22 |
| - clearTimeout(timeoutId) |
23 |
| - nextUpdatePromise = null |
24 |
| - resolve() |
25 |
| - }) |
26 |
| - }) |
27 |
| - await act(() => nextUpdatePromise as Promise<void>) |
| 35 | + } |
28 | 36 | }
|
| 37 | + |
| 38 | + let timedOut = false |
| 39 | + |
| 40 | + if (!checkResult()) { |
| 41 | + if (timeout) { |
| 42 | + const timeoutPromise = () => |
| 43 | + callAfter(() => { |
| 44 | + timedOut = true |
| 45 | + }, timeout) |
| 46 | + |
| 47 | + await act(() => Promise.race([waitForResult(), timeoutPromise()])) |
| 48 | + } else { |
| 49 | + await act(waitForResult) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return !timedOut |
29 | 54 | }
|
30 | 55 |
|
31 | 56 | const waitFor = async (
|
32 | 57 | callback: () => boolean | void,
|
33 |
| - { interval, timeout, suppressErrors = true }: WaitOptions = {} |
| 58 | + { interval = DEFAULT_INTERVAL, timeout = DEFAULT_TIMEOUT }: WaitForOptions = {} |
34 | 59 | ) => {
|
35 |
| - const checkResult = () => { |
| 60 | + const safeCallback = () => { |
36 | 61 | try {
|
37 |
| - const callbackResult = callback() |
38 |
| - return callbackResult ?? callbackResult === undefined |
| 62 | + return callback() |
39 | 63 | } catch (error: unknown) {
|
40 |
| - if (!suppressErrors) { |
41 |
| - throw error |
42 |
| - } |
43 |
| - return undefined |
| 64 | + return false |
44 | 65 | }
|
45 | 66 | }
|
46 | 67 |
|
47 |
| - const waitForResult = async () => { |
48 |
| - const initialTimeout = timeout |
49 |
| - while (true) { |
50 |
| - const startTime = Date.now() |
51 |
| - try { |
52 |
| - const nextCheck = interval |
53 |
| - ? Promise.race([waitForNextUpdate({ timeout }), resolveAfter(interval)]) |
54 |
| - : waitForNextUpdate({ timeout }) |
55 |
| - |
56 |
| - await nextCheck |
57 |
| - |
58 |
| - if (checkResult()) { |
59 |
| - return |
60 |
| - } |
61 |
| - } catch (error: unknown) { |
62 |
| - if (error instanceof TimeoutError && initialTimeout) { |
63 |
| - throw new TimeoutError(waitFor, initialTimeout) |
64 |
| - } |
65 |
| - throw error |
66 |
| - } |
67 |
| - if (timeout) timeout -= Date.now() - startTime |
68 |
| - } |
| 68 | + const result = await wait(safeCallback, { interval, timeout }) |
| 69 | + if (!result && timeout) { |
| 70 | + throw new TimeoutError(waitFor, timeout) |
69 | 71 | }
|
| 72 | + } |
70 | 73 |
|
71 |
| - if (!checkResult()) { |
72 |
| - await waitForResult() |
| 74 | + const waitForValueToChange = async ( |
| 75 | + selector: () => unknown, |
| 76 | + { interval = DEFAULT_INTERVAL, timeout = DEFAULT_TIMEOUT }: WaitForValueToChangeOptions = {} |
| 77 | + ) => { |
| 78 | + const initialValue = selector() |
| 79 | + |
| 80 | + const result = await wait(() => selector() !== initialValue, { interval, timeout }) |
| 81 | + if (!result && timeout) { |
| 82 | + throw new TimeoutError(waitForValueToChange, timeout) |
73 | 83 | }
|
74 | 84 | }
|
75 | 85 |
|
76 |
| - const waitForValueToChange = async (selector: () => unknown, options: WaitOptions = {}) => { |
77 |
| - const initialValue = selector() |
78 |
| - try { |
79 |
| - await waitFor(() => selector() !== initialValue, { |
80 |
| - suppressErrors: false, |
81 |
| - ...options |
82 |
| - }) |
83 |
| - } catch (error: unknown) { |
84 |
| - if (error instanceof TimeoutError && options.timeout) { |
85 |
| - throw new TimeoutError(waitForValueToChange, options.timeout) |
86 |
| - } |
87 |
| - throw error |
| 86 | + const waitForNextUpdate = async ({ |
| 87 | + timeout = DEFAULT_TIMEOUT |
| 88 | + }: WaitForNextUpdateOptions = {}) => { |
| 89 | + let updated = false |
| 90 | + addResolver(() => { |
| 91 | + updated = true |
| 92 | + }) |
| 93 | + |
| 94 | + const result = await wait(() => updated, { interval: false, timeout }) |
| 95 | + if (!result && timeout) { |
| 96 | + throw new TimeoutError(waitForNextUpdate, timeout) |
88 | 97 | }
|
89 | 98 | }
|
90 | 99 |
|
91 | 100 | return {
|
92 | 101 | waitFor,
|
93 |
| - waitForNextUpdate, |
94 |
| - waitForValueToChange |
| 102 | + waitForValueToChange, |
| 103 | + waitForNextUpdate |
95 | 104 | }
|
96 | 105 | }
|
97 | 106 |
|
|
0 commit comments