1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.memoize = memoize;
|
7 |
|
8 | var _stringify = require("./stringify");
|
9 |
|
10 |
|
11 |
|
12 | function defaultGetId() {
|
13 | return 'none';
|
14 | }
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function memoize(fn) {
|
23 | let {
|
24 | getInstanceId = defaultGetId
|
25 | } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
26 | const cache = {};
|
27 |
|
28 | const memoized = function () {
|
29 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
30 | args[_key] = arguments[_key];
|
31 | }
|
32 |
|
33 | const stringParams = (0, _stringify.stringify)(args);
|
34 | const instanceId = getInstanceId();
|
35 |
|
36 | if (!cache[instanceId]) {
|
37 | cache[instanceId] = {};
|
38 | }
|
39 |
|
40 | if (cache[instanceId][stringParams] === undefined) {
|
41 | cache[instanceId][stringParams] = fn(...args);
|
42 | }
|
43 |
|
44 | return cache[instanceId][stringParams];
|
45 | };
|
46 |
|
47 | memoized.unmemoize = function () {
|
48 | for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
49 | args[_key2] = arguments[_key2];
|
50 | }
|
51 |
|
52 | const stringParams = (0, _stringify.stringify)(args);
|
53 | const instanceId = getInstanceId();
|
54 |
|
55 | if (cache[instanceId] && cache[instanceId][stringParams] !== undefined) {
|
56 | delete cache[instanceId][stringParams];
|
57 | }
|
58 | };
|
59 |
|
60 | return memoized;
|
61 | } |
\ | No newline at end of file |