UNPKG

3.03 kBJavaScriptView Raw
1import { RECONNECT_DELAY, RECONNECT_INTERVAL } from '../Providers/constants';
2export 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 */
13var 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}());
70export { ReconnectionMonitor };
71//# sourceMappingURL=ReconnectionMonitor.js.map
\No newline at end of file