2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
import {
|
|
|
|
CheckNetworkStatusPayloadType,
|
|
|
|
NetworkActionType,
|
|
|
|
} from '../state/ducks/network';
|
|
|
|
import { getSocketStatus } from '../shims/socketStatus';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2020-02-12 21:30:58 +00:00
|
|
|
|
|
|
|
type NetworkActions = {
|
|
|
|
checkNetworkStatus: (x: CheckNetworkStatusPayloadType) => NetworkActionType;
|
|
|
|
closeConnectingGracePeriod: () => NetworkActionType;
|
|
|
|
};
|
|
|
|
|
|
|
|
const REFRESH_INTERVAL = 5000;
|
|
|
|
|
2020-09-03 14:59:24 +00:00
|
|
|
export function initializeNetworkObserver(
|
|
|
|
networkActions: NetworkActions
|
|
|
|
): void {
|
2020-02-12 21:30:58 +00:00
|
|
|
log.info(`Initializing network observer every ${REFRESH_INTERVAL}ms`);
|
|
|
|
|
|
|
|
const refresh = () => {
|
|
|
|
networkActions.checkNetworkStatus({
|
|
|
|
isOnline: navigator.onLine,
|
|
|
|
socketStatus: getSocketStatus(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-09-16 20:18:42 +00:00
|
|
|
window.Whisper.events.on('socketStatusChange', refresh);
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
window.addEventListener('online', refresh);
|
|
|
|
window.addEventListener('offline', refresh);
|
|
|
|
window.setInterval(refresh, REFRESH_INTERVAL);
|
|
|
|
window.setTimeout(() => {
|
|
|
|
networkActions.closeConnectingGracePeriod();
|
|
|
|
}, REFRESH_INTERVAL);
|
|
|
|
}
|