1 | "use strict";
|
---|
2 | var __defProp = Object.defineProperty;
|
---|
3 | var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
---|
4 | var __getOwnPropNames = Object.getOwnPropertyNames;
|
---|
5 | var __hasOwnProp = Object.prototype.hasOwnProperty;
|
---|
6 | var __export = (target, all) => {
|
---|
7 | for (var name in all)
|
---|
8 | __defProp(target, name, { get: all[name], enumerable: true });
|
---|
9 | };
|
---|
10 | var __copyProps = (to, from, except, desc) => {
|
---|
11 | if (from && typeof from === "object" || typeof from === "function") {
|
---|
12 | for (let key of __getOwnPropNames(from))
|
---|
13 | if (!__hasOwnProp.call(to, key) && key !== except)
|
---|
14 | __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
---|
15 | }
|
---|
16 | return to;
|
---|
17 | };
|
---|
18 | var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
---|
19 |
|
---|
20 | // src/index.ts
|
---|
21 | var src_exports = {};
|
---|
22 | __export(src_exports, {
|
---|
23 | createSelector: () => createSelector,
|
---|
24 | createSelectorCreator: () => createSelectorCreator,
|
---|
25 | createStructuredSelector: () => createStructuredSelector,
|
---|
26 | lruMemoize: () => lruMemoize,
|
---|
27 | referenceEqualityCheck: () => referenceEqualityCheck,
|
---|
28 | setGlobalDevModeChecks: () => setGlobalDevModeChecks,
|
---|
29 | unstable_autotrackMemoize: () => autotrackMemoize,
|
---|
30 | weakMapMemoize: () => weakMapMemoize
|
---|
31 | });
|
---|
32 | module.exports = __toCommonJS(src_exports);
|
---|
33 |
|
---|
34 | // src/devModeChecks/identityFunctionCheck.ts
|
---|
35 | var runIdentityFunctionCheck = (resultFunc, inputSelectorsResults, outputSelectorResult) => {
|
---|
36 | if (inputSelectorsResults.length === 1 && inputSelectorsResults[0] === outputSelectorResult) {
|
---|
37 | let isInputSameAsOutput = false;
|
---|
38 | try {
|
---|
39 | const emptyObject = {};
|
---|
40 | if (resultFunc(emptyObject) === emptyObject)
|
---|
41 | isInputSameAsOutput = true;
|
---|
42 | } catch {
|
---|
43 | }
|
---|
44 | if (isInputSameAsOutput) {
|
---|
45 | let stack = void 0;
|
---|
46 | try {
|
---|
47 | throw new Error();
|
---|
48 | } catch (e) {
|
---|
49 | ;
|
---|
50 | ({ stack } = e);
|
---|
51 | }
|
---|
52 | console.warn(
|
---|
53 | "The result function returned its own inputs without modification. e.g\n`createSelector([state => state.todos], todos => todos)`\nThis could lead to inefficient memoization and unnecessary re-renders.\nEnsure transformation logic is in the result function, and extraction logic is in the input selectors.",
|
---|
54 | { stack }
|
---|
55 | );
|
---|
56 | }
|
---|
57 | }
|
---|
58 | };
|
---|
59 |
|
---|
60 | // src/devModeChecks/inputStabilityCheck.ts
|
---|
61 | var runInputStabilityCheck = (inputSelectorResultsObject, options, inputSelectorArgs) => {
|
---|
62 | const { memoize, memoizeOptions } = options;
|
---|
63 | const { inputSelectorResults, inputSelectorResultsCopy } = inputSelectorResultsObject;
|
---|
64 | const createAnEmptyObject = memoize(() => ({}), ...memoizeOptions);
|
---|
65 | const areInputSelectorResultsEqual = createAnEmptyObject.apply(null, inputSelectorResults) === createAnEmptyObject.apply(null, inputSelectorResultsCopy);
|
---|
66 | if (!areInputSelectorResultsEqual) {
|
---|
67 | let stack = void 0;
|
---|
68 | try {
|
---|
69 | throw new Error();
|
---|
70 | } catch (e) {
|
---|
71 | ;
|
---|
72 | ({ stack } = e);
|
---|
73 | }
|
---|
74 | console.warn(
|
---|
75 | "An input selector returned a different result when passed same arguments.\nThis means your output selector will likely run more frequently than intended.\nAvoid returning a new reference inside your input selector, e.g.\n`createSelector([state => state.todos.map(todo => todo.id)], todoIds => todoIds.length)`",
|
---|
76 | {
|
---|
77 | arguments: inputSelectorArgs,
|
---|
78 | firstInputs: inputSelectorResults,
|
---|
79 | secondInputs: inputSelectorResultsCopy,
|
---|
80 | stack
|
---|
81 | }
|
---|
82 | );
|
---|
83 | }
|
---|
84 | };
|
---|
85 |
|
---|
86 | // src/devModeChecks/setGlobalDevModeChecks.ts
|
---|
87 | var globalDevModeChecks = {
|
---|
88 | inputStabilityCheck: "once",
|
---|
89 | identityFunctionCheck: "once"
|
---|
90 | };
|
---|
91 | var setGlobalDevModeChecks = (devModeChecks) => {
|
---|
92 | Object.assign(globalDevModeChecks, devModeChecks);
|
---|
93 | };
|
---|
94 |
|
---|
95 | // src/utils.ts
|
---|
96 | var NOT_FOUND = "NOT_FOUND";
|
---|
97 | function assertIsFunction(func, errorMessage = `expected a function, instead received ${typeof func}`) {
|
---|
98 | if (typeof func !== "function") {
|
---|
99 | throw new TypeError(errorMessage);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | function assertIsObject(object, errorMessage = `expected an object, instead received ${typeof object}`) {
|
---|
103 | if (typeof object !== "object") {
|
---|
104 | throw new TypeError(errorMessage);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | function assertIsArrayOfFunctions(array, errorMessage = `expected all items to be functions, instead received the following types: `) {
|
---|
108 | if (!array.every((item) => typeof item === "function")) {
|
---|
109 | const itemTypes = array.map(
|
---|
110 | (item) => typeof item === "function" ? `function ${item.name || "unnamed"}()` : typeof item
|
---|
111 | ).join(", ");
|
---|
112 | throw new TypeError(`${errorMessage}[${itemTypes}]`);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | var ensureIsArray = (item) => {
|
---|
116 | return Array.isArray(item) ? item : [item];
|
---|
117 | };
|
---|
118 | function getDependencies(createSelectorArgs) {
|
---|
119 | const dependencies = Array.isArray(createSelectorArgs[0]) ? createSelectorArgs[0] : createSelectorArgs;
|
---|
120 | assertIsArrayOfFunctions(
|
---|
121 | dependencies,
|
---|
122 | `createSelector expects all input-selectors to be functions, but received the following types: `
|
---|
123 | );
|
---|
124 | return dependencies;
|
---|
125 | }
|
---|
126 | function collectInputSelectorResults(dependencies, inputSelectorArgs) {
|
---|
127 | const inputSelectorResults = [];
|
---|
128 | const { length } = dependencies;
|
---|
129 | for (let i = 0; i < length; i++) {
|
---|
130 | inputSelectorResults.push(dependencies[i].apply(null, inputSelectorArgs));
|
---|
131 | }
|
---|
132 | return inputSelectorResults;
|
---|
133 | }
|
---|
134 | var getDevModeChecksExecutionInfo = (firstRun, devModeChecks) => {
|
---|
135 | const { identityFunctionCheck, inputStabilityCheck } = {
|
---|
136 | ...globalDevModeChecks,
|
---|
137 | ...devModeChecks
|
---|
138 | };
|
---|
139 | return {
|
---|
140 | identityFunctionCheck: {
|
---|
141 | shouldRun: identityFunctionCheck === "always" || identityFunctionCheck === "once" && firstRun,
|
---|
142 | run: runIdentityFunctionCheck
|
---|
143 | },
|
---|
144 | inputStabilityCheck: {
|
---|
145 | shouldRun: inputStabilityCheck === "always" || inputStabilityCheck === "once" && firstRun,
|
---|
146 | run: runInputStabilityCheck
|
---|
147 | }
|
---|
148 | };
|
---|
149 | };
|
---|
150 |
|
---|
151 | // src/autotrackMemoize/autotracking.ts
|
---|
152 | var $REVISION = 0;
|
---|
153 | var CURRENT_TRACKER = null;
|
---|
154 | var Cell = class {
|
---|
155 | revision = $REVISION;
|
---|
156 | _value;
|
---|
157 | _lastValue;
|
---|
158 | _isEqual = tripleEq;
|
---|
159 | constructor(initialValue, isEqual = tripleEq) {
|
---|
160 | this._value = this._lastValue = initialValue;
|
---|
161 | this._isEqual = isEqual;
|
---|
162 | }
|
---|
163 | // Whenever a storage value is read, it'll add itself to the current tracker if
|
---|
164 | // one exists, entangling its state with that cache.
|
---|
165 | get value() {
|
---|
166 | CURRENT_TRACKER?.add(this);
|
---|
167 | return this._value;
|
---|
168 | }
|
---|
169 | // Whenever a storage value is updated, we bump the global revision clock,
|
---|
170 | // assign the revision for this storage to the new value, _and_ we schedule a
|
---|
171 | // rerender. This is important, and it's what makes autotracking _pull_
|
---|
172 | // based. We don't actively tell the caches which depend on the storage that
|
---|
173 | // anything has happened. Instead, we recompute the caches when needed.
|
---|
174 | set value(newValue) {
|
---|
175 | if (this.value === newValue)
|
---|
176 | return;
|
---|
177 | this._value = newValue;
|
---|
178 | this.revision = ++$REVISION;
|
---|
179 | }
|
---|
180 | };
|
---|
181 | function tripleEq(a, b) {
|
---|
182 | return a === b;
|
---|
183 | }
|
---|
184 | var TrackingCache = class {
|
---|
185 | _cachedValue;
|
---|
186 | _cachedRevision = -1;
|
---|
187 | _deps = [];
|
---|
188 | hits = 0;
|
---|
189 | fn;
|
---|
190 | constructor(fn) {
|
---|
191 | this.fn = fn;
|
---|
192 | }
|
---|
193 | clear() {
|
---|
194 | this._cachedValue = void 0;
|
---|
195 | this._cachedRevision = -1;
|
---|
196 | this._deps = [];
|
---|
197 | this.hits = 0;
|
---|
198 | }
|
---|
199 | get value() {
|
---|
200 | if (this.revision > this._cachedRevision) {
|
---|
201 | const { fn } = this;
|
---|
202 | const currentTracker = /* @__PURE__ */ new Set();
|
---|
203 | const prevTracker = CURRENT_TRACKER;
|
---|
204 | CURRENT_TRACKER = currentTracker;
|
---|
205 | this._cachedValue = fn();
|
---|
206 | CURRENT_TRACKER = prevTracker;
|
---|
207 | this.hits++;
|
---|
208 | this._deps = Array.from(currentTracker);
|
---|
209 | this._cachedRevision = this.revision;
|
---|
210 | }
|
---|
211 | CURRENT_TRACKER?.add(this);
|
---|
212 | return this._cachedValue;
|
---|
213 | }
|
---|
214 | get revision() {
|
---|
215 | return Math.max(...this._deps.map((d) => d.revision), 0);
|
---|
216 | }
|
---|
217 | };
|
---|
218 | function getValue(cell) {
|
---|
219 | if (!(cell instanceof Cell)) {
|
---|
220 | console.warn("Not a valid cell! ", cell);
|
---|
221 | }
|
---|
222 | return cell.value;
|
---|
223 | }
|
---|
224 | function setValue(storage, value) {
|
---|
225 | if (!(storage instanceof Cell)) {
|
---|
226 | throw new TypeError(
|
---|
227 | "setValue must be passed a tracked store created with `createStorage`."
|
---|
228 | );
|
---|
229 | }
|
---|
230 | storage.value = storage._lastValue = value;
|
---|
231 | }
|
---|
232 | function createCell(initialValue, isEqual = tripleEq) {
|
---|
233 | return new Cell(initialValue, isEqual);
|
---|
234 | }
|
---|
235 | function createCache(fn) {
|
---|
236 | assertIsFunction(
|
---|
237 | fn,
|
---|
238 | "the first parameter to `createCache` must be a function"
|
---|
239 | );
|
---|
240 | return new TrackingCache(fn);
|
---|
241 | }
|
---|
242 |
|
---|
243 | // src/autotrackMemoize/tracking.ts
|
---|
244 | var neverEq = (a, b) => false;
|
---|
245 | function createTag() {
|
---|
246 | return createCell(null, neverEq);
|
---|
247 | }
|
---|
248 | function dirtyTag(tag, value) {
|
---|
249 | setValue(tag, value);
|
---|
250 | }
|
---|
251 | var consumeCollection = (node) => {
|
---|
252 | let tag = node.collectionTag;
|
---|
253 | if (tag === null) {
|
---|
254 | tag = node.collectionTag = createTag();
|
---|
255 | }
|
---|
256 | getValue(tag);
|
---|
257 | };
|
---|
258 | var dirtyCollection = (node) => {
|
---|
259 | const tag = node.collectionTag;
|
---|
260 | if (tag !== null) {
|
---|
261 | dirtyTag(tag, null);
|
---|
262 | }
|
---|
263 | };
|
---|
264 |
|
---|
265 | // src/autotrackMemoize/proxy.ts
|
---|
266 | var REDUX_PROXY_LABEL = Symbol();
|
---|
267 | var nextId = 0;
|
---|
268 | var proto = Object.getPrototypeOf({});
|
---|
269 | var ObjectTreeNode = class {
|
---|
270 | constructor(value) {
|
---|
271 | this.value = value;
|
---|
272 | this.value = value;
|
---|
273 | this.tag.value = value;
|
---|
274 | }
|
---|
275 | proxy = new Proxy(this, objectProxyHandler);
|
---|
276 | tag = createTag();
|
---|
277 | tags = {};
|
---|
278 | children = {};
|
---|
279 | collectionTag = null;
|
---|
280 | id = nextId++;
|
---|
281 | };
|
---|
282 | var objectProxyHandler = {
|
---|
283 | get(node, key) {
|
---|
284 | function calculateResult() {
|
---|
285 | const { value } = node;
|
---|
286 | const childValue = Reflect.get(value, key);
|
---|
287 | if (typeof key === "symbol") {
|
---|
288 | return childValue;
|
---|
289 | }
|
---|
290 | if (key in proto) {
|
---|
291 | return childValue;
|
---|
292 | }
|
---|
293 | if (typeof childValue === "object" && childValue !== null) {
|
---|
294 | let childNode = node.children[key];
|
---|
295 | if (childNode === void 0) {
|
---|
296 | childNode = node.children[key] = createNode(childValue);
|
---|
297 | }
|
---|
298 | if (childNode.tag) {
|
---|
299 | getValue(childNode.tag);
|
---|
300 | }
|
---|
301 | return childNode.proxy;
|
---|
302 | } else {
|
---|
303 | let tag = node.tags[key];
|
---|
304 | if (tag === void 0) {
|
---|
305 | tag = node.tags[key] = createTag();
|
---|
306 | tag.value = childValue;
|
---|
307 | }
|
---|
308 | getValue(tag);
|
---|
309 | return childValue;
|
---|
310 | }
|
---|
311 | }
|
---|
312 | const res = calculateResult();
|
---|
313 | return res;
|
---|
314 | },
|
---|
315 | ownKeys(node) {
|
---|
316 | consumeCollection(node);
|
---|
317 | return Reflect.ownKeys(node.value);
|
---|
318 | },
|
---|
319 | getOwnPropertyDescriptor(node, prop) {
|
---|
320 | return Reflect.getOwnPropertyDescriptor(node.value, prop);
|
---|
321 | },
|
---|
322 | has(node, prop) {
|
---|
323 | return Reflect.has(node.value, prop);
|
---|
324 | }
|
---|
325 | };
|
---|
326 | var ArrayTreeNode = class {
|
---|
327 | constructor(value) {
|
---|
328 | this.value = value;
|
---|
329 | this.value = value;
|
---|
330 | this.tag.value = value;
|
---|
331 | }
|
---|
332 | proxy = new Proxy([this], arrayProxyHandler);
|
---|
333 | tag = createTag();
|
---|
334 | tags = {};
|
---|
335 | children = {};
|
---|
336 | collectionTag = null;
|
---|
337 | id = nextId++;
|
---|
338 | };
|
---|
339 | var arrayProxyHandler = {
|
---|
340 | get([node], key) {
|
---|
341 | if (key === "length") {
|
---|
342 | consumeCollection(node);
|
---|
343 | }
|
---|
344 | return objectProxyHandler.get(node, key);
|
---|
345 | },
|
---|
346 | ownKeys([node]) {
|
---|
347 | return objectProxyHandler.ownKeys(node);
|
---|
348 | },
|
---|
349 | getOwnPropertyDescriptor([node], prop) {
|
---|
350 | return objectProxyHandler.getOwnPropertyDescriptor(node, prop);
|
---|
351 | },
|
---|
352 | has([node], prop) {
|
---|
353 | return objectProxyHandler.has(node, prop);
|
---|
354 | }
|
---|
355 | };
|
---|
356 | function createNode(value) {
|
---|
357 | if (Array.isArray(value)) {
|
---|
358 | return new ArrayTreeNode(value);
|
---|
359 | }
|
---|
360 | return new ObjectTreeNode(value);
|
---|
361 | }
|
---|
362 | function updateNode(node, newValue) {
|
---|
363 | const { value, tags, children } = node;
|
---|
364 | node.value = newValue;
|
---|
365 | if (Array.isArray(value) && Array.isArray(newValue) && value.length !== newValue.length) {
|
---|
366 | dirtyCollection(node);
|
---|
367 | } else {
|
---|
368 | if (value !== newValue) {
|
---|
369 | let oldKeysSize = 0;
|
---|
370 | let newKeysSize = 0;
|
---|
371 | let anyKeysAdded = false;
|
---|
372 | for (const _key in value) {
|
---|
373 | oldKeysSize++;
|
---|
374 | }
|
---|
375 | for (const key in newValue) {
|
---|
376 | newKeysSize++;
|
---|
377 | if (!(key in value)) {
|
---|
378 | anyKeysAdded = true;
|
---|
379 | break;
|
---|
380 | }
|
---|
381 | }
|
---|
382 | const isDifferent = anyKeysAdded || oldKeysSize !== newKeysSize;
|
---|
383 | if (isDifferent) {
|
---|
384 | dirtyCollection(node);
|
---|
385 | }
|
---|
386 | }
|
---|
387 | }
|
---|
388 | for (const key in tags) {
|
---|
389 | const childValue = value[key];
|
---|
390 | const newChildValue = newValue[key];
|
---|
391 | if (childValue !== newChildValue) {
|
---|
392 | dirtyCollection(node);
|
---|
393 | dirtyTag(tags[key], newChildValue);
|
---|
394 | }
|
---|
395 | if (typeof newChildValue === "object" && newChildValue !== null) {
|
---|
396 | delete tags[key];
|
---|
397 | }
|
---|
398 | }
|
---|
399 | for (const key in children) {
|
---|
400 | const childNode = children[key];
|
---|
401 | const newChildValue = newValue[key];
|
---|
402 | const childValue = childNode.value;
|
---|
403 | if (childValue === newChildValue) {
|
---|
404 | continue;
|
---|
405 | } else if (typeof newChildValue === "object" && newChildValue !== null) {
|
---|
406 | updateNode(childNode, newChildValue);
|
---|
407 | } else {
|
---|
408 | deleteNode(childNode);
|
---|
409 | delete children[key];
|
---|
410 | }
|
---|
411 | }
|
---|
412 | }
|
---|
413 | function deleteNode(node) {
|
---|
414 | if (node.tag) {
|
---|
415 | dirtyTag(node.tag, null);
|
---|
416 | }
|
---|
417 | dirtyCollection(node);
|
---|
418 | for (const key in node.tags) {
|
---|
419 | dirtyTag(node.tags[key], null);
|
---|
420 | }
|
---|
421 | for (const key in node.children) {
|
---|
422 | deleteNode(node.children[key]);
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | // src/lruMemoize.ts
|
---|
427 | function createSingletonCache(equals) {
|
---|
428 | let entry;
|
---|
429 | return {
|
---|
430 | get(key) {
|
---|
431 | if (entry && equals(entry.key, key)) {
|
---|
432 | return entry.value;
|
---|
433 | }
|
---|
434 | return NOT_FOUND;
|
---|
435 | },
|
---|
436 | put(key, value) {
|
---|
437 | entry = { key, value };
|
---|
438 | },
|
---|
439 | getEntries() {
|
---|
440 | return entry ? [entry] : [];
|
---|
441 | },
|
---|
442 | clear() {
|
---|
443 | entry = void 0;
|
---|
444 | }
|
---|
445 | };
|
---|
446 | }
|
---|
447 | function createLruCache(maxSize, equals) {
|
---|
448 | let entries = [];
|
---|
449 | function get(key) {
|
---|
450 | const cacheIndex = entries.findIndex((entry) => equals(key, entry.key));
|
---|
451 | if (cacheIndex > -1) {
|
---|
452 | const entry = entries[cacheIndex];
|
---|
453 | if (cacheIndex > 0) {
|
---|
454 | entries.splice(cacheIndex, 1);
|
---|
455 | entries.unshift(entry);
|
---|
456 | }
|
---|
457 | return entry.value;
|
---|
458 | }
|
---|
459 | return NOT_FOUND;
|
---|
460 | }
|
---|
461 | function put(key, value) {
|
---|
462 | if (get(key) === NOT_FOUND) {
|
---|
463 | entries.unshift({ key, value });
|
---|
464 | if (entries.length > maxSize) {
|
---|
465 | entries.pop();
|
---|
466 | }
|
---|
467 | }
|
---|
468 | }
|
---|
469 | function getEntries() {
|
---|
470 | return entries;
|
---|
471 | }
|
---|
472 | function clear() {
|
---|
473 | entries = [];
|
---|
474 | }
|
---|
475 | return { get, put, getEntries, clear };
|
---|
476 | }
|
---|
477 | var referenceEqualityCheck = (a, b) => a === b;
|
---|
478 | function createCacheKeyComparator(equalityCheck) {
|
---|
479 | return function areArgumentsShallowlyEqual(prev, next) {
|
---|
480 | if (prev === null || next === null || prev.length !== next.length) {
|
---|
481 | return false;
|
---|
482 | }
|
---|
483 | const { length } = prev;
|
---|
484 | for (let i = 0; i < length; i++) {
|
---|
485 | if (!equalityCheck(prev[i], next[i])) {
|
---|
486 | return false;
|
---|
487 | }
|
---|
488 | }
|
---|
489 | return true;
|
---|
490 | };
|
---|
491 | }
|
---|
492 | function lruMemoize(func, equalityCheckOrOptions) {
|
---|
493 | const providedOptions = typeof equalityCheckOrOptions === "object" ? equalityCheckOrOptions : { equalityCheck: equalityCheckOrOptions };
|
---|
494 | const {
|
---|
495 | equalityCheck = referenceEqualityCheck,
|
---|
496 | maxSize = 1,
|
---|
497 | resultEqualityCheck
|
---|
498 | } = providedOptions;
|
---|
499 | const comparator = createCacheKeyComparator(equalityCheck);
|
---|
500 | let resultsCount = 0;
|
---|
501 | const cache = maxSize === 1 ? createSingletonCache(comparator) : createLruCache(maxSize, comparator);
|
---|
502 | function memoized() {
|
---|
503 | let value = cache.get(arguments);
|
---|
504 | if (value === NOT_FOUND) {
|
---|
505 | value = func.apply(null, arguments);
|
---|
506 | resultsCount++;
|
---|
507 | if (resultEqualityCheck) {
|
---|
508 | const entries = cache.getEntries();
|
---|
509 | const matchingEntry = entries.find(
|
---|
510 | (entry) => resultEqualityCheck(entry.value, value)
|
---|
511 | );
|
---|
512 | if (matchingEntry) {
|
---|
513 | value = matchingEntry.value;
|
---|
514 | resultsCount !== 0 && resultsCount--;
|
---|
515 | }
|
---|
516 | }
|
---|
517 | cache.put(arguments, value);
|
---|
518 | }
|
---|
519 | return value;
|
---|
520 | }
|
---|
521 | memoized.clearCache = () => {
|
---|
522 | cache.clear();
|
---|
523 | memoized.resetResultsCount();
|
---|
524 | };
|
---|
525 | memoized.resultsCount = () => resultsCount;
|
---|
526 | memoized.resetResultsCount = () => {
|
---|
527 | resultsCount = 0;
|
---|
528 | };
|
---|
529 | return memoized;
|
---|
530 | }
|
---|
531 |
|
---|
532 | // src/autotrackMemoize/autotrackMemoize.ts
|
---|
533 | function autotrackMemoize(func) {
|
---|
534 | const node = createNode(
|
---|
535 | []
|
---|
536 | );
|
---|
537 | let lastArgs = null;
|
---|
538 | const shallowEqual = createCacheKeyComparator(referenceEqualityCheck);
|
---|
539 | const cache = createCache(() => {
|
---|
540 | const res = func.apply(null, node.proxy);
|
---|
541 | return res;
|
---|
542 | });
|
---|
543 | function memoized() {
|
---|
544 | if (!shallowEqual(lastArgs, arguments)) {
|
---|
545 | updateNode(node, arguments);
|
---|
546 | lastArgs = arguments;
|
---|
547 | }
|
---|
548 | return cache.value;
|
---|
549 | }
|
---|
550 | memoized.clearCache = () => {
|
---|
551 | return cache.clear();
|
---|
552 | };
|
---|
553 | return memoized;
|
---|
554 | }
|
---|
555 |
|
---|
556 | // src/weakMapMemoize.ts
|
---|
557 | var StrongRef = class {
|
---|
558 | constructor(value) {
|
---|
559 | this.value = value;
|
---|
560 | }
|
---|
561 | deref() {
|
---|
562 | return this.value;
|
---|
563 | }
|
---|
564 | };
|
---|
565 | var Ref = typeof WeakRef !== "undefined" ? WeakRef : StrongRef;
|
---|
566 | var UNTERMINATED = 0;
|
---|
567 | var TERMINATED = 1;
|
---|
568 | function createCacheNode() {
|
---|
569 | return {
|
---|
570 | s: UNTERMINATED,
|
---|
571 | v: void 0,
|
---|
572 | o: null,
|
---|
573 | p: null
|
---|
574 | };
|
---|
575 | }
|
---|
576 | function weakMapMemoize(func, options = {}) {
|
---|
577 | let fnNode = createCacheNode();
|
---|
578 | const { resultEqualityCheck } = options;
|
---|
579 | let lastResult;
|
---|
580 | let resultsCount = 0;
|
---|
581 | function memoized() {
|
---|
582 | let cacheNode = fnNode;
|
---|
583 | const { length } = arguments;
|
---|
584 | for (let i = 0, l = length; i < l; i++) {
|
---|
585 | const arg = arguments[i];
|
---|
586 | if (typeof arg === "function" || typeof arg === "object" && arg !== null) {
|
---|
587 | let objectCache = cacheNode.o;
|
---|
588 | if (objectCache === null) {
|
---|
589 | cacheNode.o = objectCache = /* @__PURE__ */ new WeakMap();
|
---|
590 | }
|
---|
591 | const objectNode = objectCache.get(arg);
|
---|
592 | if (objectNode === void 0) {
|
---|
593 | cacheNode = createCacheNode();
|
---|
594 | objectCache.set(arg, cacheNode);
|
---|
595 | } else {
|
---|
596 | cacheNode = objectNode;
|
---|
597 | }
|
---|
598 | } else {
|
---|
599 | let primitiveCache = cacheNode.p;
|
---|
600 | if (primitiveCache === null) {
|
---|
601 | cacheNode.p = primitiveCache = /* @__PURE__ */ new Map();
|
---|
602 | }
|
---|
603 | const primitiveNode = primitiveCache.get(arg);
|
---|
604 | if (primitiveNode === void 0) {
|
---|
605 | cacheNode = createCacheNode();
|
---|
606 | primitiveCache.set(arg, cacheNode);
|
---|
607 | } else {
|
---|
608 | cacheNode = primitiveNode;
|
---|
609 | }
|
---|
610 | }
|
---|
611 | }
|
---|
612 | const terminatedNode = cacheNode;
|
---|
613 | let result;
|
---|
614 | if (cacheNode.s === TERMINATED) {
|
---|
615 | result = cacheNode.v;
|
---|
616 | } else {
|
---|
617 | result = func.apply(null, arguments);
|
---|
618 | resultsCount++;
|
---|
619 | }
|
---|
620 | terminatedNode.s = TERMINATED;
|
---|
621 | if (resultEqualityCheck) {
|
---|
622 | const lastResultValue = lastResult?.deref?.() ?? lastResult;
|
---|
623 | if (lastResultValue != null && resultEqualityCheck(lastResultValue, result)) {
|
---|
624 | result = lastResultValue;
|
---|
625 | resultsCount !== 0 && resultsCount--;
|
---|
626 | }
|
---|
627 | const needsWeakRef = typeof result === "object" && result !== null || typeof result === "function";
|
---|
628 | lastResult = needsWeakRef ? new Ref(result) : result;
|
---|
629 | }
|
---|
630 | terminatedNode.v = result;
|
---|
631 | return result;
|
---|
632 | }
|
---|
633 | memoized.clearCache = () => {
|
---|
634 | fnNode = createCacheNode();
|
---|
635 | memoized.resetResultsCount();
|
---|
636 | };
|
---|
637 | memoized.resultsCount = () => resultsCount;
|
---|
638 | memoized.resetResultsCount = () => {
|
---|
639 | resultsCount = 0;
|
---|
640 | };
|
---|
641 | return memoized;
|
---|
642 | }
|
---|
643 |
|
---|
644 | // src/createSelectorCreator.ts
|
---|
645 | function createSelectorCreator(memoizeOrOptions, ...memoizeOptionsFromArgs) {
|
---|
646 | const createSelectorCreatorOptions = typeof memoizeOrOptions === "function" ? {
|
---|
647 | memoize: memoizeOrOptions,
|
---|
648 | memoizeOptions: memoizeOptionsFromArgs
|
---|
649 | } : memoizeOrOptions;
|
---|
650 | const createSelector2 = (...createSelectorArgs) => {
|
---|
651 | let recomputations = 0;
|
---|
652 | let dependencyRecomputations = 0;
|
---|
653 | let lastResult;
|
---|
654 | let directlyPassedOptions = {};
|
---|
655 | let resultFunc = createSelectorArgs.pop();
|
---|
656 | if (typeof resultFunc === "object") {
|
---|
657 | directlyPassedOptions = resultFunc;
|
---|
658 | resultFunc = createSelectorArgs.pop();
|
---|
659 | }
|
---|
660 | assertIsFunction(
|
---|
661 | resultFunc,
|
---|
662 | `createSelector expects an output function after the inputs, but received: [${typeof resultFunc}]`
|
---|
663 | );
|
---|
664 | const combinedOptions = {
|
---|
665 | ...createSelectorCreatorOptions,
|
---|
666 | ...directlyPassedOptions
|
---|
667 | };
|
---|
668 | const {
|
---|
669 | memoize,
|
---|
670 | memoizeOptions = [],
|
---|
671 | argsMemoize = weakMapMemoize,
|
---|
672 | argsMemoizeOptions = [],
|
---|
673 | devModeChecks = {}
|
---|
674 | } = combinedOptions;
|
---|
675 | const finalMemoizeOptions = ensureIsArray(memoizeOptions);
|
---|
676 | const finalArgsMemoizeOptions = ensureIsArray(argsMemoizeOptions);
|
---|
677 | const dependencies = getDependencies(createSelectorArgs);
|
---|
678 | const memoizedResultFunc = memoize(function recomputationWrapper() {
|
---|
679 | recomputations++;
|
---|
680 | return resultFunc.apply(
|
---|
681 | null,
|
---|
682 | arguments
|
---|
683 | );
|
---|
684 | }, ...finalMemoizeOptions);
|
---|
685 | let firstRun = true;
|
---|
686 | const selector = argsMemoize(function dependenciesChecker() {
|
---|
687 | dependencyRecomputations++;
|
---|
688 | const inputSelectorResults = collectInputSelectorResults(
|
---|
689 | dependencies,
|
---|
690 | arguments
|
---|
691 | );
|
---|
692 | lastResult = memoizedResultFunc.apply(null, inputSelectorResults);
|
---|
693 | if (process.env.NODE_ENV !== "production") {
|
---|
694 | const { identityFunctionCheck, inputStabilityCheck } = getDevModeChecksExecutionInfo(firstRun, devModeChecks);
|
---|
695 | if (identityFunctionCheck.shouldRun) {
|
---|
696 | identityFunctionCheck.run(
|
---|
697 | resultFunc,
|
---|
698 | inputSelectorResults,
|
---|
699 | lastResult
|
---|
700 | );
|
---|
701 | }
|
---|
702 | if (inputStabilityCheck.shouldRun) {
|
---|
703 | const inputSelectorResultsCopy = collectInputSelectorResults(
|
---|
704 | dependencies,
|
---|
705 | arguments
|
---|
706 | );
|
---|
707 | inputStabilityCheck.run(
|
---|
708 | { inputSelectorResults, inputSelectorResultsCopy },
|
---|
709 | { memoize, memoizeOptions: finalMemoizeOptions },
|
---|
710 | arguments
|
---|
711 | );
|
---|
712 | }
|
---|
713 | if (firstRun)
|
---|
714 | firstRun = false;
|
---|
715 | }
|
---|
716 | return lastResult;
|
---|
717 | }, ...finalArgsMemoizeOptions);
|
---|
718 | return Object.assign(selector, {
|
---|
719 | resultFunc,
|
---|
720 | memoizedResultFunc,
|
---|
721 | dependencies,
|
---|
722 | dependencyRecomputations: () => dependencyRecomputations,
|
---|
723 | resetDependencyRecomputations: () => {
|
---|
724 | dependencyRecomputations = 0;
|
---|
725 | },
|
---|
726 | lastResult: () => lastResult,
|
---|
727 | recomputations: () => recomputations,
|
---|
728 | resetRecomputations: () => {
|
---|
729 | recomputations = 0;
|
---|
730 | },
|
---|
731 | memoize,
|
---|
732 | argsMemoize
|
---|
733 | });
|
---|
734 | };
|
---|
735 | Object.assign(createSelector2, {
|
---|
736 | withTypes: () => createSelector2
|
---|
737 | });
|
---|
738 | return createSelector2;
|
---|
739 | }
|
---|
740 | var createSelector = /* @__PURE__ */ createSelectorCreator(weakMapMemoize);
|
---|
741 |
|
---|
742 | // src/createStructuredSelector.ts
|
---|
743 | var createStructuredSelector = Object.assign(
|
---|
744 | (inputSelectorsObject, selectorCreator = createSelector) => {
|
---|
745 | assertIsObject(
|
---|
746 | inputSelectorsObject,
|
---|
747 | `createStructuredSelector expects first argument to be an object where each property is a selector, instead received a ${typeof inputSelectorsObject}`
|
---|
748 | );
|
---|
749 | const inputSelectorKeys = Object.keys(inputSelectorsObject);
|
---|
750 | const dependencies = inputSelectorKeys.map(
|
---|
751 | (key) => inputSelectorsObject[key]
|
---|
752 | );
|
---|
753 | const structuredSelector = selectorCreator(
|
---|
754 | dependencies,
|
---|
755 | (...inputSelectorResults) => {
|
---|
756 | return inputSelectorResults.reduce((composition, value, index) => {
|
---|
757 | composition[inputSelectorKeys[index]] = value;
|
---|
758 | return composition;
|
---|
759 | }, {});
|
---|
760 | }
|
---|
761 | );
|
---|
762 | return structuredSelector;
|
---|
763 | },
|
---|
764 | { withTypes: () => createStructuredSelector }
|
---|
765 | );
|
---|
766 | // Annotate the CommonJS export names for ESM import in node:
|
---|
767 | 0 && (module.exports = {
|
---|
768 | createSelector,
|
---|
769 | createSelectorCreator,
|
---|
770 | createStructuredSelector,
|
---|
771 | lruMemoize,
|
---|
772 | referenceEqualityCheck,
|
---|
773 | setGlobalDevModeChecks,
|
---|
774 | unstable_autotrackMemoize,
|
---|
775 | weakMapMemoize
|
---|
776 | });
|
---|
777 | //# sourceMappingURL=reselect.cjs.map |
---|