@aws-amplify/pubsub 4.2.6-custom-pk.97 → 4.2.6-custom-pk.98

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/CHANGELOG.md +28 -108
  2. package/dist/aws-amplify-pubsub.js +506 -141
  3. package/dist/aws-amplify-pubsub.js.map +1 -1
  4. package/dist/aws-amplify-pubsub.min.js +2 -2
  5. package/dist/aws-amplify-pubsub.min.js.map +1 -1
  6. package/lib/Providers/AWSAppSyncRealTimeProvider/index.d.ts +4 -1
  7. package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +41 -12
  8. package/lib/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
  9. package/lib/Providers/MqttOverWSProvider.d.ts +1 -0
  10. package/lib/Providers/MqttOverWSProvider.js +23 -1
  11. package/lib/Providers/MqttOverWSProvider.js.map +1 -1
  12. package/lib/Providers/{AWSAppSyncRealTimeProvider/constants.d.ts → constants.d.ts} +4 -0
  13. package/lib/Providers/{AWSAppSyncRealTimeProvider/constants.js → constants.js} +4 -0
  14. package/lib/Providers/constants.js.map +1 -0
  15. package/lib/index.d.ts +2 -0
  16. package/lib/index.js +3 -0
  17. package/lib/index.js.map +1 -1
  18. package/lib/types/index.d.ts +11 -0
  19. package/lib/types/index.js +36 -0
  20. package/lib/types/index.js.map +1 -1
  21. package/lib/utils/ConnectionStateMonitor.d.ts +40 -0
  22. package/lib/utils/ConnectionStateMonitor.js +157 -0
  23. package/lib/utils/ConnectionStateMonitor.js.map +1 -0
  24. package/lib/utils/ReachabilityMonitor/index.d.ts +3 -0
  25. package/lib/utils/ReachabilityMonitor/index.js +5 -0
  26. package/lib/utils/ReachabilityMonitor/index.js.map +1 -0
  27. package/lib/utils/ReachabilityMonitor/index.native.d.ts +3 -0
  28. package/lib/utils/ReachabilityMonitor/index.native.js +11 -0
  29. package/lib/utils/ReachabilityMonitor/index.native.js.map +1 -0
  30. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +4 -1
  31. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +42 -13
  32. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
  33. package/lib-esm/Providers/MqttOverWSProvider.d.ts +1 -0
  34. package/lib-esm/Providers/MqttOverWSProvider.js +24 -2
  35. package/lib-esm/Providers/MqttOverWSProvider.js.map +1 -1
  36. package/lib-esm/Providers/{AWSAppSyncRealTimeProvider/constants.d.ts → constants.d.ts} +4 -0
  37. package/lib-esm/Providers/{AWSAppSyncRealTimeProvider/constants.js → constants.js} +4 -0
  38. package/lib-esm/Providers/constants.js.map +1 -0
  39. package/lib-esm/index.d.ts +2 -0
  40. package/lib-esm/index.js +2 -0
  41. package/lib-esm/index.js.map +1 -1
  42. package/lib-esm/types/index.d.ts +11 -0
  43. package/lib-esm/types/index.js +36 -0
  44. package/lib-esm/types/index.js.map +1 -1
  45. package/lib-esm/utils/ConnectionStateMonitor.d.ts +40 -0
  46. package/lib-esm/utils/ConnectionStateMonitor.js +152 -0
  47. package/lib-esm/utils/ConnectionStateMonitor.js.map +1 -0
  48. package/lib-esm/utils/ReachabilityMonitor/index.d.ts +3 -0
  49. package/lib-esm/utils/ReachabilityMonitor/index.js +3 -0
  50. package/lib-esm/utils/ReachabilityMonitor/index.js.map +1 -0
  51. package/lib-esm/utils/ReachabilityMonitor/index.native.d.ts +3 -0
  52. package/lib-esm/utils/ReachabilityMonitor/index.native.js +6 -0
  53. package/lib-esm/utils/ReachabilityMonitor/index.native.js.map +1 -0
  54. package/package.json +5 -5
  55. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +67 -15
  56. package/src/Providers/MqttOverWSProvider.ts +43 -2
  57. package/src/Providers/{AWSAppSyncRealTimeProvider/constants.ts → constants.ts} +5 -0
  58. package/src/index.ts +3 -0
  59. package/src/types/index.ts +36 -0
  60. package/src/utils/ConnectionStateMonitor.ts +192 -0
  61. package/src/utils/ReachabilityMonitor/index.native.ts +5 -0
  62. package/src/utils/ReachabilityMonitor/index.ts +3 -0
  63. package/lib/Providers/AWSAppSyncRealTimeProvider/constants.js.map +0 -1
  64. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/constants.js.map +0 -1
@@ -19,3 +19,39 @@ export interface SubscriptionObserver<T> {
19
19
  error(errorValue: any): void;
20
20
  complete(): void;
21
21
  }
22
+
23
+ /** @enum {string} */
24
+ export enum ConnectionState {
25
+ /*
26
+ * The connection is alive and healthy
27
+ */
28
+ Connected = 'Connected',
29
+ /*
30
+ * The connection is alive, but the connection is offline
31
+ */
32
+ ConnectedPendingNetwork = 'ConnectedPendingNetwork',
33
+ /*
34
+ * The connection has been disconnected while in use
35
+ */
36
+ ConnectionDisrupted = 'ConnectionDisrupted',
37
+ /*
38
+ * The connection has been disconnected and the network is offline
39
+ */
40
+ ConnectionDisruptedPendingNetwork = 'ConnectionDisruptedPendingNetwork',
41
+ /*
42
+ * The connection is in the process of connecting
43
+ */
44
+ Connecting = 'Connecting',
45
+ /*
46
+ * The connection is not in use and is being disconnected
47
+ */
48
+ ConnectedPendingDisconnect = 'ConnectedPendingDisconnect',
49
+ /*
50
+ * The connection is not in use and has been disconnected
51
+ */
52
+ Disconnected = 'Disconnected',
53
+ /*
54
+ * The connection is alive, but a keep alive message has been missed
55
+ */
56
+ ConnectedPendingKeepAlive = 'ConnectedPendingKeepAlive',
57
+ }
@@ -0,0 +1,192 @@
1
+ /*
2
+ * Copyright 2017-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
+ * the License. A copy of the License is located at
6
+ *
7
+ * http://aws.amazon.com/apache2.0/
8
+ *
9
+ * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
+ * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
+ * and limitations under the License.
12
+ */
13
+
14
+ import { Reachability } from '@aws-amplify/core';
15
+ import Observable, { ZenObservable } from 'zen-observable-ts';
16
+ import { ConnectionState } from '../index';
17
+ import { ReachabilityMonitor } from './ReachabilityMonitor';
18
+
19
+ // Internal types for tracking different connection states
20
+ type LinkedConnectionState = 'connected' | 'disconnected';
21
+ type LinkedHealthState = 'healthy' | 'unhealthy';
22
+ type LinkedConnectionStates = {
23
+ networkState: LinkedConnectionState;
24
+ connectionState: LinkedConnectionState | 'connecting';
25
+ intendedConnectionState: LinkedConnectionState;
26
+ keepAliveState: LinkedHealthState;
27
+ };
28
+
29
+ export const CONNECTION_CHANGE: {
30
+ [key in
31
+ | 'KEEP_ALIVE_MISSED'
32
+ | 'KEEP_ALIVE'
33
+ | 'CONNECTION_ESTABLISHED'
34
+ | 'CONNECTION_FAILED'
35
+ | 'CLOSING_CONNECTION'
36
+ | 'OPENING_CONNECTION'
37
+ | 'CLOSED'
38
+ | 'ONLINE'
39
+ | 'OFFLINE']: Partial<LinkedConnectionStates>;
40
+ } = {
41
+ KEEP_ALIVE_MISSED: { keepAliveState: 'unhealthy' },
42
+ KEEP_ALIVE: { keepAliveState: 'healthy' },
43
+ CONNECTION_ESTABLISHED: { connectionState: 'connected' },
44
+ CONNECTION_FAILED: {
45
+ intendedConnectionState: 'disconnected',
46
+ connectionState: 'disconnected',
47
+ },
48
+ CLOSING_CONNECTION: { intendedConnectionState: 'disconnected' },
49
+ OPENING_CONNECTION: {
50
+ intendedConnectionState: 'connected',
51
+ connectionState: 'connecting',
52
+ },
53
+ CLOSED: { connectionState: 'disconnected' },
54
+ ONLINE: { networkState: 'connected' },
55
+ OFFLINE: { networkState: 'disconnected' },
56
+ };
57
+
58
+ export class ConnectionStateMonitor {
59
+ /**
60
+ * @private
61
+ */
62
+ private _linkedConnectionState: LinkedConnectionStates;
63
+ private _linkedConnectionStateObservable: Observable<LinkedConnectionStates>;
64
+ private _linkedConnectionStateObserver: ZenObservable.SubscriptionObserver<LinkedConnectionStates>;
65
+ private _networkMonitoringSubscription?: ZenObservable.Subscription;
66
+
67
+ constructor() {
68
+ this._networkMonitoringSubscription = undefined;
69
+ this._linkedConnectionState = {
70
+ networkState: 'connected',
71
+ connectionState: 'disconnected',
72
+ intendedConnectionState: 'disconnected',
73
+ keepAliveState: 'healthy',
74
+ };
75
+
76
+ this._linkedConnectionStateObservable =
77
+ new Observable<LinkedConnectionStates>(connectionStateObserver => {
78
+ connectionStateObserver.next(this._linkedConnectionState);
79
+ this._linkedConnectionStateObserver = connectionStateObserver;
80
+ });
81
+ }
82
+
83
+ /**
84
+ * Turn network state monitoring on if it isn't on already
85
+ */
86
+ private enableNetworkMonitoring() {
87
+ // Maintain the network state based on the reachability monitor
88
+ if (this._networkMonitoringSubscription === undefined) {
89
+ this._networkMonitoringSubscription = ReachabilityMonitor().subscribe(
90
+ ({ online }) => {
91
+ this.record(
92
+ online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE
93
+ );
94
+ }
95
+ );
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Turn network state monitoring off if it isn't off already
101
+ */
102
+ private disableNetworkMonitoring() {
103
+ this._networkMonitoringSubscription?.unsubscribe();
104
+ this._networkMonitoringSubscription = undefined;
105
+ }
106
+
107
+ /**
108
+ * Get the observable that allows us to monitor the connection state
109
+ *
110
+ * @returns {Observable<ConnectionState>} - The observable that emits ConnectionState updates
111
+ */
112
+ public get connectionStateObservable(): Observable<ConnectionState> {
113
+ let previous: ConnectionState;
114
+
115
+ // The linked state aggregates state changes to any of the network, connection,
116
+ // intendedConnection and keepAliveHealth. Some states will change these independent
117
+ // states without changing the overall connection state.
118
+
119
+ // After translating from linked states to ConnectionState, then remove any duplicates
120
+ return this._linkedConnectionStateObservable
121
+ .map(value => {
122
+ return this.connectionStatesTranslator(value);
123
+ })
124
+ .filter(current => {
125
+ const toInclude = current !== previous;
126
+ previous = current;
127
+ return toInclude;
128
+ });
129
+ }
130
+
131
+ /*
132
+ * Updates local connection state and emits the full state to the observer.
133
+ */
134
+ record(statusUpdates: Partial<LinkedConnectionStates>) {
135
+ // Maintain the network monitor
136
+ if (statusUpdates.intendedConnectionState === 'connected') {
137
+ this.enableNetworkMonitoring();
138
+ } else if (statusUpdates.intendedConnectionState === 'disconnected') {
139
+ this.disableNetworkMonitoring();
140
+ }
141
+
142
+ // Maintain the socket state
143
+ const newSocketStatus = {
144
+ ...this._linkedConnectionState,
145
+ ...statusUpdates,
146
+ };
147
+
148
+ this._linkedConnectionState = { ...newSocketStatus };
149
+
150
+ this._linkedConnectionStateObserver.next(this._linkedConnectionState);
151
+ }
152
+
153
+ /*
154
+ * Translate the ConnectionState structure into a specific ConnectionState string literal union
155
+ */
156
+ private connectionStatesTranslator({
157
+ connectionState,
158
+ networkState,
159
+ intendedConnectionState,
160
+ keepAliveState,
161
+ }: LinkedConnectionStates): ConnectionState {
162
+ if (connectionState === 'connected' && networkState === 'disconnected')
163
+ return ConnectionState.ConnectedPendingNetwork;
164
+
165
+ if (
166
+ connectionState === 'connected' &&
167
+ intendedConnectionState === 'disconnected'
168
+ )
169
+ return ConnectionState.ConnectedPendingDisconnect;
170
+
171
+ if (
172
+ connectionState === 'disconnected' &&
173
+ intendedConnectionState === 'connected' &&
174
+ networkState === 'disconnected'
175
+ )
176
+ return ConnectionState.ConnectionDisruptedPendingNetwork;
177
+
178
+ if (
179
+ connectionState === 'disconnected' &&
180
+ intendedConnectionState === 'connected'
181
+ )
182
+ return ConnectionState.ConnectionDisrupted;
183
+
184
+ if (connectionState === 'connected' && keepAliveState === 'unhealthy')
185
+ return ConnectionState.ConnectedPendingKeepAlive;
186
+
187
+ // All remaining states directly correspond to the connection state
188
+ if (connectionState === 'connecting') return ConnectionState.Connecting;
189
+ if (connectionState === 'disconnected') return ConnectionState.Disconnected;
190
+ return ConnectionState.Connected;
191
+ }
192
+ }
@@ -0,0 +1,5 @@
1
+ import { Reachability } from '@aws-amplify/core';
2
+ import { default as NetInfo } from '@react-native-community/netinfo';
3
+
4
+ export const ReachabilityMonitor = () =>
5
+ new Reachability().networkMonitor(NetInfo);
@@ -0,0 +1,3 @@
1
+ import { Reachability } from '@aws-amplify/core';
2
+
3
+ export const ReachabilityMonitor = () => new Reachability().networkMonitor();
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/Providers/AWSAppSyncRealTimeProvider/constants.ts"],"names":[],"mappings":";;AAAa,QAAA,YAAY,GAAG,IAAI,CAAC;AAEpB,QAAA,mBAAmB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AAEnD,IAAY,aAmDX;AAnDD,WAAY,aAAa;IACxB;;;OAGG;IACH,wDAAuC,CAAA;IACvC;;;OAGG;IACH,0DAAyC,CAAA;IACzC;;;OAGG;IACH,sDAAqC,CAAA;IACrC;;;OAGG;IACH,oCAAmB,CAAA;IACnB;;;OAGG;IACH,4CAA2B,CAAA;IAC3B;;;OAGG;IACH,kCAAiB,CAAA;IACjB;;;OAGG;IACH,iDAAgC,CAAA;IAChC;;;OAGG;IACH,kCAAiB,CAAA;IACjB;;;OAGG;IACH,0CAAyB,CAAA;IACzB;;;OAGG;IACH,oCAAmB,CAAA;AACpB,CAAC,EAnDW,aAAa,GAAb,qBAAa,KAAb,qBAAa,QAmDxB;AAED,IAAY,mBAIX;AAJD,WAAY,mBAAmB;IAC9B,mEAAO,CAAA;IACP,uEAAS,CAAA;IACT,iEAAM,CAAA;AACP,CAAC,EAJW,mBAAmB,GAAnB,2BAAmB,KAAnB,2BAAmB,QAI9B;AAED,IAAY,aAIX;AAJD,WAAY,aAAa;IACxB,qDAAM,CAAA;IACN,mDAAK,CAAA;IACL,6DAAU,CAAA;AACX,CAAC,EAJW,aAAa,GAAb,qBAAa,KAAb,qBAAa,QAIxB;AAEY,QAAA,cAAc,GAAG,CAC7B,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,GAAG,KAAK,UAAU;IAChE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,iBAAiB,CAAC;IAC/B,CAAC,CAAC,mBAAmB,CACZ,CAAC;AAEC,QAAA,4BAA4B,GAAG;IAC3C,MAAM,EAAE,mCAAmC;IAC3C,kBAAkB,EAAE,SAAS;IAC7B,cAAc,EAAE,iCAAiC;CACjD,CAAC;AAEF;;GAEG;AACU,QAAA,uBAAuB,GAAG,KAAK,CAAC;AAE7C;;GAEG;AACU,QAAA,iBAAiB,GAAG,KAAK,CAAC;AAEvC;;GAEG;AACU,QAAA,0BAA0B,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/Providers/AWSAppSyncRealTimeProvider/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,IAAM,YAAY,GAAG,IAAI,CAAC;AAEjC,MAAM,CAAC,IAAM,mBAAmB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AAEnD,MAAM,CAAN,IAAY,aAmDX;AAnDD,WAAY,aAAa;IACxB;;;OAGG;IACH,wDAAuC,CAAA;IACvC;;;OAGG;IACH,0DAAyC,CAAA;IACzC;;;OAGG;IACH,sDAAqC,CAAA;IACrC;;;OAGG;IACH,oCAAmB,CAAA;IACnB;;;OAGG;IACH,4CAA2B,CAAA;IAC3B;;;OAGG;IACH,kCAAiB,CAAA;IACjB;;;OAGG;IACH,iDAAgC,CAAA;IAChC;;;OAGG;IACH,kCAAiB,CAAA;IACjB;;;OAGG;IACH,0CAAyB,CAAA;IACzB;;;OAGG;IACH,oCAAmB,CAAA;AACpB,CAAC,EAnDW,aAAa,KAAb,aAAa,QAmDxB;AAED,MAAM,CAAN,IAAY,mBAIX;AAJD,WAAY,mBAAmB;IAC9B,mEAAO,CAAA;IACP,uEAAS,CAAA;IACT,iEAAM,CAAA;AACP,CAAC,EAJW,mBAAmB,KAAnB,mBAAmB,QAI9B;AAED,MAAM,CAAN,IAAY,aAIX;AAJD,WAAY,aAAa;IACxB,qDAAM,CAAA;IACN,mDAAK,CAAA;IACL,6DAAU,CAAA;AACX,CAAC,EAJW,aAAa,KAAb,aAAa,QAIxB;AAED,MAAM,CAAC,IAAM,cAAc,GAAG,CAC7B,OAAO,MAAM,KAAK,WAAW,IAAI,OAAO,MAAM,CAAC,GAAG,KAAK,UAAU;IAChE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,iBAAiB,CAAC;IAC/B,CAAC,CAAC,mBAAmB,CACZ,CAAC;AAEZ,MAAM,CAAC,IAAM,4BAA4B,GAAG;IAC3C,MAAM,EAAE,mCAAmC;IAC3C,kBAAkB,EAAE,SAAS;IAC7B,cAAc,EAAE,iCAAiC;CACjD,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,IAAM,uBAAuB,GAAG,KAAK,CAAC;AAE7C;;GAEG;AACH,MAAM,CAAC,IAAM,iBAAiB,GAAG,KAAK,CAAC;AAEvC;;GAEG;AACH,MAAM,CAAC,IAAM,0BAA0B,GAAG,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC"}