reselect.js 4.6 KB

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