1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createCacheKeyComparator = createCacheKeyComparator;
|
7 | exports.defaultEqualityCheck = void 0;
|
8 | exports.defaultMemoize = defaultMemoize;
|
9 |
|
10 |
|
11 | var NOT_FOUND = 'NOT_FOUND';
|
12 |
|
13 | function createSingletonCache(equals) {
|
14 | var entry;
|
15 | return {
|
16 | get: function get(key) {
|
17 | if (entry && equals(entry.key, key)) {
|
18 | return entry.value;
|
19 | }
|
20 |
|
21 | return NOT_FOUND;
|
22 | },
|
23 | put: function put(key, value) {
|
24 | entry = {
|
25 | key: key,
|
26 | value: value
|
27 | };
|
28 | },
|
29 | getEntries: function getEntries() {
|
30 | return entry ? [entry] : [];
|
31 | },
|
32 | clear: function clear() {
|
33 | entry = undefined;
|
34 | }
|
35 | };
|
36 | }
|
37 |
|
38 | function createLruCache(maxSize, equals) {
|
39 | var entries = [];
|
40 |
|
41 | function get(key) {
|
42 | var cacheIndex = entries.findIndex(function (entry) {
|
43 | return equals(key, entry.key);
|
44 | });
|
45 |
|
46 | if (cacheIndex > -1) {
|
47 | var entry = entries[cacheIndex];
|
48 |
|
49 | if (cacheIndex > 0) {
|
50 | entries.splice(cacheIndex, 1);
|
51 | entries.unshift(entry);
|
52 | }
|
53 |
|
54 | return entry.value;
|
55 | }
|
56 |
|
57 |
|
58 | return NOT_FOUND;
|
59 | }
|
60 |
|
61 | function put(key, value) {
|
62 | if (get(key) === NOT_FOUND) {
|
63 |
|
64 | entries.unshift({
|
65 | key: key,
|
66 | value: value
|
67 | });
|
68 |
|
69 | if (entries.length > maxSize) {
|
70 | entries.pop();
|
71 | }
|
72 | }
|
73 | }
|
74 |
|
75 | function getEntries() {
|
76 | return entries;
|
77 | }
|
78 |
|
79 | function clear() {
|
80 | entries = [];
|
81 | }
|
82 |
|
83 | return {
|
84 | get: get,
|
85 | put: put,
|
86 | getEntries: getEntries,
|
87 | clear: clear
|
88 | };
|
89 | }
|
90 |
|
91 | var defaultEqualityCheck = function defaultEqualityCheck(a, b) {
|
92 | return a === b;
|
93 | };
|
94 |
|
95 | exports.defaultEqualityCheck = defaultEqualityCheck;
|
96 |
|
97 | function createCacheKeyComparator(equalityCheck) {
|
98 | return function areArgumentsShallowlyEqual(prev, next) {
|
99 | if (prev === null || next === null || prev.length !== next.length) {
|
100 | return false;
|
101 | }
|
102 |
|
103 |
|
104 | var length = prev.length;
|
105 |
|
106 | for (var i = 0; i < length; i++) {
|
107 | if (!equalityCheck(prev[i], next[i])) {
|
108 | return false;
|
109 | }
|
110 | }
|
111 |
|
112 | return true;
|
113 | };
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 | function defaultMemoize(func, equalityCheckOrOptions) {
|
119 | var providedOptions = typeof equalityCheckOrOptions === 'object' ? equalityCheckOrOptions : {
|
120 | equalityCheck: equalityCheckOrOptions
|
121 | };
|
122 | var _providedOptions$equa = providedOptions.equalityCheck,
|
123 | equalityCheck = _providedOptions$equa === void 0 ? defaultEqualityCheck : _providedOptions$equa,
|
124 | _providedOptions$maxS = providedOptions.maxSize,
|
125 | maxSize = _providedOptions$maxS === void 0 ? 1 : _providedOptions$maxS,
|
126 | resultEqualityCheck = providedOptions.resultEqualityCheck;
|
127 | var comparator = createCacheKeyComparator(equalityCheck);
|
128 | var cache = maxSize === 1 ? createSingletonCache(comparator) : createLruCache(maxSize, comparator);
|
129 |
|
130 | function memoized() {
|
131 | var value = cache.get(arguments);
|
132 |
|
133 | if (value === NOT_FOUND) {
|
134 |
|
135 | value = func.apply(null, arguments);
|
136 |
|
137 | if (resultEqualityCheck) {
|
138 | var entries = cache.getEntries();
|
139 | var matchingEntry = entries.find(function (entry) {
|
140 | return resultEqualityCheck(entry.value, value);
|
141 | });
|
142 |
|
143 | if (matchingEntry) {
|
144 | value = matchingEntry.value;
|
145 | }
|
146 | }
|
147 |
|
148 | cache.put(arguments, value);
|
149 | }
|
150 |
|
151 | return value;
|
152 | }
|
153 |
|
154 | memoized.clearCache = function () {
|
155 | return cache.clear();
|
156 | };
|
157 |
|
158 | return memoized;
|
159 | } |
\ | No newline at end of file |