Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(async-utils): prevent timeout and interval checks in wait from leaving open handles #682

Merged
merged 3 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 11 additions & 22 deletions src/core/asyncUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
AsyncUtils
} from '../types'

import { resolveAfter, callAfter } from '../helpers/promises'
import { createTimeoutSignal } from '../helpers/createTimeoutSignal'
import { TimeoutError } from '../helpers/error'

const DEFAULT_INTERVAL = 50
Expand All @@ -20,37 +20,26 @@ function asyncUtils(act: Act, addResolver: (callback: () => void) => void): Asyn
return callbackResult ?? callbackResult === undefined
}

const timeoutSignal = createTimeoutSignal(timeout)

const waitForResult = async () => {
while (true) {
await Promise.race(
[
new Promise<void>((resolve) => addResolver(resolve)),
interval && resolveAfter(interval)
].filter(Boolean)
)

if (checkResult()) {
const intervalSignal = createTimeoutSignal(interval)
timeoutSignal.onTimeout(() => intervalSignal.cancel())

await intervalSignal.wrap(new Promise<void>(addResolver))

if (checkResult() || timeoutSignal.timedOut) {
return
}
}
}

let timedOut = false

if (!checkResult()) {
if (timeout) {
const timeoutPromise = () =>
callAfter(() => {
timedOut = true
}, timeout)

await act(() => Promise.race([waitForResult(), timeoutPromise()]))
} else {
await act(waitForResult)
}
await act(() => timeoutSignal.wrap(waitForResult()))
}

return !timedOut
return !timeoutSignal.timedOut
}

const waitFor = async (
Expand Down
39 changes: 39 additions & 0 deletions src/helpers/createTimeoutSignal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { WaitOptions } from '../types'

function createTimeoutSignal(timeout: WaitOptions['timeout']) {
let timeoutId: NodeJS.Timeout
const timeoutCallbacks: Array<() => void> = []

const timeoutSignal = {
onTimeout(callback: () => void) {
timeoutCallbacks.push(callback)
},
wrap(promise: Promise<void>) {
return new Promise<void>((resolve, reject) => {
timeoutSignal.timedOut = false
timeoutSignal.onTimeout(resolve)

if (timeout) {
timeoutId = setTimeout(() => {
timeoutSignal.timedOut = true
timeoutCallbacks.forEach((callback) => callback())
resolve()
}, timeout)
}

promise
.then(resolve)
.catch(reject)
.finally(() => timeoutSignal.cancel())
})
},
cancel() {
clearTimeout(timeoutId)
},
timedOut: false
}

return timeoutSignal
}

export { createTimeoutSignal }
10 changes: 0 additions & 10 deletions src/helpers/promises.ts

This file was deleted.