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