UNPKG

6.78 kBJavaScriptView Raw
1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3import { __assign } from "tslib";
4import Observable from 'zen-observable-ts';
5import { ConnectionState } from '../types/PubSub';
6import { ReachabilityMonitor } from './ReachabilityMonitor';
7export var CONNECTION_CHANGE = {
8 KEEP_ALIVE_MISSED: { keepAliveState: 'unhealthy' },
9 KEEP_ALIVE: { keepAliveState: 'healthy' },
10 CONNECTION_ESTABLISHED: { connectionState: 'connected' },
11 CONNECTION_FAILED: {
12 intendedConnectionState: 'disconnected',
13 connectionState: 'disconnected',
14 },
15 CLOSING_CONNECTION: { intendedConnectionState: 'disconnected' },
16 OPENING_CONNECTION: {
17 intendedConnectionState: 'connected',
18 connectionState: 'connecting',
19 },
20 CLOSED: { connectionState: 'disconnected' },
21 ONLINE: { networkState: 'connected' },
22 OFFLINE: { networkState: 'disconnected' },
23};
24var ConnectionStateMonitor = /** @class */ (function () {
25 function ConnectionStateMonitor() {
26 var _this = this;
27 this._networkMonitoringSubscription = undefined;
28 this._linkedConnectionState = {
29 networkState: 'connected',
30 connectionState: 'disconnected',
31 intendedConnectionState: 'disconnected',
32 keepAliveState: 'healthy',
33 };
34 // Attempt to update the state with the current actual network state
35 this._initialNetworkStateSubscription = ReachabilityMonitor().subscribe(function (_a) {
36 var online = _a.online;
37 var _b;
38 _this.record(online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE);
39 (_b = _this._initialNetworkStateSubscription) === null || _b === void 0 ? void 0 : _b.unsubscribe();
40 });
41 this._linkedConnectionStateObservable =
42 new Observable(function (connectionStateObserver) {
43 connectionStateObserver.next(_this._linkedConnectionState);
44 _this._linkedConnectionStateObserver = connectionStateObserver;
45 });
46 }
47 /**
48 * Turn network state monitoring on if it isn't on already
49 */
50 ConnectionStateMonitor.prototype.enableNetworkMonitoring = function () {
51 var _this = this;
52 var _a;
53 // If no initial network state was discovered, stop trying
54 (_a = this._initialNetworkStateSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
55 // Maintain the network state based on the reachability monitor
56 if (this._networkMonitoringSubscription === undefined) {
57 this._networkMonitoringSubscription = ReachabilityMonitor().subscribe(function (_a) {
58 var online = _a.online;
59 _this.record(online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE);
60 });
61 }
62 };
63 /**
64 * Turn network state monitoring off if it isn't off already
65 */
66 ConnectionStateMonitor.prototype.disableNetworkMonitoring = function () {
67 var _a;
68 (_a = this._networkMonitoringSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
69 this._networkMonitoringSubscription = undefined;
70 };
71 Object.defineProperty(ConnectionStateMonitor.prototype, "connectionStateObservable", {
72 /**
73 * Get the observable that allows us to monitor the connection state
74 *
75 * @returns {Observable<ConnectionState>} - The observable that emits ConnectionState updates
76 */
77 get: function () {
78 var _this = this;
79 var previous;
80 // The linked state aggregates state changes to any of the network, connection,
81 // intendedConnection and keepAliveHealth. Some states will change these independent
82 // states without changing the overall connection state.
83 // After translating from linked states to ConnectionState, then remove any duplicates
84 return this._linkedConnectionStateObservable
85 .map(function (value) {
86 return _this.connectionStatesTranslator(value);
87 })
88 .filter(function (current) {
89 var toInclude = current !== previous;
90 previous = current;
91 return toInclude;
92 });
93 },
94 enumerable: true,
95 configurable: true
96 });
97 /*
98 * Updates local connection state and emits the full state to the observer.
99 */
100 ConnectionStateMonitor.prototype.record = function (statusUpdates) {
101 // Maintain the network monitor
102 if (statusUpdates.intendedConnectionState === 'connected') {
103 this.enableNetworkMonitoring();
104 }
105 else if (statusUpdates.intendedConnectionState === 'disconnected') {
106 this.disableNetworkMonitoring();
107 }
108 // Maintain the socket state
109 var newSocketStatus = __assign(__assign({}, this._linkedConnectionState), statusUpdates);
110 this._linkedConnectionState = __assign({}, newSocketStatus);
111 this._linkedConnectionStateObserver.next(this._linkedConnectionState);
112 };
113 /*
114 * Translate the ConnectionState structure into a specific ConnectionState string literal union
115 */
116 ConnectionStateMonitor.prototype.connectionStatesTranslator = function (_a) {
117 var connectionState = _a.connectionState, networkState = _a.networkState, intendedConnectionState = _a.intendedConnectionState, keepAliveState = _a.keepAliveState;
118 if (connectionState === 'connected' && networkState === 'disconnected')
119 return ConnectionState.ConnectedPendingNetwork;
120 if (connectionState === 'connected' &&
121 intendedConnectionState === 'disconnected')
122 return ConnectionState.ConnectedPendingDisconnect;
123 if (connectionState === 'disconnected' &&
124 intendedConnectionState === 'connected' &&
125 networkState === 'disconnected')
126 return ConnectionState.ConnectionDisruptedPendingNetwork;
127 if (connectionState === 'disconnected' &&
128 intendedConnectionState === 'connected')
129 return ConnectionState.ConnectionDisrupted;
130 if (connectionState === 'connected' && keepAliveState === 'unhealthy')
131 return ConnectionState.ConnectedPendingKeepAlive;
132 // All remaining states directly correspond to the connection state
133 if (connectionState === 'connecting')
134 return ConnectionState.Connecting;
135 if (connectionState === 'disconnected')
136 return ConnectionState.Disconnected;
137 return ConnectionState.Connected;
138 };
139 return ConnectionStateMonitor;
140}());
141export { ConnectionStateMonitor };
142//# sourceMappingURL=ConnectionStateMonitor.js.map
\No newline at end of file