UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

197 lines (195 loc) 6.38 kB
/* eslint-disable unused-imports/no-unused-vars */ import { of } from 'rxjs'; import { map } from 'rxjs/operators'; /** * Abstract class of SharedCache by using session storage. * * TClass: the caching data class type. * TData: the caching data interface type. * TParams: the data query parameters type. */ // eslint-disable-next-line @typescript-eslint/no-unused-vars export class SharedCache { uniqueId; uniqueVersion; instanceId; serialize; deserialize; getDataQuery; options; cache = {}; /** * Initializes a new instance of the SharedCache class. * * @param uniqueId the unique identity of the collection of data. * @param uniqueVersion the unique version of data. (Should increment any data format or query change) * @param instanceId the instance id generator. * @param serialize the data serialization. * @param deserialize the data deserialization. * @param getDataQuery the query observable. * @param options the shared cache options. */ constructor(uniqueId, uniqueVersion, instanceId, serialize, deserialize, getDataQuery, options) { this.uniqueId = uniqueId; this.uniqueVersion = uniqueVersion; this.instanceId = instanceId; this.serialize = serialize; this.deserialize = deserialize; this.getDataQuery = getDataQuery; this.options = options; } /** * Gets the id of the collection of data. */ get id() { return this.uniqueId; } /** * Gets the version of the cache for format and query. */ get version() { return this.uniqueVersion; } /** * Check if the Cache has unexpired data available for the instance id. * @param params the parameter to check for the data. */ isAvailable(params) { const instanceId = this.instanceId(params); if (this.cache[instanceId] && !this.expired(this.cache[instanceId])) { return true; } if (MsftSme.SessionStorageHandler.getSessionStorage()?.[this.cacheKey]) { this.sessionRestore(); if (this.cache[instanceId] && !this.expired(this.cache[instanceId])) { return true; } } return false; } /** * Query to find the data. * @param params the parameter to query the data. */ query(params) { if (this.isAvailable(params)) { const instanceId = this.instanceId(params); return of(this.cache[instanceId]); } return this.update(params); } /** * Refresh the data. * @param params the parameter to query the data. */ refresh(params) { if (MsftSme.SessionStorageHandler.getSessionStorage()?.[this.cacheKey]) { this.sessionRestore(); } return this.update(params); } /** * Save an instance data to session storage. * * @param params the parameter to query the data. * @param data the data object to store. */ save(params, data) { if (MsftSme.SessionStorageHandler.getSessionStorage()?.[this.cacheKey]) { this.sessionRestore(); } this.sessionSave(params, data); } /** * Clear the cache. * * @param params the parameter to query the data (optional to delete an instance) */ clear(params) { if (params) { if (MsftSme.SessionStorageHandler.getSessionStorage()?.[this.cacheKey]) { this.sessionRestore(); } this.sessionSave(params); } else { this.cache = {}; if (MsftSme.SessionStorageHandler.getSessionStorage()) { MsftSme.SessionStorageHandler.getSessionStorage()[this.cacheKey] = JSON.stringify(this.cache); } } } /** * Force restore whole data and return the data for the params specified. * * @param params the parameter to query the data. * @return SharedCacheContainer<TClass> the data for the params specified. */ forceRestore(params) { if (MsftSme.SessionStorageHandler.getSessionStorage()?.[this.cacheKey]) { const instanceId = this.instanceId(params); this.sessionRestore(); return this.cache[instanceId]; } return null; } expired(data) { if (this.options && this.options.forceRefresh) { return true; } if (!this.options || !this.options.expiration) { return false; } return (Date.now() - data.timestamp) > this.options.expiration; } update(params) { if (this.getDataQuery) { const instanceId = this.instanceId(params); return this.getDataQuery(params) .pipe(map(data => { this.sessionSave(params, data); return this.cache[instanceId]; })); } else { return of(null); } } sessionRestore() { const preProcessed = JSON.parse(sessionStorage[this.cacheKey]); const cache = {}; const keys = Object.keys(preProcessed); keys.forEach(key => { cache[key] = { timestamp: preProcessed[key].timestamp, instance: this.deserialize(preProcessed[key].instance) }; }); this.cache = cache; } sessionSave(params, data) { const instanceId = this.instanceId(params); if (data) { this.cache[instanceId] = { timestamp: Date.now(), instance: data }; } else { delete this.cache[instanceId]; } const preProcessed = {}; const keys = Object.keys(this.cache); keys.forEach(key => { preProcessed[key] = { timestamp: this.cache[key].timestamp, instance: this.serialize(this.cache[key].instance) }; }); if (MsftSme.SessionStorageHandler.getSessionStorage()) { MsftSme.SessionStorageHandler.getSessionStorage()[this.cacheKey] = JSON.stringify(preProcessed); } } get cacheKey() { // used as sessionStorage key name. return 'shared-cache:{0}.{1}'.format(this.id, this.version); } } //# sourceMappingURL=shared-cache.js.map