reselect.cjs 22 KB

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