{"version":3,"file":"KeyValueStorage.mjs","sources":["../../../src/storage/KeyValueStorage.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { ConsoleLogger } from '../Logger';\nimport { PlatformNotSupportedError } from '../errors';\nimport { isBrowser } from '../utils';\nconst logger = new ConsoleLogger('KeyValueStorage');\n/**\n * @internal\n */\nexport class KeyValueStorage {\n    constructor(storage) {\n        this.storage = storage;\n    }\n    /**\n     * This is used to set a specific item in storage\n     * @param {string} key - the key for the item\n     * @param {object} value - the value\n     * @returns {string} value that was set\n     */\n    async setItem(key, value) {\n        if (!this.storage)\n            throw new PlatformNotSupportedError();\n        this.storage.setItem(key, value);\n    }\n    /**\n     * This is used to get a specific key from storage\n     * @param {string} key - the key for the item\n     * This is used to clear the storage\n     * @returns {string} the data item\n     */\n    async getItem(key) {\n        if (!this.storage)\n            throw new PlatformNotSupportedError();\n        return this.storage.getItem(key);\n    }\n    /**\n     * This is used to remove an item from storage\n     * @param {string} key - the key being set\n     * @returns {string} value - value that was deleted\n     */\n    async removeItem(key) {\n        if (!this.storage)\n            throw new PlatformNotSupportedError();\n        this.storage.removeItem(key);\n    }\n    /**\n     * This is used to clear the storage\n     * @returns {string} nothing\n     */\n    async clear() {\n        if (!this.storage)\n            throw new PlatformNotSupportedError();\n        this.storage.clear();\n    }\n    /**\n     * This is used to allow listening for changes\n     * @param {function} listener - the function called on storage change\n     * @returns {function} an unsubscribe function that removes the listener. The\n     * underlying `window` 'storage' event listener is attached lazily on the\n     * first subscription and detached once the last listener unsubscribes. In\n     * non-browser (SSR/native) environments nothing is attached and the returned\n     * unsubscribe function is a safe no-op.\n     */\n    addListener(listener) {\n        const listeners = (this.listeners ?? (this.listeners = new Set()));\n        listeners.add(listener);\n        // Lazily attach the cross-tab 'storage' listener on the first subscription.\n        if (isBrowser() && listeners.size === 1) {\n            // Create the handler lazily (and only in a browser) so instances that\n            // never subscribe keep no function-valued own property. The same\n            // reference is reused for detach on last-unsubscribe.\n            const storageListener = (e) => {\n                // Only react to events for this instance's backing store. Without\n                // this guard a localStorage-backed instance and a\n                // sessionStorage-backed instance would react to each other's\n                // 'storage' events, since both share the single window listener.\n                if (e.storageArea !== this.storage) {\n                    return;\n                }\n                this.listeners?.forEach(l => {\n                    // A listener may be async and reject; isolate each call so one\n                    // rejecting listener neither aborts the others nor surfaces as an\n                    // unhandled promise rejection.\n                    Promise.resolve(l({\n                        key: e.key,\n                        oldValue: e.oldValue,\n                        newValue: e.newValue,\n                    })).catch(error => {\n                        logger.error('Error in storage event listener', error);\n                    });\n                });\n            };\n            this.storageListener = storageListener;\n            window.addEventListener('storage', storageListener, false);\n        }\n        return () => {\n            listeners.delete(listener);\n            const { storageListener } = this;\n            // Detach once the last subscriber unsubscribes — real teardown, not\n            // just pruning the set. Guarding on the stored handler keeps repeated\n            // unsubscribe calls idempotent.\n            if (isBrowser() && listeners.size === 0 && storageListener) {\n                window.removeEventListener('storage', storageListener, false);\n                this.storageListener = undefined;\n            }\n        };\n    }\n}\n"],"names":[],"mappings":";;;;;;;AAAA;AACA;AAIA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,iBAAiB,CAAC;AACnD;AACA;AACA;AACO,MAAM,eAAe,CAAC;AAC7B,IAAI,WAAW,CAAC,OAAO,EAAE;AACzB,QAAQ,IAAI,CAAC,OAAO,GAAG,OAAO;AAC9B,IAAI;AACJ;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,MAAM,OAAO,CAAC,GAAG,EAAE,KAAK,EAAE;AAC9B,QAAQ,IAAI,CAAC,IAAI,CAAC,OAAO;AACzB,YAAY,MAAM,IAAI,yBAAyB,EAAE;AACjD,QAAQ,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC;AACxC,IAAI;AACJ;AACA;AACA;AACA;AACA;AACA;AACA,IAAI,MAAM,OAAO,CAAC,GAAG,EAAE;AACvB,QAAQ,IAAI,CAAC,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