Skip to content

Commit d623572

Browse files
committed
refactor(async-utils): only advance timers on a single timeoutController
1 parent 972981a commit d623572

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/core/asyncUtils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
2323
return callbackResult ?? callbackResult === undefined
2424
}
2525

26-
const timeoutController = createTimeoutController(timeout, { allowFakeTimers: !interval })
26+
const timeoutController = createTimeoutController(timeout, { allowFakeTimers: true })
2727

2828
const waitForResult = async () => {
2929
while (true) {
30-
const intervalController = createTimeoutController(interval, { allowFakeTimers: true })
30+
const intervalController = createTimeoutController(interval)
3131
timeoutController.onTimeout(() => intervalController.cancel())
3232

3333
await intervalController.wrap(new Promise<void>(addResolver))

src/helpers/createTimeoutController.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { fakeTimersAreEnabled, advanceTimers } from './fakeTimers'
22

3-
function createTimeoutController(timeout: number | false, options: { allowFakeTimers: boolean }) {
3+
function createTimeoutController(timeout: number | false, { allowFakeTimers = false } = {}) {
44
let timeoutId: NodeJS.Timeout
55
const timeoutCallbacks: Array<() => void> = []
66
let finished = false
77

8-
const { allowFakeTimers } = options
9-
108
const timeoutController = {
119
onTimeout(callback: () => void) {
1210
timeoutCallbacks.push(callback)
@@ -23,7 +21,7 @@ function createTimeoutController(timeout: number | false, options: { allowFakeTi
2321
resolve()
2422
}, timeout)
2523
}
26-
24+
2725
if (fakeTimersAreEnabled() && allowFakeTimers) {
2826
advanceTimers(() => finished)
2927
}

0 commit comments

Comments
 (0)