UNPKG

1.89 kBJavaScriptView Raw
1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3import Observable from 'zen-observable-ts';
4import { ConsoleLogger as Logger } from '../Logger';
5var logger = new Logger('Reachability', 'DEBUG');
6var ReachabilityNavigator = /** @class */ (function () {
7 function ReachabilityNavigator() {
8 }
9 ReachabilityNavigator.prototype.networkMonitor = function (netInfo) {
10 /**
11 * Here netinfo refers to @react-native-community/netinfo
12 * This is needed in React Native to enable network detection
13 * We do not import it in Core so that Apps that do not use DataStore
14 * Do not need to install and link this dependency
15 * When using Reachability in React Native, pass NetInfo as a param to networkMonitor
16 */
17 if (!(netInfo && netInfo.addEventListener)) {
18 throw new Error('NetInfo must be passed to networkMonitor to enable reachability in React Native');
19 }
20 return new Observable(function (observer) {
21 logger.log('subscribing to reachability in React Native');
22 var unsubscribe = netInfo.addEventListener(function (_a) {
23 var isInternetReachable = _a.isInternetReachable;
24 // `isInternetReachable` can sometimes be `null` initially, so we want
25 // to make sure it is a boolean first before sending it to the observer.
26 if (typeof isInternetReachable === 'boolean') {
27 var online = isInternetReachable;
28 logger.log('Notifying reachability change', online);
29 observer.next({ online: online });
30 }
31 });
32 return function () {
33 unsubscribe();
34 };
35 });
36 };
37 return ReachabilityNavigator;
38}());
39export default ReachabilityNavigator;