react-redux.alternate-renderers.mjs 37 KB

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