Skip to content

Commit

Permalink
Clean up timeout in waitForOnline
Browse files Browse the repository at this point in the history
Co-authored-by: Evan Hahn <69474926+EvanHahn-Signal@users.noreply.github.com>
  • Loading branch information
automated-signal and EvanHahn-Signal committed Nov 1, 2021
1 parent 3dd3a0f commit 63fd366
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ts/util/waitForOnline.ts
Expand Up @@ -14,19 +14,24 @@ export function waitForOnline(
return;
}

let timeoutId: undefined | ReturnType<typeof setTimeout>;

const listener = () => {
cleanup();
resolve();
};

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);
Expand Down

0 comments on commit 63fd366

Please sign in to comment.