UNPKG

angular-caching

Version:

Lightweight library for data caching

58 lines (54 loc) 2.13 kB
import { Observable } from 'rxjs'; import { publishReplay, refCount, take } from 'rxjs/operators'; function InMemoryCache(params = {}) { let originalFunc; const cacheSize = params.cacheSize || 1; const chacheTTL = params.ttl; const cacheMap = new Map(); const timersMap = new Map(); return function (target, propertyKey, descriptor) { originalFunc = descriptor.value; descriptor.value = function (...args) { if (!args.length && cacheSize > 1) { throw new Error(`If you don't provide arguments, cache size could not be bigger then 1`); } const id = JSON.stringify(args); const currentCacheSize = cacheMap.size; let data = cacheMap.get(id); if (!data) { data = originalFunc.apply(this, args); if (data instanceof Observable) { data = data.pipe(publishReplay(1), refCount(), take(1)); } if (currentCacheSize >= cacheSize) { if (timersMap.has(cacheMap.keys()[0])) { clearTimeout(timersMap.get(cacheMap.keys()[0])); timersMap.delete(cacheMap.keys()[0]); } cacheMap.delete(cacheMap.keys()[0]); } if (params.sync) { cacheMap.set(id, data); } else { Promise.resolve().then(() => { cacheMap.set(id, data); }); } if (chacheTTL || chacheTTL === 0) { const timerId = setTimeout(() => { cacheMap.delete(id); timersMap.delete(id); }, chacheTTL); timersMap.set(id, timerId); } } return data; }; }; } /** * Generated bundle index. Do not edit. */ export { InMemoryCache }; //# sourceMappingURL=angular-caching-inmemory-cache.js.map