reselect.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Reselect = {}));
  5. })(this, (function (exports) { 'use strict';
  6. // Cache implementation based on Erik Rasmussen's `lru-memoize`:
  7. // https://github.com/erikras/lru-memoize
  8. var NOT_FOUND = 'NOT_FOUND';
  9. function createSingletonCache(equals) {
  10. var entry;
  11. return {
  12. get: function get(key) {
  13. if (entry && equals(entry.key, key)) {
  14. return entry.value;
  15. }
  16. return NOT_FOUND;
  17. },
  18. put: function put(key, value) {
  19. entry = {
  20. key: key,
  21. value: value
  22. };
  23. },
  24. getEntries: function getEntries() {
  25. return entry ? [entry] : [];
  26. },
  27. clear: function clear() {
  28. entry = undefined;
  29. }
  30. };
  31. }
  32. function createLruCache(maxSize, equals) {
  33. var entries = [];
  34. function get(key) {
  35. var cacheIndex = entries.findIndex(function (entry) {
  36. return equals(key, entry.key);
  37. }); // We found a cached entry
  38. if (cacheIndex > -1) {
  39. var entry = entries[cacheIndex]; // Cached entry not at top of cache, move it to the top
  40. if (cacheIndex > 0) {
  41. entries.splice(cacheIndex, 1);
  42. entries.unshift(entry);
  43. }
  44. return entry.value;
  45. } // No entry found in cache, return sentinel
  46. return NOT_FOUND;
  47. }
  48. function put(key, value) {
  49. if (get(key) === NOT_FOUND) {
  50. // TODO Is unshift slow?
  51. entries.unshift({
  52. key: key,
  53. value: value
  54. });
  55. if (entries.length > maxSize) {
  56. entries.pop();
  57. }
  58. }
  59. }
  60. function getEntries() {
  61. return entries;
  62. }
  63. function clear() {
  64. entries = [];
  65. }
  66. return {
  67. get: get,
  68. put: put,
  69. getEntries: getEntries,
  70. clear: clear
  71. };
  72. }
  73. var defaultEqualityCheck = function defaultEqualityCheck(a, b) {
  74. return a === b;
  75. };
  76. function createCacheKeyComparator(equalityCheck) {
  77. return function areArgumentsShallowlyEqual(prev, next) {
  78. if (prev === null || next === null || prev.length !== next.length) {
  79. return false;
  80. } // Do this in a for loop (and not a `forEach` or an `every`) so we can determine equality as fast as possible.
  81. var length = prev.length;
  82. for (var i = 0; i < length; i++) {
  83. if (!equalityCheck(prev[i], next[i])) {
  84. return false;
  85. }
  86. }
  87. return true;
  88. };
  89. }
  90. // defaultMemoize now supports a configurable cache size with LRU behavior,
  91. // and optional comparison of the result value with existing values
  92. function defaultMemoize(func, equalityCheckOrOptions) {
  93. var providedOptions = typeof equalityCheckOrOptions === 'object' ? equalityCheckOrOptions : {
  94. equalityCheck: equalityCheckOrOptions
  95. };
  96. var _providedOptions$equa = providedOptions.equalityCheck,
  97. equalityCheck = _providedOptions$equa === void 0 ? defaultEqualityCheck : _providedOptions$equa,
  98. _providedOptions$maxS = providedOptions.maxSize,
  99. maxSize = _providedOptions$maxS === void 0 ? 1 : _providedOptions$maxS,
  100. resultEqualityCheck = providedOptions.resultEqualityCheck;
  101. var comparator = createCacheKeyComparator(equalityCheck);
  102. var cache = maxSize === 1 ? createSingletonCache(comparator) : createLruCache(maxSize, comparator); // we reference arguments instead of spreading them for performance reasons
  103. function memoized() {
  104. var value = cache.get(arguments);
  105. if (value === NOT_FOUND) {
  106. // @ts-ignore
  107. value = func.apply(null, arguments);
  108. if (resultEqualityCheck) {
  109. var entries = cache.getEntries();
  110. var matchingEntry = entries.find(function (entry) {
  111. return resultEqualityCheck(entry.value, value);
  112. });
  113. if (matchingEntry) {
  114. value = matchingEntry.value;
  115. }
  116. }
  117. cache.put(arguments, value);
  118. }
  119. return value;
  120. }
  121. memoized.clearCache = function () {
  122. return cache.clear();
  123. };
  124. return memoized;
  125. }
  126. function getDependencies(funcs) {
  127. var dependencies = Array.isArray(funcs[0]) ? funcs[0] : funcs;
  128. if (!dependencies.every(function (dep) {
  129. return typeof dep === 'function';
  130. })) {
  131. var dependencyTypes = dependencies.map(function (dep) {
  132. return typeof dep === 'function' ? "function " + (dep.name || 'unnamed') + "()" : typeof dep;
  133. }).join(', ');
  134. throw new Error("createSelector expects all input-selectors to be functions, but received the following types: [" + dependencyTypes + "]");
  135. }
  136. return dependencies;
  137. }
  138. function createSelectorCreator(memoize) {
  139. for (var _len = arguments.length, memoizeOptionsFromArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  140. memoizeOptionsFromArgs[_key - 1] = arguments[_key];
  141. }
  142. var createSelector = function createSelector() {
  143. for (var _len2 = arguments.length, funcs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  144. funcs[_key2] = arguments[_key2];
  145. }
  146. var _recomputations = 0;
  147. var _lastResult; // Due to the intricacies of rest params, we can't do an optional arg after `...funcs`.
  148. // So, start by declaring the default value here.
  149. // (And yes, the words 'memoize' and 'options' appear too many times in this next sequence.)
  150. var directlyPassedOptions = {
  151. memoizeOptions: undefined
  152. }; // Normally, the result func or "output selector" is the last arg
  153. var resultFunc = funcs.pop(); // If the result func is actually an _object_, assume it's our options object
  154. if (typeof resultFunc === 'object') {
  155. directlyPassedOptions = resultFunc; // and pop the real result func off
  156. resultFunc = funcs.pop();
  157. }
  158. if (typeof resultFunc !== 'function') {
  159. throw new Error("createSelector expects an output function after the inputs, but received: [" + typeof resultFunc + "]");
  160. } // Determine which set of options we're using. Prefer options passed directly,
  161. // but fall back to options given to createSelectorCreator.
  162. var _directlyPassedOption = directlyPassedOptions,
  163. _directlyPassedOption2 = _directlyPassedOption.memoizeOptions,
  164. memoizeOptions = _directlyPassedOption2 === void 0 ? memoizeOptionsFromArgs : _directlyPassedOption2; // Simplifying assumption: it's unlikely that the first options arg of the provided memoizer
  165. // is an array. In most libs I've looked at, it's an equality function or options object.
  166. // Based on that, if `memoizeOptions` _is_ an array, we assume it's a full
  167. // user-provided array of options. Otherwise, it must be just the _first_ arg, and so
  168. // we wrap it in an array so we can apply it.
  169. var finalMemoizeOptions = Array.isArray(memoizeOptions) ? memoizeOptions : [memoizeOptions];
  170. var dependencies = getDependencies(funcs);
  171. var memoizedResultFunc = memoize.apply(void 0, [function recomputationWrapper() {
  172. _recomputations++; // apply arguments instead of spreading for performance.
  173. return resultFunc.apply(null, arguments);
  174. }].concat(finalMemoizeOptions)); // If a selector is called with the exact same arguments we don't need to traverse our dependencies again.
  175. var selector = memoize(function dependenciesChecker() {
  176. var params = [];
  177. var length = dependencies.length;
  178. for (var i = 0; i < length; i++) {
  179. // apply arguments instead of spreading and mutate a local list of params for performance.
  180. // @ts-ignore
  181. params.push(dependencies[i].apply(null, arguments));
  182. } // apply arguments instead of spreading for performance.
  183. _lastResult = memoizedResultFunc.apply(null, params);
  184. return _lastResult;
  185. });
  186. Object.assign(selector, {
  187. resultFunc: resultFunc,
  188. memoizedResultFunc: memoizedResultFunc,
  189. dependencies: dependencies,
  190. lastResult: function lastResult() {
  191. return _lastResult;
  192. },
  193. recomputations: function recomputations() {
  194. return _recomputations;
  195. },
  196. resetRecomputations: function resetRecomputations() {
  197. return _recomputations = 0;
  198. }
  199. });
  200. return selector;
  201. }; // @ts-ignore
  202. return createSelector;
  203. }
  204. var createSelector = /* #__PURE__ */createSelectorCreator(defaultMemoize);
  205. // Manual definition of state and output arguments
  206. var createStructuredSelector = function createStructuredSelector(selectors, selectorCreator) {
  207. if (selectorCreator === void 0) {
  208. selectorCreator = createSelector;
  209. }
  210. if (typeof selectors !== 'object') {
  211. throw new Error('createStructuredSelector expects first argument to be an object ' + ("where each property is a selector, instead received a " + typeof selectors));
  212. }
  213. var objectKeys = Object.keys(selectors);
  214. var resultSelector = selectorCreator( // @ts-ignore
  215. objectKeys.map(function (key) {
  216. return selectors[key];
  217. }), function () {
  218. for (var _len3 = arguments.length, values = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  219. values[_key3] = arguments[_key3];
  220. }
  221. return values.reduce(function (composition, value, index) {
  222. composition[objectKeys[index]] = value;
  223. return composition;
  224. }, {});
  225. });
  226. return resultSelector;
  227. };
  228. exports.createSelector = createSelector;
  229. exports.createSelectorCreator = createSelectorCreator;
  230. exports.createStructuredSelector = createStructuredSelector;
  231. exports.defaultEqualityCheck = defaultEqualityCheck;
  232. exports.defaultMemoize = defaultMemoize;
  233. Object.defineProperty(exports, '__esModule', { value: true });
  234. }));