UNPKG

4 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createCacheKeyComparator = createCacheKeyComparator;
7exports.defaultEqualityCheck = void 0;
8exports.defaultMemoize = defaultMemoize;
9// Cache implementation based on Erik Rasmussen's `lru-memoize`:
10// https://github.com/erikras/lru-memoize
11var NOT_FOUND = 'NOT_FOUND';
12
13function 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
38function 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 }); // We found a cached entry
45
46 if (cacheIndex > -1) {
47 var entry = entries[cacheIndex]; // Cached entry not at top of cache, move it to the top
48
49 if (cacheIndex > 0) {
50 entries.splice(cacheIndex, 1);
51 entries.unshift(entry);
52 }
53
54 return entry.value;
55 } // No entry found in cache, return sentinel
56
57
58 return NOT_FOUND;
59 }
60
61 function put(key, value) {
62 if (get(key) === NOT_FOUND) {
63 // TODO Is unshift slow?
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
91var defaultEqualityCheck = function defaultEqualityCheck(a, b) {
92 return a === b;
93};
94
95exports.defaultEqualityCheck = defaultEqualityCheck;
96
97function createCacheKeyComparator(equalityCheck) {
98 return function areArgumentsShallowlyEqual(prev, next) {
99 if (prev === null || next === null || prev.length !== next.length) {
100 return false;
101 } // Do this in a for loop (and not a `forEach` or an `every`) so we can determine equality as fast as possible.
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// defaultMemoize now supports a configurable cache size with LRU behavior,
117// and optional comparison of the result value with existing values
118function 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); // we reference arguments instead of spreading them for performance reasons
129
130 function memoized() {
131 var value = cache.get(arguments);
132
133 if (value === NOT_FOUND) {
134 // @ts-ignore
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