Clean up timeout in waitForOnline
This commit is contained in:
parent
8477841fd6
commit
9ea8a456f2
1 changed files with 6 additions and 1 deletions
|
@ -14,6 +14,8 @@ export function waitForOnline(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let timeoutId: undefined | ReturnType<typeof setTimeout>;
|
||||||
|
|
||||||
const listener = () => {
|
const listener = () => {
|
||||||
cleanup();
|
cleanup();
|
||||||
resolve();
|
resolve();
|
||||||
|
@ -21,12 +23,15 @@ export function waitForOnline(
|
||||||
|
|
||||||
const cleanup = () => {
|
const cleanup = () => {
|
||||||
onlineEventTarget.removeEventListener('online', listener);
|
onlineEventTarget.removeEventListener('online', listener);
|
||||||
|
if (typeof timeoutId === 'number') {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
onlineEventTarget.addEventListener('online', listener);
|
onlineEventTarget.addEventListener('online', listener);
|
||||||
|
|
||||||
if (timeout !== undefined) {
|
if (timeout !== undefined) {
|
||||||
setTimeout(() => {
|
timeoutId = setTimeout(() => {
|
||||||
cleanup();
|
cleanup();
|
||||||
reject(new Error('waitForOnline: did not come online in time'));
|
reject(new Error('waitForOnline: did not come online in time'));
|
||||||
}, timeout);
|
}, timeout);
|
||||||
|
|
Loading…
Reference in a new issue