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