index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.defaultMemoize = defaultMemoize;
  4. exports.createSelectorCreator = createSelectorCreator;
  5. exports.createStructuredSelector = createStructuredSelector;
  6. function defaultEqualityCheck(a, b) {
  7. return a === b;
  8. }
  9. function areArgumentsShallowlyEqual(equalityCheck, prev, next) {
  10. if (prev === null || next === null || prev.length !== next.length) {
  11. return false;
  12. }
  13. // Do this in a for loop (and not a `forEach` or an `every`) so we can determine equality as fast as possible.
  14. var length = prev.length;
  15. for (var i = 0; i < length; i++) {
  16. if (!equalityCheck(prev[i], next[i])) {
  17. return false;
  18. }
  19. }
  20. return true;
  21. }
  22. function defaultMemoize(func) {
  23. var equalityCheck = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultEqualityCheck;
  24. var lastArgs = null;
  25. var lastResult = null;
  26. // we reference arguments instead of spreading them for performance reasons
  27. return function () {
  28. if (!areArgumentsShallowlyEqual(equalityCheck, lastArgs, arguments)) {
  29. // apply arguments instead of spreading for performance.
  30. lastResult = func.apply(null, arguments);
  31. }
  32. lastArgs = arguments;
  33. return lastResult;
  34. };
  35. }
  36. function getDependencies(funcs) {
  37. var dependencies = Array.isArray(funcs[0]) ? funcs[0] : funcs;
  38. if (!dependencies.every(function (dep) {
  39. return typeof dep === 'function';
  40. })) {
  41. var dependencyTypes = dependencies.map(function (dep) {
  42. return typeof dep;
  43. }).join(', ');
  44. throw new Error('Selector creators expect all input-selectors to be functions, ' + ('instead received the following types: [' + dependencyTypes + ']'));
  45. }
  46. return dependencies;
  47. }
  48. function createSelectorCreator(memoize) {
  49. for (var _len = arguments.length, memoizeOptions = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  50. memoizeOptions[_key - 1] = arguments[_key];
  51. }
  52. return function () {
  53. for (var _len2 = arguments.length, funcs = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  54. funcs[_key2] = arguments[_key2];
  55. }
  56. var recomputations = 0;
  57. var resultFunc = funcs.pop();
  58. var dependencies = getDependencies(funcs);
  59. var memoizedResultFunc = memoize.apply(undefined, [function () {
  60. recomputations++;
  61. // apply arguments instead of spreading for performance.
  62. return resultFunc.apply(null, arguments);
  63. }].concat(memoizeOptions));
  64. // If a selector is called with the exact same arguments we don't need to traverse our dependencies again.
  65. var selector = defaultMemoize(function () {
  66. var params = [];
  67. var length = dependencies.length;
  68. for (var i = 0; i < length; i++) {
  69. // apply arguments instead of spreading and mutate a local list of params for performance.
  70. params.push(dependencies[i].apply(null, arguments));
  71. }
  72. // apply arguments instead of spreading for performance.
  73. return memoizedResultFunc.apply(null, params);
  74. });
  75. selector.resultFunc = resultFunc;
  76. selector.recomputations = function () {
  77. return recomputations;
  78. };
  79. selector.resetRecomputations = function () {
  80. return recomputations = 0;
  81. };
  82. return selector;
  83. };
  84. }
  85. var createSelector = exports.createSelector = createSelectorCreator(defaultMemoize);
  86. function createStructuredSelector(selectors) {
  87. var selectorCreator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : createSelector;
  88. if (typeof selectors !== 'object') {
  89. throw new Error('createStructuredSelector expects first argument to be an object ' + ('where each property is a selector, instead received a ' + typeof selectors));
  90. }
  91. var objectKeys = Object.keys(selectors);
  92. return selectorCreator(objectKeys.map(function (key) {
  93. return selectors[key];
  94. }), function () {
  95. for (var _len3 = arguments.length, values = 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. }