UNPKG

1.03 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.timedCache = void 0;
4function timedCache(fn, { timeout, useTimer, createKey }) {
5 const cache = new Map();
6 let prevTime = Infinity;
7 let shouldClean = false;
8 const get = (...args) => {
9 if (!shouldClean && useTimer) {
10 setTimeout(() => {
11 shouldClean = false;
12 cache.clear();
13 }, timeout);
14 }
15 shouldClean = true;
16 const current = Date.now();
17 if (current - prevTime >= timeout && !useTimer) {
18 cache.clear();
19 }
20 prevTime = current;
21 const key = createKey(args);
22 let value;
23 if (!cache.has(key)) {
24 value = fn(...args);
25 cache.set(key, value);
26 }
27 else {
28 value = cache.get(key);
29 }
30 return value;
31 };
32 return {
33 get: get,
34 cache,
35 };
36}
37exports.timedCache = timedCache;
38//# sourceMappingURL=timed-cache.js.map
\No newline at end of file