-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checkInternetConnectionOnStartup utility function (#94)
- Loading branch information
1 parent
0366c66
commit a8deeb7
Showing
3 changed files
with
43 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* @flow */ | ||
|
||
import { Platform, NetInfo } from 'react-native'; | ||
import checkInternetAccess from './checkInternetAccess'; | ||
|
||
// on iOS, the listener is fired immediately after registration | ||
// on Android, we need to use `isConnected.fetch`, that returns a promise which resolves with a boolean | ||
export default function checkInternetConnectionOnStartup( | ||
timeout: number = 3000, | ||
url: string = 'https://google.com', | ||
): Promise<boolean> { | ||
let connectionChecked: Promise<boolean>; | ||
if (Platform.OS === 'ios') { | ||
connectionChecked = new Promise((resolve: Function) => { | ||
const handleFirstConnectivityChangeIOS = (isConnected: boolean) => { | ||
NetInfo.isConnected.removeEventListener( | ||
'connectionChange', | ||
handleFirstConnectivityChangeIOS, | ||
); | ||
resolve(isConnected); | ||
}; | ||
NetInfo.isConnected.addEventListener( | ||
'connectionChange', | ||
handleFirstConnectivityChangeIOS, | ||
); | ||
}); | ||
} else { | ||
connectionChecked = NetInfo.isConnected.fetch(); | ||
} | ||
|
||
return connectionChecked.then((isConnected: boolean) => { | ||
if (isConnected) { | ||
return checkInternetAccess(timeout, url); | ||
} | ||
return Promise.resolve(false); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters