@aws-amplify/pubsub 4.4.9 → 4.4.10-next.13

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 (84) hide show
  1. package/CHANGELOG.md +73 -0
  2. package/dist/aws-amplify-pubsub.js +713 -378
  3. package/dist/aws-amplify-pubsub.js.map +1 -1
  4. package/dist/aws-amplify-pubsub.min.js +3 -3
  5. package/dist/aws-amplify-pubsub.min.js.map +1 -1
  6. package/lib/Providers/AWSAppSyncRealTimeProvider/index.d.ts +10 -2
  7. package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +133 -90
  8. package/lib/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
  9. package/lib/Providers/MqttOverWSProvider.d.ts +5 -1
  10. package/lib/Providers/MqttOverWSProvider.js +152 -87
  11. package/lib/Providers/MqttOverWSProvider.js.map +1 -1
  12. package/lib/Providers/PubSubProvider.d.ts +1 -1
  13. package/lib/Providers/{AWSAppSyncRealTimeProvider/constants.d.ts → constants.d.ts} +9 -0
  14. package/lib/Providers/{AWSAppSyncRealTimeProvider/constants.js → constants.js} +9 -0
  15. package/lib/Providers/constants.js.map +1 -0
  16. package/lib/index.d.ts +3 -10
  17. package/lib/index.js +3 -10
  18. package/lib/index.js.map +1 -1
  19. package/lib/types/Provider.d.ts +10 -1
  20. package/lib/types/PubSub.d.ts +22 -8
  21. package/lib/types/PubSub.js +56 -0
  22. package/lib/types/PubSub.js.map +1 -1
  23. package/lib/types/index.d.ts +0 -17
  24. package/lib/types/index.js +4 -36
  25. package/lib/types/index.js.map +1 -1
  26. package/lib/utils/ConnectionStateMonitor.d.ts +2 -1
  27. package/lib/utils/ConnectionStateMonitor.js +21 -13
  28. package/lib/utils/ConnectionStateMonitor.js.map +1 -1
  29. package/lib/utils/ReachabilityMonitor/index.d.ts +3 -0
  30. package/lib/utils/ReachabilityMonitor/index.js +5 -0
  31. package/lib/utils/ReachabilityMonitor/index.js.map +1 -0
  32. package/lib/utils/ReachabilityMonitor/index.native.d.ts +3 -0
  33. package/lib/utils/ReachabilityMonitor/index.native.js +11 -0
  34. package/lib/utils/ReachabilityMonitor/index.native.js.map +1 -0
  35. package/lib/utils/ReconnectionMonitor.d.ts +29 -0
  36. package/lib/utils/ReconnectionMonitor.js +73 -0
  37. package/lib/utils/ReconnectionMonitor.js.map +1 -0
  38. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +10 -2
  39. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +129 -86
  40. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
  41. package/lib-esm/Providers/MqttOverWSProvider.d.ts +5 -1
  42. package/lib-esm/Providers/MqttOverWSProvider.js +153 -88
  43. package/lib-esm/Providers/MqttOverWSProvider.js.map +1 -1
  44. package/lib-esm/Providers/PubSubProvider.d.ts +1 -1
  45. package/lib-esm/Providers/{AWSAppSyncRealTimeProvider/constants.d.ts → constants.d.ts} +9 -0
  46. package/lib-esm/Providers/{AWSAppSyncRealTimeProvider/constants.js → constants.js} +9 -0
  47. package/lib-esm/Providers/constants.js.map +1 -0
  48. package/lib-esm/index.d.ts +3 -10
  49. package/lib-esm/index.js +3 -11
  50. package/lib-esm/index.js.map +1 -1
  51. package/lib-esm/types/Provider.d.ts +10 -1
  52. package/lib-esm/types/PubSub.d.ts +22 -8
  53. package/lib-esm/types/PubSub.js +56 -0
  54. package/lib-esm/types/PubSub.js.map +1 -1
  55. package/lib-esm/types/index.d.ts +0 -17
  56. package/lib-esm/types/index.js +1 -36
  57. package/lib-esm/types/index.js.map +1 -1
  58. package/lib-esm/utils/ConnectionStateMonitor.d.ts +2 -1
  59. package/lib-esm/utils/ConnectionStateMonitor.js +13 -5
  60. package/lib-esm/utils/ConnectionStateMonitor.js.map +1 -1
  61. package/lib-esm/utils/ReachabilityMonitor/index.d.ts +3 -0
  62. package/lib-esm/utils/ReachabilityMonitor/index.js +3 -0
  63. package/lib-esm/utils/ReachabilityMonitor/index.js.map +1 -0
  64. package/lib-esm/utils/ReachabilityMonitor/index.native.d.ts +3 -0
  65. package/lib-esm/utils/ReachabilityMonitor/index.native.js +6 -0
  66. package/lib-esm/utils/ReachabilityMonitor/index.native.js.map +1 -0
  67. package/lib-esm/utils/ReconnectionMonitor.d.ts +29 -0
  68. package/lib-esm/utils/ReconnectionMonitor.js +71 -0
  69. package/lib-esm/utils/ReconnectionMonitor.js.map +1 -0
  70. package/package.json +5 -5
  71. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +154 -90
  72. package/src/Providers/MqttOverWSProvider.ts +120 -53
  73. package/src/Providers/PubSubProvider.ts +1 -1
  74. package/src/Providers/{AWSAppSyncRealTimeProvider/constants.ts → constants.ts} +12 -0
  75. package/src/index.ts +3 -10
  76. package/src/types/Provider.ts +13 -1
  77. package/src/types/PubSub.ts +47 -8
  78. package/src/types/index.ts +0 -43
  79. package/src/utils/ConnectionStateMonitor.ts +20 -5
  80. package/src/utils/ReachabilityMonitor/index.native.ts +5 -0
  81. package/src/utils/ReachabilityMonitor/index.ts +3 -0
  82. package/src/utils/ReconnectionMonitor.ts +74 -0
  83. package/lib/Providers/AWSAppSyncRealTimeProvider/constants.js.map +0 -1
  84. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/constants.js.map +0 -1
@@ -1,19 +1,2 @@
1
1
  export * from './Provider';
2
2
  export * from './PubSub';
3
- export interface SubscriptionObserver<T> {
4
- closed: boolean;
5
- next(value: T): void;
6
- error(errorValue: any): void;
7
- complete(): void;
8
- }
9
- /** @enum {string} */
10
- export declare enum ConnectionState {
11
- Connected = "Connected",
12
- ConnectedPendingNetwork = "ConnectedPendingNetwork",
13
- ConnectionDisrupted = "ConnectionDisrupted",
14
- ConnectionDisruptedPendingNetwork = "ConnectionDisruptedPendingNetwork",
15
- Connecting = "Connecting",
16
- ConnectedPendingDisconnect = "ConnectedPendingDisconnect",
17
- Disconnected = "Disconnected",
18
- ConnectedPendingKeepAlive = "ConnectedPendingKeepAlive"
19
- }
@@ -1,37 +1,2 @@
1
- /** @enum {string} */
2
- export var ConnectionState;
3
- (function (ConnectionState) {
4
- /*
5
- * The connection is alive and healthy
6
- */
7
- ConnectionState["Connected"] = "Connected";
8
- /*
9
- * The connection is alive, but the connection is offline
10
- */
11
- ConnectionState["ConnectedPendingNetwork"] = "ConnectedPendingNetwork";
12
- /*
13
- * The connection has been disconnected while in use
14
- */
15
- ConnectionState["ConnectionDisrupted"] = "ConnectionDisrupted";
16
- /*
17
- * The connection has been disconnected and the network is offline
18
- */
19
- ConnectionState["ConnectionDisruptedPendingNetwork"] = "ConnectionDisruptedPendingNetwork";
20
- /*
21
- * The connection is in the process of connecting
22
- */
23
- ConnectionState["Connecting"] = "Connecting";
24
- /*
25
- * The connection is not in use and is being disconnected
26
- */
27
- ConnectionState["ConnectedPendingDisconnect"] = "ConnectedPendingDisconnect";
28
- /*
29
- * The connection is not in use and has been disconnected
30
- */
31
- ConnectionState["Disconnected"] = "Disconnected";
32
- /*
33
- * The connection is alive, but a keep alive message has been missed
34
- */
35
- ConnectionState["ConnectedPendingKeepAlive"] = "ConnectedPendingKeepAlive";
36
- })(ConnectionState || (ConnectionState = {}));
1
+ export * from './PubSub';
37
2
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAsBA,qBAAqB;AACrB,MAAM,CAAN,IAAY,eAiCX;AAjCD,WAAY,eAAe;IAC1B;;OAEG;IACH,0CAAuB,CAAA;IACvB;;OAEG;IACH,sEAAmD,CAAA;IACnD;;OAEG;IACH,8DAA2C,CAAA;IAC3C;;OAEG;IACH,0FAAuE,CAAA;IACvE;;OAEG;IACH,4CAAyB,CAAA;IACzB;;OAEG;IACH,4EAAyD,CAAA;IACzD;;OAEG;IACH,gDAA6B,CAAA;IAC7B;;OAEG;IACH,0EAAuD,CAAA;AACxD,CAAC,EAjCW,eAAe,KAAf,eAAe,QAiC1B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAaA,cAAc,UAAU,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import Observable from 'zen-observable-ts';
2
- import { ConnectionState } from '../index';
2
+ import { ConnectionState } from '../types/PubSub';
3
3
  declare type LinkedConnectionState = 'connected' | 'disconnected';
4
4
  declare type LinkedHealthState = 'healthy' | 'unhealthy';
5
5
  declare type LinkedConnectionStates = {
@@ -19,6 +19,7 @@ export declare class ConnectionStateMonitor {
19
19
  private _linkedConnectionStateObservable;
20
20
  private _linkedConnectionStateObserver;
21
21
  private _networkMonitoringSubscription?;
22
+ private _initialNetworkStateSubscription?;
22
23
  constructor();
23
24
  /**
24
25
  * Turn network state monitoring on if it isn't on already
@@ -21,9 +21,9 @@ var __assign = (this && this.__assign) || function () {
21
21
  };
22
22
  return __assign.apply(this, arguments);
23
23
  };
24
- import { Reachability } from '@aws-amplify/core';
25
24
  import Observable from 'zen-observable-ts';
26
- import { ConnectionState } from '../index';
25
+ import { ConnectionState } from '../types/PubSub';
26
+ import { ReachabilityMonitor } from './ReachabilityMonitor';
27
27
  export var CONNECTION_CHANGE = {
28
28
  KEEP_ALIVE_MISSED: { keepAliveState: 'unhealthy' },
29
29
  KEEP_ALIVE: { keepAliveState: 'healthy' },
@@ -51,6 +51,13 @@ var ConnectionStateMonitor = /** @class */ (function () {
51
51
  intendedConnectionState: 'disconnected',
52
52
  keepAliveState: 'healthy',
53
53
  };
54
+ // Attempt to update the state with the current actual network state
55
+ this._initialNetworkStateSubscription = ReachabilityMonitor().subscribe(function (_a) {
56
+ var online = _a.online;
57
+ var _b;
58
+ _this.record(online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE);
59
+ (_b = _this._initialNetworkStateSubscription) === null || _b === void 0 ? void 0 : _b.unsubscribe();
60
+ });
54
61
  this._linkedConnectionStateObservable =
55
62
  new Observable(function (connectionStateObserver) {
56
63
  connectionStateObserver.next(_this._linkedConnectionState);
@@ -62,11 +69,12 @@ var ConnectionStateMonitor = /** @class */ (function () {
62
69
  */
63
70
  ConnectionStateMonitor.prototype.enableNetworkMonitoring = function () {
64
71
  var _this = this;
72
+ var _a;
73
+ // If no initial network state was discovered, stop trying
74
+ (_a = this._initialNetworkStateSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
65
75
  // Maintain the network state based on the reachability monitor
66
76
  if (this._networkMonitoringSubscription === undefined) {
67
- this._networkMonitoringSubscription = new Reachability()
68
- .networkMonitor()
69
- .subscribe(function (_a) {
77
+ this._networkMonitoringSubscription = ReachabilityMonitor().subscribe(function (_a) {
70
78
  var online = _a.online;
71
79
  _this.record(online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE);
72
80
  });
@@ -1 +1 @@
1
- {"version":3,"file":"ConnectionStateMonitor.js","sourceRoot":"","sources":["../../src/utils/ConnectionStateMonitor.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;;;;;;;;;;;;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,UAA6B,MAAM,mBAAmB,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAY3C,MAAM,CAAC,IAAM,iBAAiB,GAW1B;IACH,iBAAiB,EAAE,EAAE,cAAc,EAAE,WAAW,EAAE;IAClD,UAAU,EAAE,EAAE,cAAc,EAAE,SAAS,EAAE;IACzC,sBAAsB,EAAE,EAAE,eAAe,EAAE,WAAW,EAAE;IACxD,iBAAiB,EAAE;QAClB,uBAAuB,EAAE,cAAc;QACvC,eAAe,EAAE,cAAc;KAC/B;IACD,kBAAkB,EAAE,EAAE,uBAAuB,EAAE,cAAc,EAAE;IAC/D,kBAAkB,EAAE;QACnB,uBAAuB,EAAE,WAAW;QACpC,eAAe,EAAE,YAAY;KAC7B;IACD,MAAM,EAAE,EAAE,eAAe,EAAE,cAAc,EAAE;IAC3C,MA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B,uBAAuB,KAAK,cAAc;YAE1C,OAAO,eAAe,CAAC,0BAA0B,CAAC;QAEnD,IACC,eAAe,KAAK,cAAc;YAClC,uBAAuB,KAAK,WAAW;YACvC,YAAY,KAAK,cAAc;YAE/B,OAAO,eAAe,CAAC,iCAAiC,CAAC;QAE1D,IACC,eAAe,KAAK,cAAc;YAClC,uBAAuB,KAAK,WAAW;YAEvC,OAAO,eAAe,CAAC,mBAAmB,CAAC;QAE5C,IAAI,eAAe,KAAK,WAAW,IAAI,cAAc,KAAK,WAAW;YACpE,OAAO,eAAe,CAAC,yBAAyB,CAAC;QAElD,mEAAmE;QACnE,IAAI,eAAe,KAAK,YAAY;YAAE,OAAO,eAAe,CAAC,UAAU,CAAC;QACxE,IAAI,eAAe,KAAK,cAAc;YAAE,OAAO,eAAe,CAAC,YAAY,CAAC;QAC5E,OAAO,eAAe,CAAC,SAAS,CAAC;IAClC,CAAC;IACF,6BAAC;AAAD,CAAC,AApJD,IAoJC"}
@@ -0,0 +1,3 @@
1
+ export declare const ReachabilityMonitor: () => import("zen-observable-ts").default<{
2
+ online: boolean;
3
+ }>;
@@ -0,0 +1,3 @@
1
+ import { Reachability } from '@aws-amplify/core';
2
+ export var ReachabilityMonitor = function () { return new Reachability().networkMonitor(); };
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/ReachabilityMonitor/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,CAAC,IAAM,mBAAmB,GAAG,cAAM,OAAA,IAAI,YAAY,EAAE,CAAC,cAAc,EAAE,EAAnC,CAAmC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export declare const ReachabilityMonitor: () => import("zen-observable-ts").default<{
2
+ online: boolean;
3
+ }>;
@@ -0,0 +1,6 @@
1
+ import { Reachability } from '@aws-amplify/core';
2
+ import { default as NetInfo } from '@react-native-community/netinfo';
3
+ export var ReachabilityMonitor = function () {
4
+ return new Reachability().networkMonitor(NetInfo);
5
+ };
6
+ //# sourceMappingURL=index.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.native.js","sourceRoot":"","sources":["../../../src/utils/ReachabilityMonitor/index.native.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,iCAAiC,CAAC;AAErE,MAAM,CAAC,IAAM,mBAAmB,GAAG;IAClC,OAAA,IAAI,YAAY,EAAE,CAAC,cAAc,CAAC,OAAO,CAAC;AAA1C,CAA0C,CAAC"}
@@ -0,0 +1,29 @@
1
+ import { Observer } from 'zen-observable-ts';
2
+ export declare enum ReconnectEvent {
3
+ START_RECONNECT = "START_RECONNECT",
4
+ HALT_RECONNECT = "HALT_RECONNECT"
5
+ }
6
+ /**
7
+ * Captures the reconnect event logic used to determine when to reconnect to PubSub providers.
8
+ * Reconnnect attempts are delayed by 5 seconds to let the interface settle.
9
+ * Attempting to reconnect only once creates unrecoverable states when the network state isn't
10
+ * supported by the browser, so this keeps retrying every minute until halted.
11
+ */
12
+ export declare class ReconnectionMonitor {
13
+ private reconnectObservers;
14
+ private reconnectIntervalId?;
15
+ private reconnectSetTimeoutId?;
16
+ /**
17
+ * Add reconnect observer to the list of observers to alert on reconnect
18
+ */
19
+ addObserver(reconnectObserver: Observer<void>): void;
20
+ /**
21
+ * Given a reconnect event, start the appropriate behavior
22
+ */
23
+ record(event: ReconnectEvent): void;
24
+ /**
25
+ * Complete all reconnect observers
26
+ */
27
+ close(): void;
28
+ private _triggerReconnect;
29
+ }
@@ -0,0 +1,71 @@
1
+ import { RECONNECT_DELAY, RECONNECT_INTERVAL } from '../Providers/constants';
2
+ export var ReconnectEvent;
3
+ (function (ReconnectEvent) {
4
+ ReconnectEvent["START_RECONNECT"] = "START_RECONNECT";
5
+ ReconnectEvent["HALT_RECONNECT"] = "HALT_RECONNECT";
6
+ })(ReconnectEvent || (ReconnectEvent = {}));
7
+ /**
8
+ * Captures the reconnect event logic used to determine when to reconnect to PubSub providers.
9
+ * Reconnnect attempts are delayed by 5 seconds to let the interface settle.
10
+ * Attempting to reconnect only once creates unrecoverable states when the network state isn't
11
+ * supported by the browser, so this keeps retrying every minute until halted.
12
+ */
13
+ var ReconnectionMonitor = /** @class */ (function () {
14
+ function ReconnectionMonitor() {
15
+ this.reconnectObservers = [];
16
+ }
17
+ /**
18
+ * Add reconnect observer to the list of observers to alert on reconnect
19
+ */
20
+ ReconnectionMonitor.prototype.addObserver = function (reconnectObserver) {
21
+ this.reconnectObservers.push(reconnectObserver);
22
+ };
23
+ /**
24
+ * Given a reconnect event, start the appropriate behavior
25
+ */
26
+ ReconnectionMonitor.prototype.record = function (event) {
27
+ var _this = this;
28
+ if (event === ReconnectEvent.START_RECONNECT) {
29
+ // If the reconnection hasn't been started
30
+ if (this.reconnectSetTimeoutId === undefined &&
31
+ this.reconnectIntervalId === undefined) {
32
+ this.reconnectSetTimeoutId = setTimeout(function () {
33
+ // Reconnect now
34
+ _this._triggerReconnect();
35
+ // Retry reconnect every periodically until it works
36
+ _this.reconnectIntervalId = setInterval(function () {
37
+ _this._triggerReconnect();
38
+ }, RECONNECT_INTERVAL);
39
+ }, RECONNECT_DELAY);
40
+ }
41
+ }
42
+ if (event === ReconnectEvent.HALT_RECONNECT) {
43
+ if (this.reconnectIntervalId) {
44
+ clearInterval(this.reconnectIntervalId);
45
+ this.reconnectIntervalId = undefined;
46
+ }
47
+ if (this.reconnectSetTimeoutId) {
48
+ clearTimeout(this.reconnectSetTimeoutId);
49
+ this.reconnectSetTimeoutId = undefined;
50
+ }
51
+ }
52
+ };
53
+ /**
54
+ * Complete all reconnect observers
55
+ */
56
+ ReconnectionMonitor.prototype.close = function () {
57
+ this.reconnectObservers.forEach(function (reconnectObserver) {
58
+ var _a;
59
+ (_a = reconnectObserver.complete) === null || _a === void 0 ? void 0 : _a.call(reconnectObserver);
60
+ });
61
+ };
62
+ ReconnectionMonitor.prototype._triggerReconnect = function () {
63
+ this.reconnectObservers.forEach(function (reconnectObserver) {
64
+ var _a;
65
+ (_a = reconnectObserver.next) === null || _a === void 0 ? void 0 : _a.call(reconnectObserver);
66
+ });
67
+ };
68
+ return ReconnectionMonitor;
69
+ }());
70
+ export { ReconnectionMonitor };
71
+ //# sourceMappingURL=ReconnectionMonitor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReconnectionMonitor.js","sourceRoot":"","sources":["../../src/utils/ReconnectionMonitor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAE7E,MAAM,CAAN,IAAY,cAGX;AAHD,WAAY,cAAc;IACzB,qDAAmC,CAAA;IACnC,mDAAiC,CAAA;AAClC,CAAC,EAHW,cAAc,KAAd,cAAc,QAGzB;AAED;;;;;GAKG;AACH;IAAA;QACS,uBAAkB,GAAqB,EAAE,CAAC;IA0DnD,CAAC;IAtDA;;OAEG;IACH,yCAAW,GAAX,UAAY,iBAAiC;QAC5C,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;IACjD,CAAC;IAED;;OAEG;IACH,oCAAM,GAAN,UAAO,KAAqB;QAA5B,iBA4BC;QA3BA,IAAI,KAAK,KAAK,cAAc,CAAC,eAAe,EAAE;YAC7C,0CAA0C;YAC1C,IACC,IAAI,CAAC,qBAAqB,KAAK,SAAS;gBACxC,IAAI,CAAC,mBAAmB,KAAK,SAAS,EACrC;gBACD,IAAI,CAAC,qBAAqB,GAAG,UAAU,CAAC;oBACvC,gBAAgB;oBAChB,KAAI,CAAC,iBAAiB,EAAE,CAAC;oBACzB,oDAAoD;oBACpD,KAAI,CAAC,mBAAmB,GAAG,WAAW,CAAC;wBACtC,KAAI,CAAC,iBAAiB,EAAE,CAAC;oBAC1B,CAAC,EAAE,kBAAkB,CAAC,CAAC;gBACxB,CAAC,EAAE,eAAe,CAAC,CAAC;aACpB;SACD;QAED,IAAI,KAAK,KAAK,cAAc,CAAC,cAAc,EAAE;YAC5C,IAAI,IAAI,CAAC,mBAAmB,EAAE;gBAC7B,aAAa,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;gBACxC,IAAI,CAAC,mBAAmB,GAAG,SAAS,CAAC;aACrC;YACD,IAAI,IAAI,CAAC,qBAAqB,EAAE;gBAC/B,YAAY,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;gBACzC,IAAI,CAAC,qBAAqB,GAAG,SAAS,CAAC;aACvC;SACD;IACF,CAAC;IAED;;OAEG;IACH,mCAAK,GAAL;QACC,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,UAAA,iBAAiB;;YAChD,MAAA,iBAAiB,CAAC,QAAQ,+CAA1B,iBAAiB,EAAc;QAChC,CAAC,CAAC,CAAC;IACJ,CAAC;IAEO,+CAAiB,GAAzB;QACC,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,UAAA,iBAAiB;;YAChD,MAAA,iBAAiB,CAAC,IAAI,+CAAtB,iBAAiB,EAAU;QAC5B,CAAC,CAAC,CAAC;IACJ,CAAC;IACF,0BAAC;AAAD,CAAC,AA3DD,IA2DC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/pubsub",
3
- "version": "4.4.9",
3
+ "version": "4.4.10-next.13+5341c9f6f",
4
4
  "description": "Pubsub category of aws-amplify",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -45,9 +45,9 @@
45
45
  "cpx": "^1.5.0"
46
46
  },
47
47
  "dependencies": {
48
- "@aws-amplify/auth": "4.6.1",
49
- "@aws-amplify/cache": "4.0.50",
50
- "@aws-amplify/core": "4.6.1",
48
+ "@aws-amplify/auth": "4.6.2-next.13+5341c9f6f",
49
+ "@aws-amplify/cache": "4.0.51-next.13+5341c9f6f",
50
+ "@aws-amplify/core": "4.6.2-next.13+5341c9f6f",
51
51
  "graphql": "15.8.0",
52
52
  "paho-mqtt": "^1.1.0",
53
53
  "uuid": "^3.2.1",
@@ -103,5 +103,5 @@
103
103
  "lib-esm"
104
104
  ]
105
105
  },
106
- "gitHead": "7c46fbc75cff43c6323d29384f01cee7d3483e40"
106
+ "gitHead": "5341c9f6f1ca10fee8e34706a0e6d47afa338ba2"
107
107
  }
@@ -15,7 +15,7 @@ import { GraphQLError } from 'graphql';
15
15
  import * as url from 'url';
16
16
  import { v4 as uuid } from 'uuid';
17
17
  import { Buffer } from 'buffer';
18
- import { ProviderOptions } from '../../types';
18
+ import { ProviderOptions } from '../../types/Provider';
19
19
  import {
20
20
  Logger,
21
21
  Credentials,
@@ -26,11 +26,12 @@ import {
26
26
  jitteredExponentialRetry,
27
27
  NonRetryableError,
28
28
  ICredentials,
29
+ isNonRetryableError,
29
30
  } from '@aws-amplify/core';
30
31
  import Cache from '@aws-amplify/cache';
31
32
  import Auth, { GRAPHQL_AUTH_MODE } from '@aws-amplify/auth';
32
33
  import { AbstractPubSubProvider } from '../PubSubProvider';
33
- import { CONNECTION_STATE_CHANGE, CONTROL_MSG } from '../../index';
34
+ import { CONTROL_MSG, ConnectionState } from '../../types/PubSub';
34
35
 
35
36
  import {
36
37
  AMPLIFY_SYMBOL,
@@ -44,11 +45,16 @@ import {
44
45
  SOCKET_STATUS,
45
46
  START_ACK_TIMEOUT,
46
47
  SUBSCRIPTION_STATUS,
47
- } from './constants';
48
+ CONNECTION_STATE_CHANGE,
49
+ } from '../constants';
48
50
  import {
49
51
  ConnectionStateMonitor,
50
52
  CONNECTION_CHANGE,
51
53
  } from '../../utils/ConnectionStateMonitor';
54
+ import {
55
+ ReconnectEvent,
56
+ ReconnectionMonitor,
57
+ } from '../../utils/ReconnectionMonitor';
52
58
 
53
59
  const logger = new Logger('AWSAppSyncRealTimeProvider');
54
60
 
@@ -98,23 +104,63 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
98
104
  private keepAliveAlertTimeoutId?: ReturnType<typeof setTimeout>;
99
105
  private subscriptionObserverMap: Map<string, ObserverQuery> = new Map();
100
106
  private promiseArray: Array<{ res: Function; rej: Function }> = [];
107
+ private connectionState: ConnectionState;
101
108
  private readonly connectionStateMonitor = new ConnectionStateMonitor();
109
+ private readonly reconnectionMonitor = new ReconnectionMonitor();
110
+ private connectionStateMonitorSubscription: ZenObservable.Subscription;
102
111
 
103
112
  constructor(options: ProviderOptions = {}) {
104
113
  super(options);
105
114
  // Monitor the connection state and pass changes along to Hub
106
- this.connectionStateMonitor.connectionStateObservable.subscribe(
107
- ConnectionState => {
108
- dispatchApiEvent(
109
- CONNECTION_STATE_CHANGE,
110
- {
111
- provider: this,
112
- connectionState: ConnectionState,
113
- },
114
- `Connection state is ${ConnectionState}`
115
- );
116
- }
117
- );
115
+ this.connectionStateMonitorSubscription =
116
+ this.connectionStateMonitor.connectionStateObservable.subscribe(
117
+ connectionState => {
118
+ dispatchApiEvent(
119
+ CONNECTION_STATE_CHANGE,
120
+ {
121
+ provider: this,
122
+ connectionState,
123
+ },
124
+ `Connection state is ${connectionState}`
125
+ );
126
+ this.connectionState = connectionState;
127
+
128
+ // Trigger START_RECONNECT when the connection is disrupted
129
+ if (connectionState === ConnectionState.ConnectionDisrupted) {
130
+ this.reconnectionMonitor.record(ReconnectEvent.START_RECONNECT);
131
+ }
132
+
133
+ // Trigger HALT_RECONNECT to halt reconnection attempts when the state is anything other than
134
+ // ConnectionDisrupted or Connecting
135
+ if (
136
+ [
137
+ ConnectionState.Connected,
138
+ ConnectionState.ConnectedPendingDisconnect,
139
+ ConnectionState.ConnectedPendingKeepAlive,
140
+ ConnectionState.ConnectedPendingNetwork,
141
+ ConnectionState.ConnectedPendingNetwork,
142
+ ConnectionState.ConnectionDisruptedPendingNetwork,
143
+ ConnectionState.Disconnected,
144
+ ].includes(connectionState)
145
+ ) {
146
+ this.reconnectionMonitor.record(ReconnectEvent.HALT_RECONNECT);
147
+ }
148
+ }
149
+ );
150
+ }
151
+
152
+ /**
153
+ * Mark the socket closed and release all active listeners
154
+ */
155
+ close() {
156
+ // Mark the socket closed both in status and the connection monitor
157
+ this.socketStatus = SOCKET_STATUS.CLOSED;
158
+ this.connectionStateMonitor.record(CONNECTION_CHANGE.CONNECTION_FAILED);
159
+
160
+ // Turn off the subscription monitor Hub publishing
161
+ this.connectionStateMonitorSubscription.unsubscribe();
162
+ // Complete all reconnect observers
163
+ this.reconnectionMonitor.close();
118
164
  }
119
165
 
120
166
  getNewWebSocket(url, protocol) {
@@ -157,26 +203,44 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
157
203
  });
158
204
  observer.complete();
159
205
  } else {
206
+ let subscriptionStartActive = false;
160
207
  const subscriptionId = uuid();
161
- this._startSubscriptionWithAWSAppSyncRealTime({
162
- options,
163
- observer,
164
- subscriptionId,
165
- }).catch<any>(err => {
166
- observer.error({
167
- errors: [
168
- {
169
- ...new GraphQLError(
208
+ const startSubscription = () => {
209
+ if (!subscriptionStartActive) {
210
+ subscriptionStartActive = true;
211
+ const startSubscriptionPromise =
212
+ this._startSubscriptionWithAWSAppSyncRealTime({
213
+ options,
214
+ observer,
215
+ subscriptionId,
216
+ }).catch<any>(err => {
217
+ logger.debug(
170
218
  `${CONTROL_MSG.REALTIME_SUBSCRIPTION_INIT_ERROR}: ${err}`
171
- ),
172
- },
173
- ],
174
- });
175
- this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
176
- observer.complete();
219
+ );
220
+
221
+ this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
222
+ });
223
+ startSubscriptionPromise.finally(() => {
224
+ subscriptionStartActive = false;
225
+ });
226
+ }
227
+ };
228
+
229
+ let reconnectSubscription: ZenObservable.Subscription;
230
+
231
+ // Add an observable to the reconnection list to manage reconnection for this subscription
232
+ reconnectSubscription = new Observable(observer => {
233
+ this.reconnectionMonitor.addObserver(observer);
234
+ }).subscribe(() => {
235
+ startSubscription();
177
236
  });
178
237
 
238
+ startSubscription();
239
+
179
240
  return async () => {
241
+ // Cleanup reconnection subscription
242
+ reconnectSubscription?.unsubscribe();
243
+
180
244
  // Cleanup after unsubscribing or observer.complete was called after _startSubscriptionWithAWSAppSyncRealTime
181
245
  try {
182
246
  // Waiting that subscription has been connected before trying to unsubscribe
@@ -287,24 +351,7 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
287
351
  additionalHeaders,
288
352
  });
289
353
  } catch (err) {
290
- logger.debug({ err });
291
- const message = err['message'] ?? '';
292
- this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
293
- observer.error({
294
- errors: [
295
- {
296
- ...new GraphQLError(`${CONTROL_MSG.CONNECTION_FAILED}: ${message}`),
297
- },
298
- ],
299
- });
300
- observer.complete();
301
- const { subscriptionFailedCallback } =
302
- this.subscriptionObserverMap.get(subscriptionId) || {};
303
-
304
- // Notify concurrent unsubscription
305
- if (typeof subscriptionFailedCallback === 'function') {
306
- subscriptionFailedCallback();
307
- }
354
+ this._logStartSubscriptionError(subscriptionId, observer, err);
308
355
  return;
309
356
  }
310
357
 
@@ -332,6 +379,44 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
332
379
  }
333
380
  }
334
381
 
382
+ // Log logic for start subscription failures
383
+ private _logStartSubscriptionError(subscriptionId, observer, err) {
384
+ logger.debug({ err });
385
+ const message = err['message'] ?? '';
386
+ // Resolving to give the state observer time to propogate the update
387
+ Promise.resolve(
388
+ this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED)
389
+ );
390
+
391
+ // Capture the error only when the network didn't cause disruption
392
+ if (
393
+ this.connectionState !== ConnectionState.ConnectionDisruptedPendingNetwork
394
+ ) {
395
+ // When the error is non-retriable, error out the observable
396
+ if (isNonRetryableError(err)) {
397
+ observer.error({
398
+ errors: [
399
+ {
400
+ ...new GraphQLError(
401
+ `${CONTROL_MSG.CONNECTION_FAILED}: ${message}`
402
+ ),
403
+ },
404
+ ],
405
+ });
406
+ } else {
407
+ logger.debug(`${CONTROL_MSG.CONNECTION_FAILED}: ${message}`);
408
+ }
409
+
410
+ const { subscriptionFailedCallback } =
411
+ this.subscriptionObserverMap.get(subscriptionId) || {};
412
+
413
+ // Notify concurrent unsubscription
414
+ if (typeof subscriptionFailedCallback === 'function') {
415
+ subscriptionFailedCallback();
416
+ }
417
+ }
418
+ }
419
+
335
420
  // Waiting that subscription has been connected before trying to unsubscribe
336
421
  private async _waitForSubscriptionToBeConnected(subscriptionId: string) {
337
422
  const subscriptionObserver =
@@ -504,6 +589,10 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
504
589
  subscriptionState,
505
590
  });
506
591
 
592
+ logger.debug(
593
+ `${CONTROL_MSG.CONNECTION_FAILED}: ${JSON.stringify(payload)}`
594
+ );
595
+
507
596
  observer.error({
508
597
  errors: [
509
598
  {
@@ -513,9 +602,9 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
513
602
  },
514
603
  ],
515
604
  });
605
+
516
606
  if (startAckTimeoutId) clearTimeout(startAckTimeoutId);
517
607
 
518
- observer.complete();
519
608
  if (typeof subscriptionFailedCallback === 'function') {
520
609
  subscriptionFailedCallback();
521
610
  }
@@ -525,14 +614,7 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
525
614
 
526
615
  private _errorDisconnect(msg: string) {
527
616
  logger.debug(`Disconnect error: ${msg}`);
528
- this.subscriptionObserverMap.forEach(({ observer }) => {
529
- if (observer && !observer.closed) {
530
- observer.error({
531
- errors: [{ ...new GraphQLError(msg) }],
532
- });
533
- }
534
- });
535
- this.subscriptionObserverMap.clear();
617
+
536
618
  if (this.awsRealTimeSocket) {
537
619
  this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
538
620
  this.awsRealTimeSocket.close();
@@ -556,22 +638,7 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
556
638
  subscriptionState: SUBSCRIPTION_STATUS.FAILED,
557
639
  });
558
640
 
559
- if (observer && !observer.closed) {
560
- observer.error({
561
- errors: [
562
- {
563
- ...new GraphQLError(
564
- `Subscription timeout ${JSON.stringify({
565
- query,
566
- variables,
567
- })}`
568
- ),
569
- },
570
- ],
571
- });
572
- // Cleanup will be automatically executed
573
- observer.complete();
574
- }
641
+ this.connectionStateMonitor.record(CONNECTION_CHANGE.CLOSED);
575
642
  logger.debug(
576
643
  'timeoutStartSubscription',
577
644
  JSON.stringify({ query, variables })
@@ -640,6 +707,7 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
640
707
  this.socketStatus = SOCKET_STATUS.READY;
641
708
  this.promiseArray = [];
642
709
  } catch (err) {
710
+ logger.debug('Connection exited with', err);
643
711
  this.promiseArray.forEach(({ rej }) => rej(err));
644
712
  this.promiseArray = [];
645
713
  if (
@@ -676,9 +744,6 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
676
744
  logger.debug(`WebSocket connection error`);
677
745
  };
678
746
  newSocket.onclose = () => {
679
- this.connectionStateMonitor.record(
680
- CONNECTION_CHANGE.CONNECTION_FAILED
681
- );
682
747
  rej(new Error('Connection handshake error'));
683
748
  };
684
749
  newSocket.onopen = () => {
@@ -687,7 +752,6 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
687
752
  };
688
753
  });
689
754
  })();
690
-
691
755
  // Step 2: wait for ack from AWS AppSyncReaTime after sending init
692
756
  await (() => {
693
757
  return new Promise((res, rej) => {
@@ -747,20 +811,20 @@ export class AWSAppSyncRealTimeProvider extends AbstractPubSubProvider {
747
811
  };
748
812
  this.awsRealTimeSocket.send(JSON.stringify(gqlInit));
749
813
 
750
- setTimeout(checkAckOk.bind(this, ackOk), CONNECTION_INIT_TIMEOUT);
751
- }
814
+ const checkAckOk = (ackOk: boolean) => {
815
+ if (!ackOk) {
816
+ this.connectionStateMonitor.record(
817
+ CONNECTION_CHANGE.CONNECTION_FAILED
818
+ );
819
+ rej(
820
+ new Error(
821
+ `Connection timeout: ack from AWSAppSyncRealTime was not received after ${CONNECTION_INIT_TIMEOUT} ms`
822
+ )
823
+ );
824
+ }
825
+ };
752
826
 
753
- function checkAckOk(ackOk: boolean) {
754
- if (!ackOk) {
755
- this.connectionStateMonitor.record(
756
- CONNECTION_CHANGE.CONNECTION_FAILED
757
- );
758
- rej(
759
- new Error(
760
- `Connection timeout: ack from AWSRealTime was not received on ${CONNECTION_INIT_TIMEOUT} ms`
761
- )
762
- );
763
- }
827
+ setTimeout(() => checkAckOk(ackOk), CONNECTION_INIT_TIMEOUT);
764
828
  }
765
829
  });
766
830
  })();