UNPKG

@aws-amplify/pubsub

Version:

Pubsub category of aws-amplify

1 lines 9.45 kB
{"version":3,"file":"ConnectionStateMonitor.mjs","sources":["../../../src/utils/ConnectionStateMonitor.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { Observable, filter, map } from 'rxjs';\nimport { ConnectionState } from '../types/PubSub';\nimport { ReachabilityMonitor } from './ReachabilityMonitor';\nexport const CONNECTION_CHANGE = {\n KEEP_ALIVE_MISSED: { keepAliveState: 'unhealthy' },\n KEEP_ALIVE: { keepAliveState: 'healthy' },\n CONNECTION_ESTABLISHED: { connectionState: 'connected' },\n CONNECTION_FAILED: {\n intendedConnectionState: 'disconnected',\n connectionState: 'disconnected',\n },\n CLOSING_CONNECTION: { intendedConnectionState: 'disconnected' },\n OPENING_CONNECTION: {\n intendedConnectionState: 'connected',\n connectionState: 'connecting',\n },\n CLOSED: { connectionState: 'disconnected' },\n ONLINE: { networkState: 'connected' },\n OFFLINE: { networkState: 'disconnected' },\n};\nexport class ConnectionStateMonitor {\n constructor() {\n this._networkMonitoringSubscription = undefined;\n this._linkedConnectionState = {\n networkState: 'connected',\n connectionState: 'disconnected',\n intendedConnectionState: 'disconnected',\n keepAliveState: 'healthy',\n };\n // Attempt to update the state with the current actual network state\n this._initialNetworkStateSubscription = ReachabilityMonitor().subscribe(({ online }) => {\n this.record(online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE);\n this._initialNetworkStateSubscription?.unsubscribe();\n });\n this._linkedConnectionStateObservable =\n new Observable(connectionStateObserver => {\n connectionStateObserver.next(this._linkedConnectionState);\n this._linkedConnectionStateObserver = connectionStateObserver;\n });\n }\n /**\n * Turn network state monitoring on if it isn't on already\n */\n enableNetworkMonitoring() {\n // If no initial network state was discovered, stop trying\n this._initialNetworkStateSubscription?.unsubscribe();\n // Maintain the network state based on the reachability monitor\n if (this._networkMonitoringSubscription === undefined) {\n this._networkMonitoringSubscription = ReachabilityMonitor().subscribe(({ online }) => {\n this.record(online ? CONNECTION_CHANGE.ONLINE : CONNECTION_CHANGE.OFFLINE);\n });\n }\n }\n /**\n * Turn network state monitoring off if it isn't off already\n */\n disableNetworkMonitoring() {\n this._networkMonitoringSubscription?.unsubscribe();\n this._networkMonitoringSubscription = undefined;\n }\n /**\n * Get the observable that allows us to monitor the connection state\n *\n * @returns {Observable<ConnectionState>} - The observable that emits ConnectionState updates\n */\n get connectionStateObservable() {\n let previous;\n // The linked state aggregates state changes to any of the network, connection,\n // intendedConnection and keepAliveHealth. Some states will change these independent\n // states without changing the overall connection state.\n // After translating from linked states to ConnectionState, then remove any duplicates\n return this._linkedConnectionStateObservable\n .pipe(map(value => {\n return this.connectionStatesTranslator(value);\n }))\n .pipe(filter(current => {\n const toInclude = current !== previous;\n previous = current;\n return toInclude;\n }));\n }\n /*\n * Updates local connection state and emits the full state to the observer.\n */\n record(statusUpdates) {\n // Maintain the network monitor\n if (statusUpdates.intendedConnectionState === 'connected') {\n this.enableNetworkMonitoring();\n }\n else if (statusUpdates.intendedConnectionState === 'disconnected') {\n this.disableNetworkMonitoring();\n }\n // Maintain the socket state\n const newSocketStatus = {\n ...this._linkedConnectionState,\n ...statusUpdates,\n };\n this._linkedConnectionState = { ...newSocketStatus };\n this._linkedConnectionStateObserver?.next(this._linkedConnectionState);\n }\n /*\n * Translate the ConnectionState structure into a specific ConnectionState string literal union\n */\n connectionStatesTranslator({ connectionState, networkState, intendedConnectionState, keepAliveState, }) {\n if (connectionState === 'connected' && networkState === 'disconnected')\n return ConnectionState.ConnectedPendingNetwork;\n if (connectionState === 'connected' &&\n intendedConnectionState === 'disconnected')\n return ConnectionState.ConnectedPendingDisconnect;\n if (connectionState === 'disconnected' &&\n intendedConnectionState === 'connected' &&\n networkState === 'disconnected')\n return ConnectionState.ConnectionDisruptedPendingNetwork;\n if (connectionState === 'disconnected' &&\n intendedConnectionState === 'connected')\n return ConnectionState.ConnectionDisrupted;\n if (connectionState === 'connected' && keepAliveState === 'unhealthy')\n return ConnectionState.ConnectedPendingKeepAlive;\n // All remaining states directly correspond to the connection state\n if (connectionState === 'connecting')\n return ConnectionState.Connecting;\n if (connectionState === 'disconnected')\n return ConnectionState.Disconnected;\n return ConnectionState.Connected;\n }\n}\n"],"names":[],"mappings":";;;;AAAA;AACA;AAIY,MAAC,iBAAiB,GAAG;AACjC,IAAI,iBAAiB,EAAE,EAAE,cAAc,EAAE,WAAW,EAAE;AACtD,IAAI,UAAU,EAAE,EAAE,cAAc,EAAE,SAAS,EAAE;AAC7C,IAAI,sBAAsB,EAAE,EAAE,eAAe,EAAE,WAAW,EAAE;AAC5D,IAAI,iBAAiB,EAAE;AACvB,QAAQ,uBAAuB,EAAE,cAAc;AAC/C,QAAQ,eAAe,EAAE,cAAc;AACvC,KAAK;AACL,IAAI,kBAAkB,EAAE,EAAE,uBAAuB,EAAE,cAAc,EAAE;AACnE,IAAI,kBAAkB,EAAE;AACxB,QAAQ,uBAAuB,EAAE,WAAW;AAC5C,QAAQ,eAAe,EAAE,YAAY;AACrC,KAAK;AACL,IAAI,MAAM,EAAE,EAAE,eAAe,EAAE,cAAc,EAAE;AAC/C,IAAI,MAAM,EAAE,EAAE,YAAY,EAAE,WAAW,EAAE;AACzC,IAAI,OAAO,EAAE,EAAE,YAAY,EAAE,cAAc,EAAE;AAC7C,EAAE;AACK,MAAM,sBAAsB,CAAC;AACpC,IAAI,WAAW,GAAG;AAClB,QAAQ,IAAI,CAAC,8BAA8B,GAAG,SAAS,CAAC;AACxD,QAAQ,IAAI,CAAC,sBAAsB,GAAG;AACtC,YAAY,YAAY,EAAE,WAAW;AACrC,YAAY,eAAe,EAAE,cAAc;AAC3C,YAAY,uBAAuB,EAAE,cAAc;AACnD,YAAY,cAAc,EAAE,SAAS;AACrC,SAAS,CAAC;AACV;AACA,QAAQ,IAAI,CAAC,gCAAgC,GAAG,mBAAmB,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,MAAM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