2020-10-30 15:34:04 -05:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 14:15:33 -05:00
|
|
|
import type {
|
2020-02-12 13:30:58 -08:00
|
|
|
CheckNetworkStatusPayloadType,
|
|
|
|
NetworkActionType,
|
|
|
|
} from '../state/ducks/network';
|
|
|
|
import { getSocketStatus } from '../shims/socketStatus';
|
2021-09-17 14:27:53 -04:00
|
|
|
import * as log from '../logging/log';
|
2021-10-06 14:59:34 -07:00
|
|
|
import { SECOND } from '../util/durations';
|
2022-01-03 13:44:04 -08:00
|
|
|
import { SocketStatus } from '../types/SocketStatus';
|
2020-02-12 13:30:58 -08:00
|
|
|
|
|
|
|
type NetworkActions = {
|
|
|
|
checkNetworkStatus: (x: CheckNetworkStatusPayloadType) => NetworkActionType;
|
|
|
|
closeConnectingGracePeriod: () => NetworkActionType;
|
|
|
|
};
|
|
|
|
|
2020-09-03 07:59:24 -07:00
|
|
|
export function initializeNetworkObserver(
|
|
|
|
networkActions: NetworkActions
|
|
|
|
): void {
|
2021-10-06 14:59:34 -07:00
|
|
|
log.info('Initializing network observer');
|
2020-02-12 13:30:58 -08:00
|
|
|
|
|
|
|
const refresh = () => {
|
2022-01-03 13:44:04 -08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:30:58 -08:00
|
|
|
networkActions.checkNetworkStatus({
|
|
|
|
isOnline: navigator.onLine,
|
2022-01-03 13:44:04 -08:00
|
|
|
socketStatus,
|
2020-02-12 13:30:58 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-09-16 13:18:42 -07:00
|
|
|
window.Whisper.events.on('socketStatusChange', refresh);
|
|
|
|
|
2020-02-12 13:30:58 -08:00
|
|
|
window.addEventListener('online', refresh);
|
|
|
|
window.addEventListener('offline', refresh);
|
|
|
|
window.setTimeout(() => {
|
|
|
|
networkActions.closeConnectingGracePeriod();
|
2021-10-06 14:59:34 -07:00
|
|
|
}, 5 * SECOND);
|
2020-02-12 13:30:58 -08:00
|
|
|
}
|