Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async componentDidMount() {
const state = await NetInfo.fetch()
let newState = {}
newState.connectedToWifi = state.type === 'wifi' && state.isConnected
let host = await AsyncStorage.getItem('@ipFinder.host')
// Check if we have host or that current is still available and if we are connected to wifi
// other check for host
if ((!host || !(await this.isHost(host))) && newState.connectedToWifi) {
host = null
SplashScreen.hide()
if (await deviceInfo.isEmulator()) {
// If we are a emulator then check the host ip
if (await this.isHost('10.0.2.2:5000')) {
export default function init(store: Store) {
BackHandler.addEventListener("hardwareBackPress", () => {
const { nav }: { nav: NavigationState } = store.getState();
const { index } = nav;
if (index > 1) {
store.dispatch(StackActions.pop({}));
return true;
}
return false;
});
NetInfo.fetch().then(state => {
store.dispatch(NetworkActions.update(state));
});
NetInfo.addEventListener(state => {
if (typeof state === "object") {
store.dispatch(NetworkActions.update(state));
}
});
common(store);
}
async isAvailableAsync(): Promise {
if (this._isAvailable) {
return this._isAvailable;
}
try {
const netInfo = await NetInfo.fetch();
this._isAvailable = netInfo.isConnected;
} catch (e) {
this._isAvailable = false;
console.warn(`Uncaught error when fetching connectivity status: ${e}`);
}
return this._isAvailable;
}
isConnected(): Promise {
return RNNetInfo.fetch().then((state: RNNetInfo.NetInfoState) => {
return state.isConnected;
}).catch(() => {
return Promise.reject('NetInfo.isConnected.fetch() failed');
});
}
getType(): Promise {
return RNNetInfo.fetch().then((state: RNNetInfo.NetInfoState) => {
return NetInfo._getNetworkTypeFromConnectionInfo(state);
});
}
_refreshNetworkStatus = () => {
NetInfo.fetch().then(this._networkStatusChanged);
};
connect = (displayBar = false) => {
const {connection, startPeriodicStatusUpdates} = this.props.actions;
clearTimeout(this.connectionRetryTimeout);
NetInfo.fetch().then(async ({isConnected}) => {
const {hasInternet, serverReachable} = await checkConnection(isConnected);
connection(hasInternet);
this.hasInternet = hasInternet;
this.serverReachable = serverReachable;
if (serverReachable) {
this.statusUpdates = true;
this.initializeWebSocket();
startPeriodicStatusUpdates();
} else {
if (displayBar) {
this.show();
}
this.handleWebSocket(false);
const initOsNetworkStatus = async () => {
const {type} = await NetInfo.fetch()
return ConfigGen.createOsNetworkStatusChanged({isInit: true, online: type !== 'none', type})
}
const isOnline = async () => {
let isConnected = true;
try {
const connectionInfo = await NetInfo.fetch();
if (connectionInfo.type === "none" || connectionInfo.type === "unknown") isConnected = false;
} catch (e) {}
return isConnected;
};