signal-desktop/ts/util/waitForOnline.ts

22 lines
497 B
TypeScript
Raw Normal View History

2021-04-08 16:24:21 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
export function waitForOnline(
navigator: Readonly<{ onLine: boolean }>,
onlineEventTarget: EventTarget
): Promise<void> {
return new Promise(resolve => {
if (navigator.onLine) {
resolve();
return;
}
const listener = () => {
onlineEventTarget.removeEventListener('online', listener);
resolve();
};
onlineEventTarget.addEventListener('online', listener);
});
}