UNPKG

@studyportals/sp-hs-misc

Version:

Miscellaneous code used in HouseStark's projects

58 lines 1.61 kB
class EagerInMemoryCache { get cache() { return this._cache; } get promiseToBuildTheCache() { return this._promiseToBuildTheCache; } constructor() { this._cache = null; this._promiseToBuildTheCache = null; } async get(key) { const cache = await this.obtainCache(); if (cache.has(key)) { return cache.get(key); } throw new Error(`A cache entry does not exist for the specified key: ${key}`); } async obtainCache() { if (this.isCacheBuilt()) { return this.cache; } return this.acquireCache(); } isCacheBuilt() { return null != this.cache; } acquireCache() { if (this.isCacheBeingBuilt()) { return this.promiseToBuildTheCache; } return this._promiseToBuildTheCache = this.buildCache(); } isCacheBeingBuilt() { return null != this.promiseToBuildTheCache; } async buildCache() { const data = await this.retrieveData(); const cache = this.createCacheFromData(data); return this._cache = cache; } retrieveData() { throw new Error("Not implemented"); } createCacheFromData(data) { const cache = new Map(); for (let dataItem of data) { const key = this.extractKey(dataItem); cache.set(key, dataItem); } return cache; } extractKey(dataItem) { throw new Error("Not implemented"); } } export { EagerInMemoryCache }; //# sourceMappingURL=eager-in-memory-cache.class.js.map