-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(async-utils): prevent timeout and interval checks in wait from le…
…aving open handles (#682) * fix(async-utils): prevent timeout and interval checks in wait from leaving open handles * refactor(async-utils): rename timeoutSignal to timeoutController
- Loading branch information
Showing
3 changed files
with
50 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { WaitOptions } from '../types' | ||
|
||
function createTimeoutController(timeout: WaitOptions['timeout']) { | ||
let timeoutId: NodeJS.Timeout | ||
const timeoutCallbacks: Array<() => void> = [] | ||
|
||
const timeoutController = { | ||
onTimeout(callback: () => void) { | ||
timeoutCallbacks.push(callback) | ||
}, | ||
wrap(promise: Promise<void>) { | ||
return new Promise<void>((resolve, reject) => { | ||
timeoutController.timedOut = false | ||
timeoutController.onTimeout(resolve) | ||
|
||
if (timeout) { | ||
timeoutId = setTimeout(() => { | ||
timeoutController.timedOut = true | ||
timeoutCallbacks.forEach((callback) => callback()) | ||
resolve() | ||
}, timeout) | ||
} | ||
|
||
promise | ||
.then(resolve) | ||
.catch(reject) | ||
.finally(() => timeoutController.cancel()) | ||
}) | ||
}, | ||
cancel() { | ||
clearTimeout(timeoutId) | ||
}, | ||
timedOut: false | ||
} | ||
|
||
return timeoutController | ||
} | ||
|
||
export { createTimeoutController } |
This file was deleted.
Oops, something went wrong.