1 | import { defaultMemoize, defaultEqualityCheck } from './defaultMemoize';
|
2 | export { defaultMemoize, defaultEqualityCheck };
|
3 |
|
4 | function getDependencies(funcs) {
|
5 | var dependencies = Array.isArray(funcs[0]) ? funcs[0] : funcs;
|
6 |
|
7 | if (!dependencies.every(function (dep) {
|
8 | return typeof dep === 'function';
|
9 | })) {
|
10 | var dependencyTypes = dependencies.map(function (dep) {
|
11 | return typeof dep === 'function' ? "function " + (dep.name || 'unnamed') + "()" : typeof dep;
|
12 | }).join(', ');
|
13 | throw new Error("createSelector expects all input-selectors to be functions, but received the following types: [" + dependencyTypes + "]");
|
14 | }
|
15 |
|
16 | return dependencies;
|
17 | }
|
18 |
|
19 | export function createSelectorCreator(memoize) {
|
20 | for (var _len = arguments.length, memoizeOptionsFromArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
21 | memoizeOptionsFromArgs[_key - 1] = arguments[_key];
|
22 | }
|
23 |
|
24 | var createSelector = function createSelector() {
|
25 | for (var _len2 = arguments.length, funcs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
26 | funcs[_key2] = arguments[_key2];
|
27 | }
|
28 |
|
29 | var _recomputations = 0;
|
30 |
|
31 | var _lastResult;
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | var directlyPassedOptions = {
|
37 | memoizeOptions: undefined
|
38 | };
|
39 |
|
40 | var resultFunc = funcs.pop();
|
41 |
|
42 | if (typeof resultFunc === 'object') {
|
43 | directlyPassedOptions = resultFunc;
|
44 |
|
45 | resultFunc = funcs.pop();
|
46 | }
|
47 |
|
48 | if (typeof resultFunc !== 'function') {
|
49 | throw new Error("createSelector expects an output function after the inputs, but received: [" + typeof resultFunc + "]");
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 | var _directlyPassedOption = directlyPassedOptions,
|
55 | _directlyPassedOption2 = _directlyPassedOption.memoizeOptions,
|
56 | memoizeOptions = _directlyPassedOption2 === void 0 ? memoizeOptionsFromArgs : _directlyPassedOption2;
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | var finalMemoizeOptions = Array.isArray(memoizeOptions) ? memoizeOptions : [memoizeOptions];
|
63 | var dependencies = getDependencies(funcs);
|
64 | var memoizedResultFunc = memoize.apply(void 0, [function recomputationWrapper() {
|
65 | _recomputations++;
|
66 |
|
67 | return resultFunc.apply(null, arguments);
|
68 | }].concat(finalMemoizeOptions));
|
69 |
|
70 | var selector = memoize(function dependenciesChecker() {
|
71 | var params = [];
|
72 | var length = dependencies.length;
|
73 |
|
74 | for (var i = 0; i < length; i++) {
|
75 |
|
76 |
|
77 | params.push(dependencies[i].apply(null, arguments));
|
78 | }
|
79 |
|
80 |
|
81 | _lastResult = memoizedResultFunc.apply(null, params);
|
82 | return _lastResult;
|
83 | });
|
84 | Object.assign(selector, {
|
85 | resultFunc: resultFunc,
|
86 | memoizedResultFunc: memoizedResultFunc,
|
87 | dependencies: dependencies,
|
88 | lastResult: function lastResult() {
|
89 | return _lastResult;
|
90 | },
|
91 | recomputations: function recomputations() {
|
92 | return _recomputations;
|
93 | },
|
94 | resetRecomputations: function resetRecomputations() {
|
95 | return _recomputations = 0;
|
96 | }
|
97 | });
|
98 | return selector;
|
99 | };
|
100 |
|
101 |
|
102 | return createSelector;
|
103 | }
|
104 | export var createSelector = createSelectorCreator(defaultMemoize);
|
105 |
|
106 | export var createStructuredSelector = function createStructuredSelector(selectors, selectorCreator) {
|
107 | if (selectorCreator === void 0) {
|
108 | selectorCreator = createSelector;
|
109 | }
|
110 |
|
111 | if (typeof selectors !== 'object') {
|
112 | throw new Error('createStructuredSelector expects first argument to be an object ' + ("where each property is a selector, instead received a " + typeof selectors));
|
113 | }
|
114 |
|
115 | var objectKeys = Object.keys(selectors);
|
116 | var resultSelector = selectorCreator(
|
117 | objectKeys.map(function (key) {
|
118 | return selectors[key];
|
119 | }), function () {
|
120 | for (var _len3 = arguments.length, values = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
121 | values[_key3] = arguments[_key3];
|
122 | }
|
123 |
|
124 | return values.reduce(function (composition, value, index) {
|
125 | composition[objectKeys[index]] = value;
|
126 | return composition;
|
127 | }, {});
|
128 | });
|
129 | return resultSelector;
|
130 | }; |
\ | No newline at end of file |