UNPKG

@studyportals/sp-hs-misc

Version:

Miscellaneous code used in HouseStark's projects

76 lines 2.68 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EagerInMemoryCache = void 0; class EagerInMemoryCache { get cache() { return this._cache; } get promiseToBuildTheCache() { return this._promiseToBuildTheCache; } constructor() { this._cache = null; this._promiseToBuildTheCache = null; } get(key) { return __awaiter(this, void 0, void 0, function* () { const cache = yield this.obtainCache(); if (cache.has(key)) { return cache.get(key); } throw new Error(`A cache entry does not exist for the specified key: ${key}`); }); } obtainCache() { return __awaiter(this, void 0, void 0, function* () { 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; } buildCache() { return __awaiter(this, void 0, void 0, function* () { const data = yield 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"); } } exports.EagerInMemoryCache = EagerInMemoryCache; //# sourceMappingURL=eager-in-memory-cache.class.js.map