UNPKG

@stylable/core

Version:

CSS for Components

38 lines 1.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.timedCache = void 0; function timedCache(fn, { timeout, useTimer, createKey }) { const cache = new Map(); let prevTime = Infinity; let shouldClean = false; const get = (...args) => { if (!shouldClean && useTimer) { setTimeout(() => { shouldClean = false; cache.clear(); }, timeout); } shouldClean = true; const current = Date.now(); if (current - prevTime >= timeout && !useTimer) { cache.clear(); } prevTime = current; const key = createKey(args); let value; if (!cache.has(key)) { value = fn(...args); cache.set(key, value); } else { value = cache.get(key); } return value; }; return { get: get, cache, }; } exports.timedCache = timedCache; //# sourceMappingURL=timed-cache.js.map