UNPKG

5.86 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createSelector = void 0;
7exports.createSelectorCreator = createSelectorCreator;
8exports.createStructuredSelector = void 0;
9Object.defineProperty(exports, "defaultEqualityCheck", {
10 enumerable: true,
11 get: function get() {
12 return _defaultMemoize.defaultEqualityCheck;
13 }
14});
15Object.defineProperty(exports, "defaultMemoize", {
16 enumerable: true,
17 get: function get() {
18 return _defaultMemoize.defaultMemoize;
19 }
20});
21
22var _defaultMemoize = require("./defaultMemoize");
23
24function 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
39function 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; // Due to the intricacies of rest params, we can't do an optional arg after `...funcs`.
52 // So, start by declaring the default value here.
53 // (And yes, the words 'memoize' and 'options' appear too many times in this next sequence.)
54
55
56 var directlyPassedOptions = {
57 memoizeOptions: undefined
58 }; // Normally, the result func or "output selector" is the last arg
59
60 var resultFunc = funcs.pop(); // If the result func is actually an _object_, assume it's our options object
61
62 if (typeof resultFunc === 'object') {
63 directlyPassedOptions = resultFunc; // and pop the real result func off
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 } // Determine which set of options we're using. Prefer options passed directly,
71 // but fall back to options given to createSelectorCreator.
72
73
74 var _directlyPassedOption = directlyPassedOptions,
75 _directlyPassedOption2 = _directlyPassedOption.memoizeOptions,
76 memoizeOptions = _directlyPassedOption2 === void 0 ? memoizeOptionsFromArgs : _directlyPassedOption2; // Simplifying assumption: it's unlikely that the first options arg of the provided memoizer
77 // is an array. In most libs I've looked at, it's an equality function or options object.
78 // Based on that, if `memoizeOptions` _is_ an array, we assume it's a full
79 // user-provided array of options. Otherwise, it must be just the _first_ arg, and so
80 // we wrap it in an array so we can apply it.
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++; // apply arguments instead of spreading for performance.
86
87 return resultFunc.apply(null, arguments);
88 }].concat(finalMemoizeOptions)); // If a selector is called with the exact same arguments we don't need to traverse our dependencies again.
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 // apply arguments instead of spreading and mutate a local list of params for performance.
96 // @ts-ignore
97 params.push(dependencies[i].apply(null, arguments));
98 } // apply arguments instead of spreading for performance.
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 }; // @ts-ignore
120
121
122 return createSelector;
123}
124
125var createSelector = /* #__PURE__ */createSelectorCreator(_defaultMemoize.defaultMemoize);
126exports.createSelector = createSelector;
127
128// Manual definition of state and output arguments
129var 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( // @ts-ignore
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
155exports.createStructuredSelector = createStructuredSelector;
\No newline at end of file