react-redux.mjs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. // src/index.ts
  2. import * as React2 from "react";
  3. import { useSyncExternalStoreWithSelector as useSyncExternalStoreWithSelector2 } from "use-sync-external-store/with-selector.js";
  4. // src/utils/reactBatchedUpdates.ts
  5. import { unstable_batchedUpdates } from "react-dom";
  6. // src/utils/batch.ts
  7. function defaultNoopBatch(callback) {
  8. callback();
  9. }
  10. var batch = defaultNoopBatch;
  11. var setBatch = (newBatch) => batch = newBatch;
  12. var getBatch = () => batch;
  13. // src/utils/react.ts
  14. import * as ReactOriginal from "react";
  15. var React = (
  16. // prettier-ignore
  17. // @ts-ignore
  18. "default" in ReactOriginal ? ReactOriginal["default"] : ReactOriginal
  19. );
  20. // src/components/Context.ts
  21. var ContextKey = Symbol.for(`react-redux-context`);
  22. var gT = typeof globalThis !== "undefined" ? globalThis : (
  23. /* fall back to a per-module scope (pre-8.1 behaviour) if `globalThis` is not available */
  24. {}
  25. );
  26. function getContext() {
  27. if (!React.createContext)
  28. return {};
  29. const contextMap = gT[ContextKey] ?? (gT[ContextKey] = /* @__PURE__ */ new Map());
  30. let realContext = contextMap.get(React.createContext);
  31. if (!realContext) {
  32. realContext = React.createContext(
  33. null
  34. );
  35. if (process.env.NODE_ENV !== "production") {
  36. realContext.displayName = "ReactRedux";
  37. }
  38. contextMap.set(React.createContext, realContext);
  39. }
  40. return realContext;
  41. }
  42. var ReactReduxContext = /* @__PURE__ */ getContext();
  43. // src/utils/useSyncExternalStore.ts
  44. var notInitialized = () => {
  45. throw new Error("uSES not initialized!");
  46. };
  47. // src/hooks/useReduxContext.ts
  48. function createReduxContextHook(context = ReactReduxContext) {
  49. return function useReduxContext2() {
  50. const contextValue = React.useContext(context);
  51. if (process.env.NODE_ENV !== "production" && !contextValue) {
  52. throw new Error(
  53. "could not find react-redux context value; please ensure the component is wrapped in a <Provider>"
  54. );
  55. }
  56. return contextValue;
  57. };
  58. }
  59. var useReduxContext = /* @__PURE__ */ createReduxContextHook();
  60. // src/hooks/useSelector.ts
  61. var useSyncExternalStoreWithSelector = notInitialized;
  62. var initializeUseSelector = (fn) => {
  63. useSyncExternalStoreWithSelector = fn;
  64. };
  65. var refEquality = (a, b) => a === b;
  66. function createSelectorHook(context = ReactReduxContext) {
  67. const useReduxContext2 = context === ReactReduxContext ? useReduxContext : createReduxContextHook(context);
  68. return function useSelector2(selector, equalityFnOrOptions = {}) {
  69. const { equalityFn = refEquality, devModeChecks = {} } = typeof equalityFnOrOptions === "function" ? { equalityFn: equalityFnOrOptions } : equalityFnOrOptions;
  70. if (process.env.NODE_ENV !== "production") {
  71. if (!selector) {
  72. throw new Error(`You must pass a selector to useSelector`);
  73. }
  74. if (typeof selector !== "function") {
  75. throw new Error(`You must pass a function as a selector to useSelector`);
  76. }
  77. if (typeof equalityFn !== "function") {
  78. throw new Error(
  79. `You must pass a function as an equality function to useSelector`
  80. );
  81. }
  82. }
  83. const {
  84. store,
  85. subscription,
  86. getServerState,
  87. stabilityCheck,
  88. identityFunctionCheck
  89. } = useReduxContext2();
  90. const firstRun = React.useRef(true);
  91. const wrappedSelector = React.useCallback(
  92. {
  93. [selector.name](state) {
  94. const selected = selector(state);
  95. if (process.env.NODE_ENV !== "production") {
  96. const {
  97. identityFunctionCheck: finalIdentityFunctionCheck,
  98. stabilityCheck: finalStabilityCheck
  99. } = {
  100. stabilityCheck,
  101. identityFunctionCheck,
  102. ...devModeChecks
  103. };
  104. if (finalStabilityCheck === "always" || finalStabilityCheck === "once" && firstRun.current) {
  105. const toCompare = selector(state);
  106. if (!equalityFn(selected, toCompare)) {
  107. let stack = void 0;
  108. try {
  109. throw new Error();
  110. } catch (e) {
  111. ;
  112. ({ stack } = e);
  113. }
  114. console.warn(
  115. "Selector " + (selector.name || "unknown") + " returned a different result when called with the same parameters. This can lead to unnecessary rerenders.\nSelectors that return a new reference (such as an object or an array) should be memoized: https://redux.js.org/usage/deriving-data-selectors#optimizing-selectors-with-memoization",
  116. {
  117. state,
  118. selected,
  119. selected2: toCompare,
  120. stack
  121. }
  122. );
  123. }
  124. }
  125. if (finalIdentityFunctionCheck === "always" || finalIdentityFunctionCheck === "once" && firstRun.current) {
  126. if (selected === state) {
  127. let stack = void 0;
  128. try {
  129. throw new Error();
  130. } catch (e) {
  131. ;
  132. ({ stack } = e);
  133. }
  134. console.warn(
  135. "Selector " + (selector.name || "unknown") + " returned the root state when called. This can lead to unnecessary rerenders.\nSelectors that return the entire state are almost certainly a mistake, as they will cause a rerender whenever *anything* in state changes.",
  136. { stack }
  137. );
  138. }
  139. }
  140. if (firstRun.current)
  141. firstRun.current = false;
  142. }
  143. return selected;
  144. }
  145. }[selector.name],
  146. [selector, stabilityCheck, devModeChecks.stabilityCheck]
  147. );
  148. const selectedState = useSyncExternalStoreWithSelector(
  149. subscription.addNestedSub,
  150. store.getState,
  151. getServerState || store.getState,
  152. wrappedSelector,
  153. equalityFn
  154. );
  155. React.useDebugValue(selectedState);
  156. return selectedState;
  157. };
  158. }
  159. var useSelector = /* @__PURE__ */ createSelectorHook();
  160. // src/utils/react-is.ts
  161. var REACT_ELEMENT_TYPE = Symbol.for("react.element");
  162. var REACT_PORTAL_TYPE = Symbol.for("react.portal");
  163. var REACT_FRAGMENT_TYPE = Symbol.for("react.fragment");
  164. var REACT_STRICT_MODE_TYPE = Symbol.for("react.strict_mode");
  165. var REACT_PROFILER_TYPE = Symbol.for("react.profiler");
  166. var REACT_PROVIDER_TYPE = Symbol.for("react.provider");
  167. var REACT_CONTEXT_TYPE = Symbol.for("react.context");
  168. var REACT_SERVER_CONTEXT_TYPE = Symbol.for("react.server_context");
  169. var REACT_FORWARD_REF_TYPE = Symbol.for("react.forward_ref");
  170. var REACT_SUSPENSE_TYPE = Symbol.for("react.suspense");
  171. var REACT_SUSPENSE_LIST_TYPE = Symbol.for("react.suspense_list");
  172. var REACT_MEMO_TYPE = Symbol.for("react.memo");
  173. var REACT_LAZY_TYPE = Symbol.for("react.lazy");
  174. var REACT_OFFSCREEN_TYPE = Symbol.for("react.offscreen");
  175. var REACT_CLIENT_REFERENCE = Symbol.for("react.client.reference");
  176. var ForwardRef = REACT_FORWARD_REF_TYPE;
  177. var Memo = REACT_MEMO_TYPE;
  178. function isValidElementType(type) {
  179. if (typeof type === "string" || typeof type === "function") {
  180. return true;
  181. }
  182. if (type === REACT_FRAGMENT_TYPE || type === REACT_PROFILER_TYPE || type === REACT_STRICT_MODE_TYPE || type === REACT_SUSPENSE_TYPE || type === REACT_SUSPENSE_LIST_TYPE || type === REACT_OFFSCREEN_TYPE) {
  183. return true;
  184. }
  185. if (typeof type === "object" && type !== null) {
  186. if (type.$$typeof === REACT_LAZY_TYPE || type.$$typeof === REACT_MEMO_TYPE || type.$$typeof === REACT_PROVIDER_TYPE || type.$$typeof === REACT_CONTEXT_TYPE || type.$$typeof === REACT_FORWARD_REF_TYPE || // This needs to include all possible module reference object
  187. // types supported by any Flight configuration anywhere since
  188. // we don't know which Flight build this will end up being used
  189. // with.
  190. type.$$typeof === REACT_CLIENT_REFERENCE || type.getModuleId !== void 0) {
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. function typeOf(object) {
  197. if (typeof object === "object" && object !== null) {
  198. const $$typeof = object.$$typeof;
  199. switch ($$typeof) {
  200. case REACT_ELEMENT_TYPE: {
  201. const type = object.type;
  202. switch (type) {
  203. case REACT_FRAGMENT_TYPE:
  204. case REACT_PROFILER_TYPE:
  205. case REACT_STRICT_MODE_TYPE:
  206. case REACT_SUSPENSE_TYPE:
  207. case REACT_SUSPENSE_LIST_TYPE:
  208. return type;
  209. default: {
  210. const $$typeofType = type && type.$$typeof;
  211. switch ($$typeofType) {
  212. case REACT_SERVER_CONTEXT_TYPE:
  213. case REACT_CONTEXT_TYPE:
  214. case REACT_FORWARD_REF_TYPE:
  215. case REACT_LAZY_TYPE:
  216. case REACT_MEMO_TYPE:
  217. case REACT_PROVIDER_TYPE:
  218. return $$typeofType;
  219. default:
  220. return $$typeof;
  221. }
  222. }
  223. }
  224. }
  225. case REACT_PORTAL_TYPE: {
  226. return $$typeof;
  227. }
  228. }
  229. }
  230. return void 0;
  231. }
  232. function isContextConsumer(object) {
  233. return typeOf(object) === REACT_CONTEXT_TYPE;
  234. }
  235. function isMemo(object) {
  236. return typeOf(object) === REACT_MEMO_TYPE;
  237. }
  238. // src/utils/warning.ts
  239. function warning(message) {
  240. if (typeof console !== "undefined" && typeof console.error === "function") {
  241. console.error(message);
  242. }
  243. try {
  244. throw new Error(message);
  245. } catch (e) {
  246. }
  247. }
  248. // src/connect/verifySubselectors.ts
  249. function verify(selector, methodName) {
  250. if (!selector) {
  251. throw new Error(`Unexpected value for ${methodName} in connect.`);
  252. } else if (methodName === "mapStateToProps" || methodName === "mapDispatchToProps") {
  253. if (!Object.prototype.hasOwnProperty.call(selector, "dependsOnOwnProps")) {
  254. warning(
  255. `The selector for ${methodName} of connect did not specify a value for dependsOnOwnProps.`
  256. );
  257. }
  258. }
  259. }
  260. function verifySubselectors(mapStateToProps, mapDispatchToProps, mergeProps) {
  261. verify(mapStateToProps, "mapStateToProps");
  262. verify(mapDispatchToProps, "mapDispatchToProps");
  263. verify(mergeProps, "mergeProps");
  264. }
  265. // src/connect/selectorFactory.ts
  266. function pureFinalPropsSelectorFactory(mapStateToProps, mapDispatchToProps, mergeProps, dispatch, {
  267. areStatesEqual,
  268. areOwnPropsEqual,
  269. areStatePropsEqual
  270. }) {
  271. let hasRunAtLeastOnce = false;
  272. let state;
  273. let ownProps;
  274. let stateProps;
  275. let dispatchProps;
  276. let mergedProps;
  277. function handleFirstCall(firstState, firstOwnProps) {
  278. state = firstState;
  279. ownProps = firstOwnProps;
  280. stateProps = mapStateToProps(state, ownProps);
  281. dispatchProps = mapDispatchToProps(dispatch, ownProps);
  282. mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
  283. hasRunAtLeastOnce = true;
  284. return mergedProps;
  285. }
  286. function handleNewPropsAndNewState() {
  287. stateProps = mapStateToProps(state, ownProps);
  288. if (mapDispatchToProps.dependsOnOwnProps)
  289. dispatchProps = mapDispatchToProps(dispatch, ownProps);
  290. mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
  291. return mergedProps;
  292. }
  293. function handleNewProps() {
  294. if (mapStateToProps.dependsOnOwnProps)
  295. stateProps = mapStateToProps(state, ownProps);
  296. if (mapDispatchToProps.dependsOnOwnProps)
  297. dispatchProps = mapDispatchToProps(dispatch, ownProps);
  298. mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
  299. return mergedProps;
  300. }
  301. function handleNewState() {
  302. const nextStateProps = mapStateToProps(state, ownProps);
  303. const statePropsChanged = !areStatePropsEqual(nextStateProps, stateProps);
  304. stateProps = nextStateProps;
  305. if (statePropsChanged)
  306. mergedProps = mergeProps(stateProps, dispatchProps, ownProps);
  307. return mergedProps;
  308. }
  309. function handleSubsequentCalls(nextState, nextOwnProps) {
  310. const propsChanged = !areOwnPropsEqual(nextOwnProps, ownProps);
  311. const stateChanged = !areStatesEqual(
  312. nextState,
  313. state,
  314. nextOwnProps,
  315. ownProps
  316. );
  317. state = nextState;
  318. ownProps = nextOwnProps;
  319. if (propsChanged && stateChanged)
  320. return handleNewPropsAndNewState();
  321. if (propsChanged)
  322. return handleNewProps();
  323. if (stateChanged)
  324. return handleNewState();
  325. return mergedProps;
  326. }
  327. return function pureFinalPropsSelector(nextState, nextOwnProps) {
  328. return hasRunAtLeastOnce ? handleSubsequentCalls(nextState, nextOwnProps) : handleFirstCall(nextState, nextOwnProps);
  329. };
  330. }
  331. function finalPropsSelectorFactory(dispatch, {
  332. initMapStateToProps,
  333. initMapDispatchToProps,
  334. initMergeProps,
  335. ...options
  336. }) {
  337. const mapStateToProps = initMapStateToProps(dispatch, options);
  338. const mapDispatchToProps = initMapDispatchToProps(dispatch, options);
  339. const mergeProps = initMergeProps(dispatch, options);
  340. if (process.env.NODE_ENV !== "production") {
  341. verifySubselectors(mapStateToProps, mapDispatchToProps, mergeProps);
  342. }
  343. return pureFinalPropsSelectorFactory(mapStateToProps, mapDispatchToProps, mergeProps, dispatch, options);
  344. }
  345. // src/utils/bindActionCreators.ts
  346. function bindActionCreators(actionCreators, dispatch) {
  347. const boundActionCreators = {};
  348. for (const key in actionCreators) {
  349. const actionCreator = actionCreators[key];
  350. if (typeof actionCreator === "function") {
  351. boundActionCreators[key] = (...args) => dispatch(actionCreator(...args));
  352. }
  353. }
  354. return boundActionCreators;
  355. }
  356. // src/utils/isPlainObject.ts
  357. function isPlainObject(obj) {
  358. if (typeof obj !== "object" || obj === null)
  359. return false;
  360. let proto = Object.getPrototypeOf(obj);
  361. if (proto === null)
  362. return true;
  363. let baseProto = proto;
  364. while (Object.getPrototypeOf(baseProto) !== null) {
  365. baseProto = Object.getPrototypeOf(baseProto);
  366. }
  367. return proto === baseProto;
  368. }
  369. // src/utils/verifyPlainObject.ts
  370. function verifyPlainObject(value, displayName, methodName) {
  371. if (!isPlainObject(value)) {
  372. warning(
  373. `${methodName}() in ${displayName} must return a plain object. Instead received ${value}.`
  374. );
  375. }
  376. }
  377. // src/connect/wrapMapToProps.ts
  378. function wrapMapToPropsConstant(getConstant) {
  379. return function initConstantSelector(dispatch) {
  380. const constant = getConstant(dispatch);
  381. function constantSelector() {
  382. return constant;
  383. }
  384. constantSelector.dependsOnOwnProps = false;
  385. return constantSelector;
  386. };
  387. }
  388. function getDependsOnOwnProps(mapToProps) {
  389. return mapToProps.dependsOnOwnProps ? Boolean(mapToProps.dependsOnOwnProps) : mapToProps.length !== 1;
  390. }
  391. function wrapMapToPropsFunc(mapToProps, methodName) {
  392. return function initProxySelector(dispatch, { displayName }) {
  393. const proxy = function mapToPropsProxy(stateOrDispatch, ownProps) {
  394. return proxy.dependsOnOwnProps ? proxy.mapToProps(stateOrDispatch, ownProps) : proxy.mapToProps(stateOrDispatch, void 0);
  395. };
  396. proxy.dependsOnOwnProps = true;
  397. proxy.mapToProps = function detectFactoryAndVerify(stateOrDispatch, ownProps) {
  398. proxy.mapToProps = mapToProps;
  399. proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps);
  400. let props = proxy(stateOrDispatch, ownProps);
  401. if (typeof props === "function") {
  402. proxy.mapToProps = props;
  403. proxy.dependsOnOwnProps = getDependsOnOwnProps(props);
  404. props = proxy(stateOrDispatch, ownProps);
  405. }
  406. if (process.env.NODE_ENV !== "production")
  407. verifyPlainObject(props, displayName, methodName);
  408. return props;
  409. };
  410. return proxy;
  411. };
  412. }
  413. // src/connect/invalidArgFactory.ts
  414. function createInvalidArgFactory(arg, name) {
  415. return (dispatch, options) => {
  416. throw new Error(
  417. `Invalid value of type ${typeof arg} for ${name} argument when connecting component ${options.wrappedComponentName}.`
  418. );
  419. };
  420. }
  421. // src/connect/mapDispatchToProps.ts
  422. function mapDispatchToPropsFactory(mapDispatchToProps) {
  423. return mapDispatchToProps && typeof mapDispatchToProps === "object" ? wrapMapToPropsConstant(
  424. (dispatch) => (
  425. // @ts-ignore
  426. bindActionCreators(mapDispatchToProps, dispatch)
  427. )
  428. ) : !mapDispatchToProps ? wrapMapToPropsConstant((dispatch) => ({
  429. dispatch
  430. })) : typeof mapDispatchToProps === "function" ? (
  431. // @ts-ignore
  432. wrapMapToPropsFunc(mapDispatchToProps, "mapDispatchToProps")
  433. ) : createInvalidArgFactory(mapDispatchToProps, "mapDispatchToProps");
  434. }
  435. // src/connect/mapStateToProps.ts
  436. function mapStateToPropsFactory(mapStateToProps) {
  437. return !mapStateToProps ? wrapMapToPropsConstant(() => ({})) : typeof mapStateToProps === "function" ? (
  438. // @ts-ignore
  439. wrapMapToPropsFunc(mapStateToProps, "mapStateToProps")
  440. ) : createInvalidArgFactory(mapStateToProps, "mapStateToProps");
  441. }
  442. // src/connect/mergeProps.ts
  443. function defaultMergeProps(stateProps, dispatchProps, ownProps) {
  444. return { ...ownProps, ...stateProps, ...dispatchProps };
  445. }
  446. function wrapMergePropsFunc(mergeProps) {
  447. return function initMergePropsProxy(dispatch, { displayName, areMergedPropsEqual }) {
  448. let hasRunOnce = false;
  449. let mergedProps;
  450. return function mergePropsProxy(stateProps, dispatchProps, ownProps) {
  451. const nextMergedProps = mergeProps(stateProps, dispatchProps, ownProps);
  452. if (hasRunOnce) {
  453. if (!areMergedPropsEqual(nextMergedProps, mergedProps))
  454. mergedProps = nextMergedProps;
  455. } else {
  456. hasRunOnce = true;
  457. mergedProps = nextMergedProps;
  458. if (process.env.NODE_ENV !== "production")
  459. verifyPlainObject(mergedProps, displayName, "mergeProps");
  460. }
  461. return mergedProps;
  462. };
  463. };
  464. }
  465. function mergePropsFactory(mergeProps) {
  466. return !mergeProps ? () => defaultMergeProps : typeof mergeProps === "function" ? wrapMergePropsFunc(mergeProps) : createInvalidArgFactory(mergeProps, "mergeProps");
  467. }
  468. // src/utils/Subscription.ts
  469. function createListenerCollection() {
  470. const batch3 = getBatch();
  471. let first = null;
  472. let last = null;
  473. return {
  474. clear() {
  475. first = null;
  476. last = null;
  477. },
  478. notify() {
  479. batch3(() => {
  480. let listener = first;
  481. while (listener) {
  482. listener.callback();
  483. listener = listener.next;
  484. }
  485. });
  486. },
  487. get() {
  488. let listeners = [];
  489. let listener = first;
  490. while (listener) {
  491. listeners.push(listener);
  492. listener = listener.next;
  493. }
  494. return listeners;
  495. },
  496. subscribe(callback) {
  497. let isSubscribed = true;
  498. let listener = last = {
  499. callback,
  500. next: null,
  501. prev: last
  502. };
  503. if (listener.prev) {
  504. listener.prev.next = listener;
  505. } else {
  506. first = listener;
  507. }
  508. return function unsubscribe() {
  509. if (!isSubscribed || first === null)
  510. return;
  511. isSubscribed = false;
  512. if (listener.next) {
  513. listener.next.prev = listener.prev;
  514. } else {
  515. last = listener.prev;
  516. }
  517. if (listener.prev) {
  518. listener.prev.next = listener.next;
  519. } else {
  520. first = listener.next;
  521. }
  522. };
  523. }
  524. };
  525. }
  526. var nullListeners = {
  527. notify() {
  528. },
  529. get: () => []
  530. };
  531. function createSubscription(store, parentSub) {
  532. let unsubscribe;
  533. let listeners = nullListeners;
  534. let subscriptionsAmount = 0;
  535. let selfSubscribed = false;
  536. function addNestedSub(listener) {
  537. trySubscribe();
  538. const cleanupListener = listeners.subscribe(listener);
  539. let removed = false;
  540. return () => {
  541. if (!removed) {
  542. removed = true;
  543. cleanupListener();
  544. tryUnsubscribe();
  545. }
  546. };
  547. }
  548. function notifyNestedSubs() {
  549. listeners.notify();
  550. }
  551. function handleChangeWrapper() {
  552. if (subscription.onStateChange) {
  553. subscription.onStateChange();
  554. }
  555. }
  556. function isSubscribed() {
  557. return selfSubscribed;
  558. }
  559. function trySubscribe() {
  560. subscriptionsAmount++;
  561. if (!unsubscribe) {
  562. unsubscribe = parentSub ? parentSub.addNestedSub(handleChangeWrapper) : store.subscribe(handleChangeWrapper);
  563. listeners = createListenerCollection();
  564. }
  565. }
  566. function tryUnsubscribe() {
  567. subscriptionsAmount--;
  568. if (unsubscribe && subscriptionsAmount === 0) {
  569. unsubscribe();
  570. unsubscribe = void 0;
  571. listeners.clear();
  572. listeners = nullListeners;
  573. }
  574. }
  575. function trySubscribeSelf() {
  576. if (!selfSubscribed) {
  577. selfSubscribed = true;
  578. trySubscribe();
  579. }
  580. }
  581. function tryUnsubscribeSelf() {
  582. if (selfSubscribed) {
  583. selfSubscribed = false;
  584. tryUnsubscribe();
  585. }
  586. }
  587. const subscription = {
  588. addNestedSub,
  589. notifyNestedSubs,
  590. handleChangeWrapper,
  591. isSubscribed,
  592. trySubscribe: trySubscribeSelf,
  593. tryUnsubscribe: tryUnsubscribeSelf,
  594. getListeners: () => listeners
  595. };
  596. return subscription;
  597. }
  598. // src/utils/useIsomorphicLayoutEffect.ts
  599. var canUseDOM = !!(typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined");
  600. var useIsomorphicLayoutEffect = canUseDOM ? React.useLayoutEffect : React.useEffect;
  601. // src/utils/shallowEqual.ts
  602. function is(x, y) {
  603. if (x === y) {
  604. return x !== 0 || y !== 0 || 1 / x === 1 / y;
  605. } else {
  606. return x !== x && y !== y;
  607. }
  608. }
  609. function shallowEqual(objA, objB) {
  610. if (is(objA, objB))
  611. return true;
  612. if (typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null) {
  613. return false;
  614. }
  615. const keysA = Object.keys(objA);
  616. const keysB = Object.keys(objB);
  617. if (keysA.length !== keysB.length)
  618. return false;
  619. for (let i = 0; i < keysA.length; i++) {
  620. if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
  621. return false;
  622. }
  623. }
  624. return true;
  625. }
  626. // src/utils/hoistStatics.ts
  627. var REACT_STATICS = {
  628. childContextTypes: true,
  629. contextType: true,
  630. contextTypes: true,
  631. defaultProps: true,
  632. displayName: true,
  633. getDefaultProps: true,
  634. getDerivedStateFromError: true,
  635. getDerivedStateFromProps: true,
  636. mixins: true,
  637. propTypes: true,
  638. type: true
  639. };
  640. var KNOWN_STATICS = {
  641. name: true,
  642. length: true,
  643. prototype: true,
  644. caller: true,
  645. callee: true,
  646. arguments: true,
  647. arity: true
  648. };
  649. var FORWARD_REF_STATICS = {
  650. $$typeof: true,
  651. render: true,
  652. defaultProps: true,
  653. displayName: true,
  654. propTypes: true
  655. };
  656. var MEMO_STATICS = {
  657. $$typeof: true,
  658. compare: true,
  659. defaultProps: true,
  660. displayName: true,
  661. propTypes: true,
  662. type: true
  663. };
  664. var TYPE_STATICS = {
  665. [ForwardRef]: FORWARD_REF_STATICS,
  666. [Memo]: MEMO_STATICS
  667. };
  668. function getStatics(component) {
  669. if (isMemo(component)) {
  670. return MEMO_STATICS;
  671. }
  672. return TYPE_STATICS[component["$$typeof"]] || REACT_STATICS;
  673. }
  674. var defineProperty = Object.defineProperty;
  675. var getOwnPropertyNames = Object.getOwnPropertyNames;
  676. var getOwnPropertySymbols = Object.getOwnPropertySymbols;
  677. var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
  678. var getPrototypeOf = Object.getPrototypeOf;
  679. var objectPrototype = Object.prototype;
  680. function hoistNonReactStatics(targetComponent, sourceComponent) {
  681. if (typeof sourceComponent !== "string") {
  682. if (objectPrototype) {
  683. const inheritedComponent = getPrototypeOf(sourceComponent);
  684. if (inheritedComponent && inheritedComponent !== objectPrototype) {
  685. hoistNonReactStatics(targetComponent, inheritedComponent);
  686. }
  687. }
  688. let keys = getOwnPropertyNames(sourceComponent);
  689. if (getOwnPropertySymbols) {
  690. keys = keys.concat(getOwnPropertySymbols(sourceComponent));
  691. }
  692. const targetStatics = getStatics(targetComponent);
  693. const sourceStatics = getStatics(sourceComponent);
  694. for (let i = 0; i < keys.length; ++i) {
  695. const key = keys[i];
  696. if (!KNOWN_STATICS[key] && !(sourceStatics && sourceStatics[key]) && !(targetStatics && targetStatics[key])) {
  697. const descriptor = getOwnPropertyDescriptor(sourceComponent, key);
  698. try {
  699. defineProperty(targetComponent, key, descriptor);
  700. } catch (e) {
  701. }
  702. }
  703. }
  704. }
  705. return targetComponent;
  706. }
  707. // src/components/connect.tsx
  708. var useSyncExternalStore = notInitialized;
  709. var initializeConnect = (fn) => {
  710. useSyncExternalStore = fn;
  711. };
  712. var NO_SUBSCRIPTION_ARRAY = [null, null];
  713. var stringifyComponent = (Comp) => {
  714. try {
  715. return JSON.stringify(Comp);
  716. } catch (err) {
  717. return String(Comp);
  718. }
  719. };
  720. function useIsomorphicLayoutEffectWithArgs(effectFunc, effectArgs, dependencies) {
  721. useIsomorphicLayoutEffect(() => effectFunc(...effectArgs), dependencies);
  722. }
  723. function captureWrapperProps(lastWrapperProps, lastChildProps, renderIsScheduled, wrapperProps, childPropsFromStoreUpdate, notifyNestedSubs) {
  724. lastWrapperProps.current = wrapperProps;
  725. renderIsScheduled.current = false;
  726. if (childPropsFromStoreUpdate.current) {
  727. childPropsFromStoreUpdate.current = null;
  728. notifyNestedSubs();
  729. }
  730. }
  731. function subscribeUpdates(shouldHandleStateChanges, store, subscription, childPropsSelector, lastWrapperProps, lastChildProps, renderIsScheduled, isMounted, childPropsFromStoreUpdate, notifyNestedSubs, additionalSubscribeListener) {
  732. if (!shouldHandleStateChanges)
  733. return () => {
  734. };
  735. let didUnsubscribe = false;
  736. let lastThrownError = null;
  737. const checkForUpdates = () => {
  738. if (didUnsubscribe || !isMounted.current) {
  739. return;
  740. }
  741. const latestStoreState = store.getState();
  742. let newChildProps, error;
  743. try {
  744. newChildProps = childPropsSelector(
  745. latestStoreState,
  746. lastWrapperProps.current
  747. );
  748. } catch (e) {
  749. error = e;
  750. lastThrownError = e;
  751. }
  752. if (!error) {
  753. lastThrownError = null;
  754. }
  755. if (newChildProps === lastChildProps.current) {
  756. if (!renderIsScheduled.current) {
  757. notifyNestedSubs();
  758. }
  759. } else {
  760. lastChildProps.current = newChildProps;
  761. childPropsFromStoreUpdate.current = newChildProps;
  762. renderIsScheduled.current = true;
  763. additionalSubscribeListener();
  764. }
  765. };
  766. subscription.onStateChange = checkForUpdates;
  767. subscription.trySubscribe();
  768. checkForUpdates();
  769. const unsubscribeWrapper = () => {
  770. didUnsubscribe = true;
  771. subscription.tryUnsubscribe();
  772. subscription.onStateChange = null;
  773. if (lastThrownError) {
  774. throw lastThrownError;
  775. }
  776. };
  777. return unsubscribeWrapper;
  778. }
  779. function strictEqual(a, b) {
  780. return a === b;
  781. }
  782. var hasWarnedAboutDeprecatedPureOption = false;
  783. function connect(mapStateToProps, mapDispatchToProps, mergeProps, {
  784. // The `pure` option has been removed, so TS doesn't like us destructuring this to check its existence.
  785. // @ts-ignore
  786. pure,
  787. areStatesEqual = strictEqual,
  788. areOwnPropsEqual = shallowEqual,
  789. areStatePropsEqual = shallowEqual,
  790. areMergedPropsEqual = shallowEqual,
  791. // use React's forwardRef to expose a ref of the wrapped component
  792. forwardRef = false,
  793. // the context consumer to use
  794. context = ReactReduxContext
  795. } = {}) {
  796. if (process.env.NODE_ENV !== "production") {
  797. if (pure !== void 0 && !hasWarnedAboutDeprecatedPureOption) {
  798. hasWarnedAboutDeprecatedPureOption = true;
  799. warning(
  800. 'The `pure` option has been removed. `connect` is now always a "pure/memoized" component'
  801. );
  802. }
  803. }
  804. const Context = context;
  805. const initMapStateToProps = mapStateToPropsFactory(mapStateToProps);
  806. const initMapDispatchToProps = mapDispatchToPropsFactory(mapDispatchToProps);
  807. const initMergeProps = mergePropsFactory(mergeProps);
  808. const shouldHandleStateChanges = Boolean(mapStateToProps);
  809. const wrapWithConnect = (WrappedComponent) => {
  810. if (process.env.NODE_ENV !== "production") {
  811. const isValid = /* @__PURE__ */ isValidElementType(WrappedComponent);
  812. if (!isValid)
  813. throw new Error(
  814. `You must pass a component to the function returned by connect. Instead received ${stringifyComponent(
  815. WrappedComponent
  816. )}`
  817. );
  818. }
  819. const wrappedComponentName = WrappedComponent.displayName || WrappedComponent.name || "Component";
  820. const displayName = `Connect(${wrappedComponentName})`;
  821. const selectorFactoryOptions = {
  822. shouldHandleStateChanges,
  823. displayName,
  824. wrappedComponentName,
  825. WrappedComponent,
  826. // @ts-ignore
  827. initMapStateToProps,
  828. // @ts-ignore
  829. initMapDispatchToProps,
  830. initMergeProps,
  831. areStatesEqual,
  832. areStatePropsEqual,
  833. areOwnPropsEqual,
  834. areMergedPropsEqual
  835. };
  836. function ConnectFunction(props) {
  837. const [propsContext, reactReduxForwardedRef, wrapperProps] = React.useMemo(() => {
  838. const { reactReduxForwardedRef: reactReduxForwardedRef2, ...wrapperProps2 } = props;
  839. return [props.context, reactReduxForwardedRef2, wrapperProps2];
  840. }, [props]);
  841. const ContextToUse = React.useMemo(() => {
  842. let ResultContext = Context;
  843. if (propsContext?.Consumer) {
  844. if (process.env.NODE_ENV !== "production") {
  845. const isValid = /* @__PURE__ */ isContextConsumer(
  846. // @ts-ignore
  847. /* @__PURE__ */ React.createElement(propsContext.Consumer, null)
  848. );
  849. if (!isValid) {
  850. throw new Error(
  851. "You must pass a valid React context consumer as `props.context`"
  852. );
  853. }
  854. ResultContext = propsContext;
  855. }
  856. }
  857. return ResultContext;
  858. }, [propsContext, Context]);
  859. const contextValue = React.useContext(ContextToUse);
  860. const didStoreComeFromProps = Boolean(props.store) && Boolean(props.store.getState) && Boolean(props.store.dispatch);
  861. const didStoreComeFromContext = Boolean(contextValue) && Boolean(contextValue.store);
  862. if (process.env.NODE_ENV !== "production" && !didStoreComeFromProps && !didStoreComeFromContext) {
  863. throw new Error(
  864. `Could not find "store" in the context of "${displayName}". Either wrap the root component in a <Provider>, or pass a custom React context provider to <Provider> and the corresponding React context consumer to ${displayName} in connect options.`
  865. );
  866. }
  867. const store = didStoreComeFromProps ? props.store : contextValue.store;
  868. const getServerState = didStoreComeFromContext ? contextValue.getServerState : store.getState;
  869. const childPropsSelector = React.useMemo(() => {
  870. return finalPropsSelectorFactory(store.dispatch, selectorFactoryOptions);
  871. }, [store]);
  872. const [subscription, notifyNestedSubs] = React.useMemo(() => {
  873. if (!shouldHandleStateChanges)
  874. return NO_SUBSCRIPTION_ARRAY;
  875. const subscription2 = createSubscription(
  876. store,
  877. didStoreComeFromProps ? void 0 : contextValue.subscription
  878. );
  879. const notifyNestedSubs2 = subscription2.notifyNestedSubs.bind(subscription2);
  880. return [subscription2, notifyNestedSubs2];
  881. }, [store, didStoreComeFromProps, contextValue]);
  882. const overriddenContextValue = React.useMemo(() => {
  883. if (didStoreComeFromProps) {
  884. return contextValue;
  885. }
  886. return {
  887. ...contextValue,
  888. subscription
  889. };
  890. }, [didStoreComeFromProps, contextValue, subscription]);
  891. const lastChildProps = React.useRef();
  892. const lastWrapperProps = React.useRef(wrapperProps);
  893. const childPropsFromStoreUpdate = React.useRef();
  894. const renderIsScheduled = React.useRef(false);
  895. const isProcessingDispatch = React.useRef(false);
  896. const isMounted = React.useRef(false);
  897. const latestSubscriptionCallbackError = React.useRef();
  898. useIsomorphicLayoutEffect(() => {
  899. isMounted.current = true;
  900. return () => {
  901. isMounted.current = false;
  902. };
  903. }, []);
  904. const actualChildPropsSelector = React.useMemo(() => {
  905. const selector = () => {
  906. if (childPropsFromStoreUpdate.current && wrapperProps === lastWrapperProps.current) {
  907. return childPropsFromStoreUpdate.current;
  908. }
  909. return childPropsSelector(store.getState(), wrapperProps);
  910. };
  911. return selector;
  912. }, [store, wrapperProps]);
  913. const subscribeForReact = React.useMemo(() => {
  914. const subscribe = (reactListener) => {
  915. if (!subscription) {
  916. return () => {
  917. };
  918. }
  919. return subscribeUpdates(
  920. shouldHandleStateChanges,
  921. store,
  922. subscription,
  923. // @ts-ignore
  924. childPropsSelector,
  925. lastWrapperProps,
  926. lastChildProps,
  927. renderIsScheduled,
  928. isMounted,
  929. childPropsFromStoreUpdate,
  930. notifyNestedSubs,
  931. reactListener
  932. );
  933. };
  934. return subscribe;
  935. }, [subscription]);
  936. useIsomorphicLayoutEffectWithArgs(captureWrapperProps, [
  937. lastWrapperProps,
  938. lastChildProps,
  939. renderIsScheduled,
  940. wrapperProps,
  941. childPropsFromStoreUpdate,
  942. notifyNestedSubs
  943. ]);
  944. let actualChildProps;
  945. try {
  946. actualChildProps = useSyncExternalStore(
  947. // TODO We're passing through a big wrapper that does a bunch of extra side effects besides subscribing
  948. subscribeForReact,
  949. // TODO This is incredibly hacky. We've already processed the store update and calculated new child props,
  950. // TODO and we're just passing that through so it triggers a re-render for us rather than relying on `uSES`.
  951. actualChildPropsSelector,
  952. getServerState ? () => childPropsSelector(getServerState(), wrapperProps) : actualChildPropsSelector
  953. );
  954. } catch (err) {
  955. if (latestSubscriptionCallbackError.current) {
  956. ;
  957. err.message += `
  958. The error may be correlated with this previous error:
  959. ${latestSubscriptionCallbackError.current.stack}
  960. `;
  961. }
  962. throw err;
  963. }
  964. useIsomorphicLayoutEffect(() => {
  965. latestSubscriptionCallbackError.current = void 0;
  966. childPropsFromStoreUpdate.current = void 0;
  967. lastChildProps.current = actualChildProps;
  968. });
  969. const renderedWrappedComponent = React.useMemo(() => {
  970. return (
  971. // @ts-ignore
  972. /* @__PURE__ */ React.createElement(
  973. WrappedComponent,
  974. {
  975. ...actualChildProps,
  976. ref: reactReduxForwardedRef
  977. }
  978. )
  979. );
  980. }, [reactReduxForwardedRef, WrappedComponent, actualChildProps]);
  981. const renderedChild = React.useMemo(() => {
  982. if (shouldHandleStateChanges) {
  983. return /* @__PURE__ */ React.createElement(ContextToUse.Provider, { value: overriddenContextValue }, renderedWrappedComponent);
  984. }
  985. return renderedWrappedComponent;
  986. }, [ContextToUse, renderedWrappedComponent, overriddenContextValue]);
  987. return renderedChild;
  988. }
  989. const _Connect = React.memo(ConnectFunction);
  990. const Connect = _Connect;
  991. Connect.WrappedComponent = WrappedComponent;
  992. Connect.displayName = ConnectFunction.displayName = displayName;
  993. if (forwardRef) {
  994. const _forwarded = React.forwardRef(function forwardConnectRef(props, ref) {
  995. return /* @__PURE__ */ React.createElement(Connect, { ...props, reactReduxForwardedRef: ref });
  996. });
  997. const forwarded = _forwarded;
  998. forwarded.displayName = displayName;
  999. forwarded.WrappedComponent = WrappedComponent;
  1000. return /* @__PURE__ */ hoistNonReactStatics(forwarded, WrappedComponent);
  1001. }
  1002. return /* @__PURE__ */ hoistNonReactStatics(Connect, WrappedComponent);
  1003. };
  1004. return wrapWithConnect;
  1005. }
  1006. var connect_default = connect;
  1007. // src/components/Provider.tsx
  1008. function Provider({
  1009. store,
  1010. context,
  1011. children,
  1012. serverState,
  1013. stabilityCheck = "once",
  1014. identityFunctionCheck = "once"
  1015. }) {
  1016. const contextValue = React.useMemo(() => {
  1017. const subscription = createSubscription(store);
  1018. return {
  1019. store,
  1020. subscription,
  1021. getServerState: serverState ? () => serverState : void 0,
  1022. stabilityCheck,
  1023. identityFunctionCheck
  1024. };
  1025. }, [store, serverState, stabilityCheck, identityFunctionCheck]);
  1026. const previousState = React.useMemo(() => store.getState(), [store]);
  1027. useIsomorphicLayoutEffect(() => {
  1028. const { subscription } = contextValue;
  1029. subscription.onStateChange = subscription.notifyNestedSubs;
  1030. subscription.trySubscribe();
  1031. if (previousState !== store.getState()) {
  1032. subscription.notifyNestedSubs();
  1033. }
  1034. return () => {
  1035. subscription.tryUnsubscribe();
  1036. subscription.onStateChange = void 0;
  1037. };
  1038. }, [contextValue, previousState]);
  1039. const Context = context || ReactReduxContext;
  1040. return /* @__PURE__ */ React.createElement(Context.Provider, { value: contextValue }, children);
  1041. }
  1042. var Provider_default = Provider;
  1043. // src/hooks/useStore.ts
  1044. function createStoreHook(context = ReactReduxContext) {
  1045. const useReduxContext2 = (
  1046. // @ts-ignore
  1047. context === ReactReduxContext ? useReduxContext : (
  1048. // @ts-ignore
  1049. createReduxContextHook(context)
  1050. )
  1051. );
  1052. return function useStore2() {
  1053. const { store } = useReduxContext2();
  1054. return store;
  1055. };
  1056. }
  1057. var useStore = /* @__PURE__ */ createStoreHook();
  1058. // src/hooks/useDispatch.ts
  1059. function createDispatchHook(context = ReactReduxContext) {
  1060. const useStore2 = (
  1061. // @ts-ignore
  1062. context === ReactReduxContext ? useStore : createStoreHook(context)
  1063. );
  1064. return function useDispatch2() {
  1065. const store = useStore2();
  1066. return store.dispatch;
  1067. };
  1068. }
  1069. var useDispatch = /* @__PURE__ */ createDispatchHook();
  1070. // src/index.ts
  1071. initializeUseSelector(useSyncExternalStoreWithSelector2);
  1072. initializeConnect(React2.useSyncExternalStore);
  1073. setBatch(unstable_batchedUpdates);
  1074. var batch2 = unstable_batchedUpdates;
  1075. export {
  1076. Provider_default as Provider,
  1077. ReactReduxContext,
  1078. batch2 as batch,
  1079. connect_default as connect,
  1080. createDispatchHook,
  1081. createSelectorHook,
  1082. createStoreHook,
  1083. shallowEqual,
  1084. useDispatch,
  1085. useSelector,
  1086. useStore
  1087. };
  1088. //# sourceMappingURL=react-redux.mjs.map