react-redux.legacy-esm.js 39 KB

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