UNPKG

typescript-cacheable

Version:

An in-memory caching (memoization) decorator for Typescript

58 lines 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalStorageCacheRegistry = void 0; const CacheableOptions_1 = require("../CacheableOptions"); const CacheRegistry_1 = require("./CacheRegistry"); class LocalStorageCacheRegistry extends CacheRegistry_1.CacheRegistry { constructor() { super(...arguments); this.getStoreFunction = undefined; } getOrInitCache(target, methodName) { const cache = super.getOrInitCache(target, methodName); const targetClass = target.constructor.name; if (!cache.has(targetClass)) { cache.set(targetClass, new Map()); } return cache.get(targetClass); } getCacheHost(target, methodName) { if (this.getStoreFunction === undefined) { throw `getStore function is undefined for ${this.getTarget(target, methodName)}`; } const host = this.getStoreFunction(); if (host === undefined) { throw `getStore function returned undefined for ${this.getTarget(target, methodName)}`; } return host; } newCache() { return new Map(); } processOptions(target, methodName, options) { if (this.getStoreFunction !== undefined) { return; } if (options === undefined || options.getStore === undefined) { throw `options.getStore is required for ${this.getTarget(target, methodName)}. options: ${JSON.stringify(options)}`; } this.getStoreFunction = options.getStore; } initCache(cacheHost) { if (cacheHost === undefined) { super.initCache(this.getCacheHost(undefined)); } else { super.initCache(cacheHost); } } initialiseStore(storeFunction) { this.processOptions(undefined, '?', (0, CacheableOptions_1.optionsWithDefaults)({ scope: 'LOCAL_STORAGE', getStore: storeFunction })); this.initCache(); } getTarget(target, methodName) { return (target === undefined ? '?' : target.constructor.name) + '::' + (methodName !== null && methodName !== void 0 ? methodName : '?'); } } exports.LocalStorageCacheRegistry = LocalStorageCacheRegistry; //# sourceMappingURL=LocalCacheRegistry.js.map