mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2025-12-24 20:26:24 +00:00
Rename files
This commit is contained in:
47
ts/util/waitForOnline.dom.ts
Normal file
47
ts/util/waitForOnline.dom.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { clearTimeoutIfNecessary } from './clearTimeoutIfNecessary.std.js';
|
||||
|
||||
export type WaitForOnlineOptionsType = Readonly<{
|
||||
server: Readonly<{ isOnline: () => boolean | undefined }>;
|
||||
events?: {
|
||||
on: (event: 'online', fn: () => void) => void;
|
||||
off: (event: 'online', fn: () => void) => void;
|
||||
};
|
||||
timeout?: number;
|
||||
}>;
|
||||
|
||||
export function waitForOnline({
|
||||
server,
|
||||
events = window.Whisper.events,
|
||||
timeout,
|
||||
}: WaitForOnlineOptionsType): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (server.isOnline()) {
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
let timeoutId: undefined | ReturnType<typeof setTimeout>;
|
||||
|
||||
const listener = () => {
|
||||
cleanup();
|
||||
resolve();
|
||||
};
|
||||
|
||||
const cleanup = () => {
|
||||
events.off('online', listener);
|
||||
clearTimeoutIfNecessary(timeoutId);
|
||||
};
|
||||
|
||||
events.on('online', listener);
|
||||
|
||||
if (timeout !== undefined) {
|
||||
timeoutId = setTimeout(() => {
|
||||
cleanup();
|
||||
reject(new Error('waitForOnline: did not come online in time'));
|
||||
}, timeout);
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user