UNPKG

@stnekroman/tstools

Version:

Set of handy tools for TypeScript development

34 lines (33 loc) 962 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoadingCache = void 0; const MemoryDictionary_1 = require("./MemoryDictionary"); class LoadingCache { constructor(mapImpl = new MemoryDictionary_1.MemoryDictionary()) { this.map = mapImpl; } uniqueKey(...args) { return args[0]; } get(...args) { const cacheKey = this.uniqueKey(...args); if (this.map.has(cacheKey)) { return this.map.get(cacheKey); } const promise = this.load(...args); this.map.set(cacheKey, promise); return promise; } has(...args) { const cacheKey = this.uniqueKey(...args); return this.map.has(cacheKey); } put(...[data, ...args]) { const cacheKey = this.uniqueKey(...args); this.map.set(cacheKey, Promise.resolve(data)); } clear() { this.map.clear(); } } exports.LoadingCache = LoadingCache;