react-redux.development.cjs 38 KB

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