react-redux.react-native.js 37 KB

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