1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Memoize = Memoize;
|
4 |
|
5 | var cacheProp = Symbol.for('[memoize]');
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function Memoize(keyBuilder) {
|
11 |
|
12 | return function (_, propertyKey, descriptor) {
|
13 | if (descriptor.value != null) {
|
14 |
|
15 | descriptor.value = memoize(propertyKey, descriptor.value, keyBuilder || (function (v) { return v; }));
|
16 | }
|
17 | else if (descriptor.get != null) {
|
18 | descriptor.get = memoize(propertyKey, descriptor.get, keyBuilder || (function () { return propertyKey; }));
|
19 | }
|
20 | };
|
21 | }
|
22 |
|
23 |
|
24 | function ensureCache(target, reset) {
|
25 | if (reset === void 0) { reset = false; }
|
26 | if (reset || !target[cacheProp]) {
|
27 | Object.defineProperty(target, cacheProp, {
|
28 | value: Object.create(null),
|
29 | configurable: true,
|
30 | });
|
31 | }
|
32 | return target[cacheProp];
|
33 | }
|
34 |
|
35 |
|
36 | function ensureChildCache(target, key, reset) {
|
37 | if (reset === void 0) { reset = false; }
|
38 | var dict = ensureCache(target);
|
39 | if (reset || !dict[key]) {
|
40 |
|
41 | dict[key] = new Map();
|
42 | }
|
43 |
|
44 | return dict[key];
|
45 | }
|
46 | function memoize(namespace,
|
47 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
48 | func,
|
49 |
|
50 | keyBuilder) {
|
51 |
|
52 | return function () {
|
53 | var args = [];
|
54 | for (var _i = 0; _i < arguments.length; _i++) {
|
55 | args[_i] = arguments[_i];
|
56 | }
|
57 | var cache = ensureChildCache(this, namespace);
|
58 | var key = keyBuilder.apply(this, args);
|
59 | if (cache.has(key))
|
60 | return cache.get(key);
|
61 |
|
62 | var res = func.apply(this, args);
|
63 | cache.set(key, res);
|
64 | return res;
|
65 | };
|
66 | }
|