Skip to content

Commit

Permalink
refactor(async-utils): only advance timers on a single timeoutController
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeyper committed Sep 19, 2021
1 parent 972981a commit d623572
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/core/asyncUtils.ts
Expand Up @@ -23,11 +23,11 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
return callbackResult ?? callbackResult === undefined
}

const timeoutController = createTimeoutController(timeout, { allowFakeTimers: !interval })
const timeoutController = createTimeoutController(timeout, { allowFakeTimers: true })

const waitForResult = async () => {
while (true) {
const intervalController = createTimeoutController(interval, { allowFakeTimers: true })
const intervalController = createTimeoutController(interval)
timeoutController.onTimeout(() => intervalController.cancel())

await intervalController.wrap(new Promise<void>(addResolver))
Expand Down
6 changes: 2 additions & 4 deletions src/helpers/createTimeoutController.ts
@@ -1,12 +1,10 @@
import { fakeTimersAreEnabled, advanceTimers } from './fakeTimers'

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

const { allowFakeTimers } = options

const timeoutController = {
onTimeout(callback: () => void) {
timeoutCallbacks.push(callback)
Expand All @@ -23,7 +21,7 @@ function createTimeoutController(timeout: number | false, options: { allowFakeTi
resolve()
}, timeout)
}

if (fakeTimersAreEnabled() && allowFakeTimers) {
advanceTimers(() => finished)
}
Expand Down

0 comments on commit d623572

Please sign in to comment.