@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
1 lines • 3.64 kB
Source Map (JSON)
{"version":3,"sources":["../../../packages/core/base/data-store/browser-storage-data-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,UAAU,EAAM,MAAM,MAAM,CAAC;AAGjD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD;;;GAGG;AACH,8BAAsB,uBAAuB,CAAC,KAAK,CAAE,SAAQ,eAAe,CAAC,KAAK,EAAE,MAAM,CAAC;IAK3E,SAAS,CAAC,UAAU,EAAE,MAAM;IAAE,SAAS,CAAC,OAAO,EAAE,OAAO;IAHpE;;OAEG;gBACmB,UAAU,EAAE,MAAM,EAAY,OAAO,EAAE,OAAO;IAKpE;;OAEG;IACH,SAAS,CAAC,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC;IAIvC;;OAEG;IACH,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,CAAC,IAAI,CAAC;IAKjD;;OAEG;IACH,SAAS,CAAC,SAAS,IAAI,UAAU,CAAC,IAAI,CAAC;IAKvC;;OAEG;IACH,SAAS,CAAC,gBAAgB,CAAC,IAAI,EAAE,KAAK,GAAG,MAAM;IAI/C;;OAEG;IACH,SAAS,CAAC,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,KAAK;IAUvD;;OAEG;IACH,OAAO,CAAC,uBAAuB;CAYlC","file":"browser-storage-data-store.d.ts","sourcesContent":["import { fromEvent, Observable, of } from 'rxjs';\r\nimport { filter } from 'rxjs/operators';\r\nimport { LogLevel } from '../../diagnostics/log-level';\r\nimport { CachedDataStore } from './cached-data-store';\r\n\r\n/**\r\n * Defines a single value data store using browser storage as the underlying storage mechanism\r\n * This is used as a base class for LocalDataStore and SessionDataStore which use LocalStorage and SessionStorage respectively\r\n */\r\nexport abstract class BrowserStorageDataStore<TData> extends CachedDataStore<TData, string> {\r\n\r\n /**\r\n * Initializes a new instance of DataStore<T>\r\n */\r\n constructor(protected storageKey: string, protected storage: Storage) {\r\n super();\r\n this.listenForStorageChanges();\r\n }\r\n\r\n /**\r\n * Implementation to get the stored data\r\n */\r\n protected getData(): Observable<string> {\r\n return this.storage ? of(this.storage.getItem(this.storageKey)) : of('');\r\n }\r\n\r\n /**\r\n * Implementation to set the stored data\r\n */\r\n protected setData(data: string): Observable<void> {\r\n this.storage?.setItem(this.storageKey, data);\r\n return of(null);\r\n }\r\n\r\n /**\r\n * Implementation to clear the stored data\r\n */\r\n protected clearData(): Observable<void> {\r\n this.storage?.removeItem(this.storageKey);\r\n return of(null);\r\n }\r\n\r\n /**\r\n * Transforms data in preparation for storage. Default behavior is no operation\r\n */\r\n protected transformToStore(data: TData): string {\r\n return JSON.stringify(data);\r\n }\r\n\r\n /**\r\n * Transforms data in preparation for usage. Default behavior is no operation\r\n */\r\n protected transformFromStore(storedData: string): TData {\r\n let data: TData = null;\r\n try {\r\n data = JSON.parse(storedData);\r\n } catch (error) {\r\n this.log(`Failed to parse data retrieved with storage key: ${this.storageKey}`, LogLevel.Debug);\r\n }\r\n return data;\r\n }\r\n\r\n /**\r\n * Listens for when other windows have modified our storage key and emits when the stored data has changed.\r\n */\r\n private listenForStorageChanges(): void {\r\n this.rxjsLifetime.addSubscriptions(\r\n fromEvent(window, 'storage').pipe(\r\n filter((event: StorageEvent) => {\r\n return event.storageArea === this.storage && event.key === this.storageKey;\r\n })\r\n ).subscribe((event: StorageEvent) => {\r\n const data = this.transformFromStore(event.newValue);\r\n this.dataChangedSubject.next(data);\r\n })\r\n );\r\n }\r\n}\r\n"]}