2020-02-12 21:30:58 +00:00
|
|
|
import {
|
|
|
|
CheckNetworkStatusPayloadType,
|
|
|
|
NetworkActionType,
|
|
|
|
} from '../state/ducks/network';
|
|
|
|
import { getSocketStatus } from '../shims/socketStatus';
|
|
|
|
|
|
|
|
type NetworkActions = {
|
|
|
|
checkNetworkStatus: (x: CheckNetworkStatusPayloadType) => NetworkActionType;
|
|
|
|
closeConnectingGracePeriod: () => NetworkActionType;
|
|
|
|
};
|
|
|
|
|
|
|
|
const REFRESH_INTERVAL = 5000;
|
|
|
|
|
|
|
|
export function initializeNetworkObserver(networkActions: NetworkActions) {
|
2020-03-31 20:03:38 +00:00
|
|
|
const { log } = window;
|
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(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('online', refresh);
|
|
|
|
window.addEventListener('offline', refresh);
|
|
|
|
window.setInterval(refresh, REFRESH_INTERVAL);
|
|
|
|
window.setTimeout(() => {
|
|
|
|
networkActions.closeConnectingGracePeriod();
|
|
|
|
}, REFRESH_INTERVAL);
|
|
|
|
}
|