UNPKG

2.93 kBJavaScriptView Raw
1var __values = (this && this.__values) || function(o) {
2 var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
3 if (m) return m.call(o);
4 if (o && typeof o.length === "number") return {
5 next: function () {
6 if (o && i >= o.length) o = void 0;
7 return { value: o && o[i++], done: !o };
8 }
9 };
10 throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
11};
12// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
13// SPDX-License-Identifier: Apache-2.0
14import { browserOrNode, isWebWorker } from '@aws-amplify/core';
15import Observable from 'zen-observable-ts';
16var ReachabilityNavigator = /** @class */ (function () {
17 function ReachabilityNavigator() {
18 }
19 ReachabilityNavigator.prototype.networkMonitor = function (netInfo) {
20 if (browserOrNode().isNode) {
21 return Observable.from([{ online: true }]);
22 }
23 var globalObj = isWebWorker() ? self : window;
24 return new Observable(function (observer) {
25 observer.next({ online: globalObj.navigator.onLine });
26 var notifyOnline = function () { return observer.next({ online: true }); };
27 var notifyOffline = function () { return observer.next({ online: false }); };
28 globalObj.addEventListener('online', notifyOnline);
29 globalObj.addEventListener('offline', notifyOffline);
30 ReachabilityNavigator._observers.push(observer);
31 return function () {
32 globalObj.removeEventListener('online', notifyOnline);
33 globalObj.removeEventListener('offline', notifyOffline);
34 ReachabilityNavigator._observers = ReachabilityNavigator._observers.filter(function (_observer) { return _observer !== observer; });
35 };
36 });
37 };
38 // expose observers to simulate offline mode for integration testing
39 ReachabilityNavigator._observerOverride = function (status) {
40 var e_1, _a;
41 var _loop_1 = function (observer) {
42 if (observer.closed) {
43 ReachabilityNavigator._observers = ReachabilityNavigator._observers.filter(function (_observer) { return _observer !== observer; });
44 return "continue";
45 }
46 observer.next(status);
47 };
48 try {
49 for (var _b = __values(ReachabilityNavigator._observers), _c = _b.next(); !_c.done; _c = _b.next()) {
50 var observer = _c.value;
51 _loop_1(observer);
52 }
53 }
54 catch (e_1_1) { e_1 = { error: e_1_1 }; }
55 finally {
56 try {
57 if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
58 }
59 finally { if (e_1) throw e_1.error; }
60 }
61 };
62 ReachabilityNavigator._observers = [];
63 return ReachabilityNavigator;
64}());
65export default ReachabilityNavigator;