Clean up timeout in `waitForOnline`

This commit is contained in:
Evan Hahn 2021-11-01 13:38:26 -05:00 committed by GitHub
parent 8477841fd6
commit 9ea8a456f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 1 deletions

View File

@ -14,6 +14,8 @@ export function waitForOnline(
return;
}
let timeoutId: undefined | ReturnType<typeof setTimeout>;
const listener = () => {
cleanup();
resolve();
@ -21,12 +23,15 @@ export function waitForOnline(
const cleanup = () => {
onlineEventTarget.removeEventListener('online', listener);
if (typeof timeoutId === 'number') {
clearTimeout(timeoutId);
}
};
onlineEventTarget.addEventListener('online', listener);
if (timeout !== undefined) {
setTimeout(() => {
timeoutId = setTimeout(() => {
cleanup();
reject(new Error('waitForOnline: did not come online in time'));
}, timeout);