Signal-Desktop/ts/services/networkObserver.ts

46 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type {
CheckNetworkStatusPayloadType,
NetworkActionType,
} from '../state/ducks/network';
import { getSocketStatus } from '../shims/socketStatus';
import * as log from '../logging/log';
2021-10-06 21:59:34 +00:00
import { SECOND } from '../util/durations';
import { SocketStatus } from '../types/SocketStatus';
type NetworkActions = {
checkNetworkStatus: (x: CheckNetworkStatusPayloadType) => NetworkActionType;
closeConnectingGracePeriod: () => NetworkActionType;
};
2020-09-03 14:59:24 +00:00
export function initializeNetworkObserver(
networkActions: NetworkActions
): void {
2021-10-06 21:59:34 +00:00
log.info('Initializing network observer');
const refresh = () => {
const socketStatus = getSocketStatus();
if (socketStatus === SocketStatus.CLOSED) {
// If we couldn't connect during startup - we should still switch SQL to
// the main process to avoid stalling UI.
window.Signal.Data.goBackToMainProcess();
}
networkActions.checkNetworkStatus({
isOnline: navigator.onLine,
socketStatus,
});
};
2021-09-16 20:18:42 +00:00
window.Whisper.events.on('socketStatusChange', refresh);
window.addEventListener('online', refresh);
window.addEventListener('offline', refresh);
window.setTimeout(() => {
networkActions.closeConnectingGracePeriod();
2021-10-06 21:59:34 +00:00
}, 5 * SECOND);
}