UNPKG

@aws-amplify/core

Version:
1 lines 3.37 kB
{"version":3,"file":"SessionListener.native.mjs","sources":["../../../../src/utils/sessionListener/SessionListener.native.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { loadAppState } from '@aws-amplify/react-native';\nconst stateChangeListeners = new Set();\nconst isActive = (appState) => appState === 'active';\nconst isInactive = (appState) => appState === 'inactive' || appState === 'background';\nexport class SessionListener {\n constructor() {\n this.handleStateChange = this.handleStateChange.bind(this);\n // Setup state listeners\n loadAppState().addEventListener('change', this.handleStateChange);\n }\n addStateChangeListener(listener, notifyOnAdd = false) {\n stateChangeListeners.add(listener);\n // Notify new handlers of the current state on add if the current state has been determined\n if (notifyOnAdd && this.currentAppState !== undefined) {\n listener(this.getSessionState());\n }\n }\n removeStateChangeListener(handler) {\n stateChangeListeners.delete(handler);\n }\n handleStateChange(nextAppState) {\n if ((this.currentAppState === undefined ||\n isInactive(this.currentAppState)) &&\n isActive(nextAppState)) {\n this.notifyHandlers('started');\n }\n else if (isActive(this.currentAppState) && isInactive(nextAppState)) {\n this.notifyHandlers('ended');\n }\n this.currentAppState = nextAppState;\n }\n notifyHandlers(state) {\n stateChangeListeners.forEach(listener => {\n listener(state);\n });\n }\n getSessionState() {\n if (isActive(this.currentAppState)) {\n return 'started';\n }\n // Consider any other app state as ended\n return 'ended';\n }\n}\n"],"names":[],"mappings":";;AAAA;AACA;AAEA,MAAM,oBAAoB,GAAG,IAAI,GAAG,EAAE,CAAC;AACvC,MAAM,QAAQ,GAAG,CAAC,QAAQ,KAAK,QAAQ,KAAK,QAAQ,CAAC;AACrD,MAAM,UAAU,GAAG,CAAC,QAAQ,KAAK,QAAQ,KAAK,UAAU,IAAI,QAAQ,KAAK,YAAY,CAAC;AAC/E,MAAM,eAAe,CAAC;AAC7B,IAAI,WAAW,GAAG;AAClB,QAAQ,IAAI,CAAC,iBAAiB,GAAG,IAAI,CAAC,iBAAi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