1 |
|
2 |
|
3 | import { __assign } from "tslib";
|
4 | import Observable from 'zen-observable-ts';
|
5 | import { ConnectionState } from '../types/PubSub';
|
6 | import { ReachabilityMonitor } from './ReachabilityMonitor';
|
7 | export 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 | };
|
24 | var ConnectionStateMonitor = (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 |
|
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 |
|
49 |
|
50 | ConnectionStateMonitor.prototype.enableNetworkMonitoring = function () {
|
51 | var _this = this;
|
52 | var _a;
|
53 |
|
54 | (_a = this._initialNetworkStateSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
|
55 |
|
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 |
|
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 |
|
74 |
|
75 |
|
76 |
|
77 | get: function () {
|
78 | var _this = this;
|
79 | var previous;
|
80 |
|
81 |
|
82 |
|
83 |
|
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 |
|
99 |
|
100 | ConnectionStateMonitor.prototype.record = function (statusUpdates) {
|
101 |
|
102 | if (statusUpdates.intendedConnectionState === 'connected') {
|
103 | this.enableNetworkMonitoring();
|
104 | }
|
105 | else if (statusUpdates.intendedConnectionState === 'disconnected') {
|
106 | this.disableNetworkMonitoring();
|
107 | }
|
108 |
|
109 | var newSocketStatus = __assign(__assign({}, this._linkedConnectionState), statusUpdates);
|
110 | this._linkedConnectionState = __assign({}, newSocketStatus);
|
111 | this._linkedConnectionStateObserver.next(this._linkedConnectionState);
|
112 | };
|
113 | |
114 |
|
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 |
|
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 | }());
|
141 | export { ConnectionStateMonitor };
|
142 |
|
\ | No newline at end of file |