UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

1 lines 2.04 kB
{"version":3,"sources":["../../../packages/core/base/data-store/cached-data-store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAM,MAAM,MAAM,CAAC;AAEtC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC;;GAEG;AACH,8BAAsB,eAAe,CAAC,KAAK,EAAE,WAAW,CAAE,SAAQ,SAAS,CAAC,KAAK,EAAE,WAAW,CAAC;IAE3F;;OAEG;IACH,IAAW,WAAW,IAAI,KAAK,CAE9B;IACD,OAAO,CAAC,mBAAmB,CAAQ;IACnC,OAAO,CAAC,MAAM,CAAS;IAEvB;;OAEG;;IASH;;OAEG;IACI,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC;IAU/B;;OAEG;IACI,UAAU,IAAI,IAAI;CAI5B","file":"cached-data-store.d.ts","sourcesContent":["import { Observable, of } from 'rxjs';\r\nimport { tap } from 'rxjs/operators';\r\nimport { DataStore } from './data-store';\r\n\r\n/**\r\n * Defines a base class extended from DataStore but with additional caching cabailities\r\n */\r\nexport abstract class CachedDataStore<TData, TStoredData> extends DataStore<TData, TStoredData> {\r\n\r\n /**\r\n * Backing subject for dataCleared property\r\n */\r\n public get cachedValue(): TData {\r\n return this.internalCachedValue;\r\n }\r\n private internalCachedValue: TData;\r\n private cached = false;\r\n\r\n /**\r\n * Initializes a new instance of CachedDataStore<TData, TStoredData>\r\n */\r\n constructor() {\r\n super();\r\n this.rxjsLifetime.addSubscriptions(\r\n this.dataChanged.subscribe(data => this.internalCachedValue = data),\r\n this.dataCleared.subscribe(() => this.clearCache())\r\n );\r\n }\r\n\r\n /**\r\n * Gets the data value from the data store\r\n */\r\n public get(): Observable<TData> {\r\n if (this.cached) {\r\n return of(this.cachedValue);\r\n }\r\n return super.get().pipe(tap(data => {\r\n this.internalCachedValue = data;\r\n this.cached = true;\r\n }));\r\n }\r\n\r\n /**\r\n * Clears the cache from this data store\r\n */\r\n public clearCache(): void {\r\n this.cached = false;\r\n this.internalCachedValue = null;\r\n }\r\n}\r\n"]}