index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { defaultMemoize, defaultEqualityCheck } from './defaultMemoize';
  2. export { defaultMemoize, defaultEqualityCheck };
  3. function getDependencies(funcs) {
  4. var dependencies = Array.isArray(funcs[0]) ? funcs[0] : funcs;
  5. if (!dependencies.every(function (dep) {
  6. return typeof dep === 'function';
  7. })) {
  8. var dependencyTypes = dependencies.map(function (dep) {
  9. return typeof dep === 'function' ? "function " + (dep.name || 'unnamed') + "()" : typeof dep;
  10. }).join(', ');
  11. throw new Error("createSelector expects all input-selectors to be functions, but received the following types: [" + dependencyTypes + "]");
  12. }
  13. return dependencies;
  14. }
  15. export function createSelectorCreator(memoize) {
  16. for (var _len = arguments.length, memoizeOptionsFromArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  17. memoizeOptionsFromArgs[_key - 1] = arguments[_key];
  18. }
  19. var createSelector = function createSelector() {
  20. for (var _len2 = arguments.length, funcs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  21. funcs[_key2] = arguments[_key2];
  22. }
  23. var _recomputations = 0;
  24. var _lastResult; // Due to the intricacies of rest params, we can't do an optional arg after `...funcs`.
  25. // So, start by declaring the default value here.
  26. // (And yes, the words 'memoize' and 'options' appear too many times in this next sequence.)
  27. var directlyPassedOptions = {
  28. memoizeOptions: undefined
  29. }; // Normally, the result func or "output selector" is the last arg
  30. var resultFunc = funcs.pop(); // If the result func is actually an _object_, assume it's our options object
  31. if (typeof resultFunc === 'object') {
  32. directlyPassedOptions = resultFunc; // and pop the real result func off
  33. resultFunc = funcs.pop();
  34. }
  35. if (typeof resultFunc !== 'function') {
  36. throw new Error("createSelector expects an output function after the inputs, but received: [" + typeof resultFunc + "]");
  37. } // Determine which set of options we're using. Prefer options passed directly,
  38. // but fall back to options given to createSelectorCreator.
  39. var _directlyPassedOption = directlyPassedOptions,
  40. _directlyPassedOption2 = _directlyPassedOption.memoizeOptions,
  41. memoizeOptions = _directlyPassedOption2 === void 0 ? memoizeOptionsFromArgs : _directlyPassedOption2; // Simplifying assumption: it's unlikely that the first options arg of the provided memoizer
  42. // is an array. In most libs I've looked at, it's an equality function or options object.
  43. // Based on that, if `memoizeOptions` _is_ an array, we assume it's a full
  44. // user-provided array of options. Otherwise, it must be just the _first_ arg, and so
  45. // we wrap it in an array so we can apply it.
  46. var finalMemoizeOptions = Array.isArray(memoizeOptions) ? memoizeOptions : [memoizeOptions];
  47. var dependencies = getDependencies(funcs);
  48. var memoizedResultFunc = memoize.apply(void 0, [function recomputationWrapper() {
  49. _recomputations++; // apply arguments instead of spreading for performance.
  50. return resultFunc.apply(null, arguments);
  51. }].concat(finalMemoizeOptions)); // If a selector is called with the exact same arguments we don't need to traverse our dependencies again.
  52. var selector = memoize(function dependenciesChecker() {
  53. var params = [];
  54. var length = dependencies.length;
  55. for (var i = 0; i < length; i++) {
  56. // apply arguments instead of spreading and mutate a local list of params for performance.
  57. // @ts-ignore
  58. params.push(dependencies[i].apply(null, arguments));
  59. } // apply arguments instead of spreading for performance.
  60. _lastResult = memoizedResultFunc.apply(null, params);
  61. return _lastResult;
  62. });
  63. Object.assign(selector, {
  64. resultFunc: resultFunc,
  65. memoizedResultFunc: memoizedResultFunc,
  66. dependencies: dependencies,
  67. lastResult: function lastResult() {
  68. return _lastResult;
  69. },
  70. recomputations: function recomputations() {
  71. return _recomputations;
  72. },
  73. resetRecomputations: function resetRecomputations() {
  74. return _recomputations = 0;
  75. }
  76. });
  77. return selector;
  78. }; // @ts-ignore
  79. return createSelector;
  80. }
  81. export var createSelector = /* #__PURE__ */createSelectorCreator(defaultMemoize);
  82. // Manual definition of state and output arguments
  83. export var createStructuredSelector = function createStructuredSelector(selectors, selectorCreator) {
  84. if (selectorCreator === void 0) {
  85. selectorCreator = createSelector;
  86. }
  87. if (typeof selectors !== 'object') {
  88. throw new Error('createStructuredSelector expects first argument to be an object ' + ("where each property is a selector, instead received a " + typeof selectors));
  89. }
  90. var objectKeys = Object.keys(selectors);
  91. var resultSelector = selectorCreator( // @ts-ignore
  92. objectKeys.map(function (key) {
  93. return selectors[key];
  94. }), function () {
  95. for (var _len3 = arguments.length, values = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  96. values[_key3] = arguments[_key3];
  97. }
  98. return values.reduce(function (composition, value, index) {
  99. composition[objectKeys[index]] = value;
  100. return composition;
  101. }, {});
  102. });
  103. return resultSelector;
  104. };