UNPKG

5.18 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _nativeInterface = _interopRequireDefault(require("./nativeInterface"));
9
10var _internetReachability = _interopRequireDefault(require("./internetReachability"));
11
12var PrivateTypes = _interopRequireWildcard(require("./privateTypes"));
13
14function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
16function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
21
22class State {
23 constructor(configuration) {
24 _defineProperty(this, "_nativeEventSubscription", null);
25
26 _defineProperty(this, "_subscriptions", new Set());
27
28 _defineProperty(this, "_latestState", null);
29
30 _defineProperty(this, "_internetReachability", void 0);
31
32 _defineProperty(this, "_handleNativeStateUpdate", state => {
33 // Update the internet reachability module
34 this._internetReachability.update(state); // Convert the state from native to JS shape
35
36
37 const convertedState = this._convertState(state); // Update the listeners
38
39
40 this._latestState = convertedState;
41
42 this._subscriptions.forEach(handler => handler(convertedState));
43 });
44
45 _defineProperty(this, "_handleInternetReachabilityUpdate", isInternetReachable => {
46 if (!this._latestState) {
47 return;
48 }
49
50 const nextState = { ...this._latestState,
51 isInternetReachable
52 };
53 this._latestState = nextState;
54
55 this._subscriptions.forEach(handler => handler(nextState));
56 });
57
58 _defineProperty(this, "_fetchCurrentState", async requestedInterface => {
59 const state = await _nativeInterface.default.getCurrentState(requestedInterface); // Update the internet reachability module
60
61 this._internetReachability.update(state); // Convert and store the new state
62
63
64 const convertedState = this._convertState(state);
65
66 if (!requestedInterface) {
67 this._latestState = convertedState;
68
69 this._subscriptions.forEach(handler => handler(convertedState));
70 }
71
72 return convertedState;
73 });
74
75 _defineProperty(this, "_convertState", input => {
76 if (typeof input.isInternetReachable === 'boolean') {
77 return input;
78 } else {
79 return { ...input,
80 isInternetReachable: this._internetReachability.currentState()
81 };
82 }
83 });
84
85 _defineProperty(this, "latest", requestedInterface => {
86 if (requestedInterface) {
87 return this._fetchCurrentState(requestedInterface);
88 } else if (this._latestState) {
89 return Promise.resolve(this._latestState);
90 } else {
91 return this._fetchCurrentState();
92 }
93 });
94
95 _defineProperty(this, "add", handler => {
96 // Add the subscription handler to our set
97 this._subscriptions.add(handler); // Send it the latest data we have
98
99
100 if (this._latestState) {
101 handler(this._latestState);
102 } else {
103 this.latest().then(handler);
104 }
105 });
106
107 _defineProperty(this, "remove", handler => {
108 this._subscriptions.delete(handler);
109 });
110
111 _defineProperty(this, "tearDown", () => {
112 if (this._internetReachability) {
113 this._internetReachability.tearDown();
114 }
115
116 if (this._nativeEventSubscription) {
117 this._nativeEventSubscription.remove();
118 }
119
120 this._subscriptions.clear();
121 });
122
123 // Add the listener to the internet connectivity events
124 this._internetReachability = new _internetReachability.default(configuration, this._handleInternetReachabilityUpdate); // Add the subscription to the native events
125
126 this._nativeEventSubscription = _nativeInterface.default.eventEmitter.addListener(PrivateTypes.DEVICE_CONNECTIVITY_EVENT, this._handleNativeStateUpdate); // Fetch the current state from the native module
127
128 this._fetchCurrentState();
129 }
130
131}
132
133exports.default = State;
134//# sourceMappingURL=state.js.map
\No newline at end of file