2019-08-22 15:08:34 +00:00
|
|
|
// @flow
|
|
|
|
import NetInfo from '@react-native-community/netinfo';
|
|
|
|
import type { NetInfoState, NetInfoSubscription } from '@react-native-community/netinfo';
|
2020-05-20 10:57:03 +00:00
|
|
|
import EventEmitter from 'events';
|
2019-08-22 15:08:34 +00:00
|
|
|
|
|
|
|
import { ONLINE_STATE_CHANGED_EVENT } from './events';
|
|
|
|
import type { NetworkInfo } from './types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The network info service implementation for iOS and Android. 'react-native-netinfo' seems to support windows as well,
|
|
|
|
* but that has not been tested and is nto used by jitsi-meet.
|
|
|
|
*/
|
|
|
|
export default class NetworkInfoService extends EventEmitter {
|
|
|
|
/**
|
|
|
|
* Stores the native subscription for future cleanup.
|
|
|
|
*/
|
|
|
|
_subscription: NetInfoSubscription;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts library's structure to {@link NetworkInfo} used by jitsi-meet.
|
|
|
|
*
|
|
|
|
* @param {NetInfoState} netInfoState - The new state given by the native library.
|
|
|
|
* @private
|
|
|
|
* @returns {NetworkInfo}
|
|
|
|
*/
|
|
|
|
static _convertNetInfoState(netInfoState: NetInfoState): NetworkInfo {
|
|
|
|
return {
|
|
|
|
isOnline: netInfoState.isInternetReachable,
|
|
|
|
details: netInfoState.details,
|
|
|
|
networkType: netInfoState.type
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for support.
|
|
|
|
*
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
static isSupported() {
|
|
|
|
return Boolean(NetInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the service.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
start() {
|
|
|
|
this._subscription = NetInfo.addEventListener(netInfoState => {
|
|
|
|
this.emit(ONLINE_STATE_CHANGED_EVENT, NetworkInfoService._convertNetInfoState(netInfoState));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the service.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
stop() {
|
|
|
|
if (this._subscription) {
|
|
|
|
this._subscription();
|
|
|
|
this._subscription = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|