1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.memoize = void 0;
|
4 | const stringify_js_1 = require("./stringify.js");
|
5 | function defaultGetId() {
|
6 | return 'none';
|
7 | }
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function memoize(fn, { getInstanceId = defaultGetId } = {}) {
|
13 | const cache = {};
|
14 | const memoized = (...args) => {
|
15 | const stringParams = (0, stringify_js_1.stringify)(args);
|
16 | const instanceId = getInstanceId();
|
17 | if (!cache[instanceId]) {
|
18 | cache[instanceId] = {};
|
19 | }
|
20 | if (cache[instanceId][stringParams] === undefined) {
|
21 | cache[instanceId][stringParams] = fn(...args);
|
22 | }
|
23 | return cache[instanceId][stringParams];
|
24 | };
|
25 | memoized.unmemoize = (...args) => {
|
26 | const stringParams = (0, stringify_js_1.stringify)(args);
|
27 | const instanceId = getInstanceId();
|
28 | if (cache[instanceId]?.[stringParams] !== undefined) {
|
29 | delete cache[instanceId][stringParams];
|
30 | }
|
31 | };
|
32 | return memoized;
|
33 | }
|
34 | exports.memoize = memoize;
|