2022-09-23 09:03:25 +00:00
|
|
|
import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app/actionTypes';
|
|
|
|
import MiddlewareRegistry from '../redux/MiddlewareRegistry';
|
2019-08-22 15:08:34 +00:00
|
|
|
|
2020-05-20 10:57:03 +00:00
|
|
|
import NetworkInfoService from './NetworkInfoService';
|
2019-08-22 15:08:34 +00:00
|
|
|
import { _storeNetworkInfoCleanup, setNetworkInfo } from './actions';
|
|
|
|
import { STORE_NAME } from './constants';
|
|
|
|
import { ONLINE_STATE_CHANGED_EVENT } from './events';
|
|
|
|
import logger from './logger';
|
|
|
|
import type { NetworkInfo } from './types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware for 'base/net-info' feature.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
|
|
|
|
const result = next(action);
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
case APP_WILL_MOUNT:
|
|
|
|
if (NetworkInfoService.isSupported()) {
|
|
|
|
const networkInfoService = new NetworkInfoService();
|
|
|
|
const stop = () => {
|
|
|
|
networkInfoService.stop();
|
2023-02-02 11:12:31 +00:00
|
|
|
|
|
|
|
// @ts-ignore
|
2019-08-22 15:08:34 +00:00
|
|
|
networkInfoService.removeAllListeners();
|
|
|
|
};
|
|
|
|
|
2023-02-02 11:12:31 +00:00
|
|
|
// @ts-ignore
|
2019-08-22 15:08:34 +00:00
|
|
|
networkInfoService.addListener(
|
|
|
|
ONLINE_STATE_CHANGED_EVENT,
|
|
|
|
({ isOnline, networkType, details }: NetworkInfo) => {
|
|
|
|
logger.info('Network changed', JSON.stringify({
|
|
|
|
isOnline,
|
|
|
|
details,
|
|
|
|
networkType
|
|
|
|
}));
|
|
|
|
dispatch(setNetworkInfo({
|
|
|
|
isOnline,
|
|
|
|
networkType,
|
|
|
|
details
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch(_storeNetworkInfoCleanup(stop));
|
|
|
|
|
|
|
|
networkInfoService.start();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case APP_WILL_UNMOUNT: {
|
|
|
|
const { _cleanup } = getState()[STORE_NAME];
|
|
|
|
|
|
|
|
if (_cleanup) {
|
|
|
|
_cleanup();
|
|
|
|
dispatch(_storeNetworkInfoCleanup(undefined));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|