Skip to content

Commit 2bd7222

Browse files
committed
chore(refactor): refactor TimeoutError to simplify constructor args
1 parent 1ea2afe commit 2bd7222

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

Diff for: src/asyncUtils.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export interface WaitOptions {
77
}
88

99
class TimeoutError extends Error {
10-
constructor(utilName: string, { timeout }: Pick<WaitOptions, 'timeout'>) {
11-
super(`Timed out in ${utilName} after ${timeout as number}ms.`)
10+
constructor(utilName: string, timeout: number) {
11+
super(`Timed out in ${utilName} after ${timeout}ms.`)
1212
}
1313
}
1414

@@ -21,14 +21,14 @@ function resolveAfter(ms: number) {
2121
function asyncUtils(addResolver: (callback: () => void) => void) {
2222
let nextUpdatePromise: Promise<void> | null = null
2323

24-
const waitForNextUpdate = async (options: Pick<WaitOptions, 'timeout'> = {}) => {
24+
const waitForNextUpdate = async ({ timeout }: Pick<WaitOptions, 'timeout'> = {}) => {
2525
if (!nextUpdatePromise) {
2626
nextUpdatePromise = new Promise((resolve, reject) => {
2727
let timeoutId: ReturnType<typeof setTimeout>
28-
if (options.timeout && options.timeout > 0) {
28+
if (timeout && timeout > 0) {
2929
timeoutId = setTimeout(
30-
() => reject(new TimeoutError('waitForNextUpdate', options)),
31-
options.timeout
30+
() => reject(new TimeoutError('waitForNextUpdate', timeout)),
31+
timeout
3232
)
3333
}
3434
addResolver(() => {
@@ -73,8 +73,8 @@ function asyncUtils(addResolver: (callback: () => void) => void) {
7373
return
7474
}
7575
} catch (error: unknown) {
76-
if (error instanceof TimeoutError) {
77-
throw new TimeoutError('waitFor', { timeout: initialTimeout })
76+
if (error instanceof TimeoutError && initialTimeout) {
77+
throw new TimeoutError('waitFor', initialTimeout)
7878
}
7979
throw error as Error
8080
}
@@ -95,8 +95,8 @@ function asyncUtils(addResolver: (callback: () => void) => void) {
9595
...options
9696
})
9797
} catch (error: unknown) {
98-
if (error instanceof TimeoutError) {
99-
throw new TimeoutError('waitForValueToChange', options)
98+
if (error instanceof TimeoutError && options.timeout) {
99+
throw new TimeoutError('waitForValueToChange', options.timeout)
100100
}
101101
throw error as Error
102102
}

0 commit comments

Comments
 (0)