Skip to content

Commit 36f25fb

Browse files
committed
refactor: return boolean instead of throwing error to handle timeout
1 parent 71455c4 commit 36f25fb

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

src/core/asyncUtils.ts

+29-35
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ const DEFAULT_INTERVAL = 50
1414
const DEFAULT_TIMEOUT = 1000
1515

1616
function asyncUtils(act: Act, addResolver: (callback: () => void) => void): AsyncUtils {
17-
const wait = async (
18-
callback: () => boolean | void,
19-
{ interval = DEFAULT_INTERVAL, timeout = DEFAULT_TIMEOUT }: WaitOptions = {}
20-
) => {
17+
const wait = async (callback: () => boolean | void, { interval, timeout }: WaitOptions) => {
2118
const checkResult = () => {
2219
const callbackResult = callback()
2320
return callbackResult ?? callbackResult === undefined
@@ -38,68 +35,65 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
3835
}
3936
}
4037

38+
let timedOut = false
39+
4140
if (!checkResult()) {
4241
if (timeout) {
43-
const timeoutPromise = callAfter(() => {
44-
throw new TimeoutError(wait, timeout)
45-
}, timeout)
42+
const timeoutPromise = () =>
43+
callAfter(() => {
44+
timedOut = true
45+
}, timeout)
4646

47-
await act(() => Promise.race([waitForResult(), timeoutPromise]))
47+
await act(() => Promise.race([waitForResult(), timeoutPromise()]))
4848
} else {
4949
await act(waitForResult)
5050
}
5151
}
52+
53+
return !timedOut
5254
}
5355

54-
const waitFor = async (callback: () => boolean | void, options: WaitForOptions = {}) => {
56+
const waitFor = async (
57+
callback: () => boolean | void,
58+
{ interval = DEFAULT_INTERVAL, timeout = DEFAULT_TIMEOUT }: WaitForOptions = {}
59+
) => {
5560
const safeCallback = () => {
5661
try {
5762
return callback()
5863
} catch (error: unknown) {
5964
return false
6065
}
6166
}
62-
try {
63-
await wait(safeCallback, options)
64-
} catch (error: unknown) {
65-
if (error instanceof TimeoutError) {
66-
throw new TimeoutError(waitFor, error.timeout)
67-
}
68-
throw error
67+
68+
const result = await wait(safeCallback, { interval, timeout })
69+
if (!result && timeout) {
70+
throw new TimeoutError(waitFor, timeout)
6971
}
7072
}
7173

7274
const waitForValueToChange = async (
7375
selector: () => unknown,
74-
options: WaitForValueToChangeOptions = {}
76+
{ interval = DEFAULT_INTERVAL, timeout = DEFAULT_TIMEOUT }: WaitForValueToChangeOptions = {}
7577
) => {
7678
const initialValue = selector()
77-
try {
78-
await wait(() => selector() !== initialValue, options)
79-
} catch (error: unknown) {
80-
if (error instanceof TimeoutError) {
81-
throw new TimeoutError(waitForValueToChange, error.timeout)
82-
}
83-
throw error
79+
80+
const result = await wait(() => selector() !== initialValue, { interval, timeout })
81+
if (!result && timeout) {
82+
throw new TimeoutError(waitForValueToChange, timeout)
8483
}
8584
}
8685

87-
const waitForNextUpdate = async (options: WaitForNextUpdateOptions = {}) => {
86+
const waitForNextUpdate = async ({
87+
timeout = DEFAULT_TIMEOUT
88+
}: WaitForNextUpdateOptions = {}) => {
8889
let updated = false
8990
addResolver(() => {
9091
updated = true
9192
})
9293

93-
try {
94-
await wait(() => updated, {
95-
interval: false,
96-
...options
97-
})
98-
} catch (error: unknown) {
99-
if (error instanceof TimeoutError) {
100-
throw new TimeoutError(waitForNextUpdate, error.timeout)
101-
}
102-
throw error
94+
const result = await wait(() => updated, { interval: false, timeout })
95+
if (!result && timeout) {
96+
throw new TimeoutError(waitForNextUpdate, timeout)
10397
}
10498
}
10599

src/helpers/error.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
class TimeoutError extends Error {
2-
timeout: number
3-
42
constructor(util: Function, timeout: number) {
53
super(`Timed out in ${util.name} after ${timeout}ms.`)
6-
this.timeout = timeout
74
}
85
}
96

0 commit comments

Comments
 (0)