connectAdvanced.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
  3. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];
  4. exports.__esModule = true;
  5. exports["default"] = connectAdvanced;
  6. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  7. var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
  8. var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
  9. var _react = _interopRequireWildcard(require("react"));
  10. var _reactIs = require("react-is");
  11. var _Subscription = require("../utils/Subscription");
  12. var _useIsomorphicLayoutEffect = require("../utils/useIsomorphicLayoutEffect");
  13. var _Context = require("./Context");
  14. var _excluded = ["getDisplayName", "methodName", "renderCountProp", "shouldHandleStateChanges", "storeKey", "withRef", "forwardRef", "context"],
  15. _excluded2 = ["reactReduxForwardedRef"];
  16. // Define some constant arrays just to avoid re-creating these
  17. var EMPTY_ARRAY = [];
  18. var NO_SUBSCRIPTION_ARRAY = [null, null];
  19. var stringifyComponent = function stringifyComponent(Comp) {
  20. try {
  21. return JSON.stringify(Comp);
  22. } catch (err) {
  23. return String(Comp);
  24. }
  25. };
  26. function storeStateUpdatesReducer(state, action) {
  27. var updateCount = state[1];
  28. return [action.payload, updateCount + 1];
  29. }
  30. function useIsomorphicLayoutEffectWithArgs(effectFunc, effectArgs, dependencies) {
  31. (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(function () {
  32. return effectFunc.apply(void 0, effectArgs);
  33. }, dependencies);
  34. }
  35. function captureWrapperProps(lastWrapperProps, lastChildProps, renderIsScheduled, wrapperProps, actualChildProps, childPropsFromStoreUpdate, notifyNestedSubs) {
  36. // We want to capture the wrapper props and child props we used for later comparisons
  37. lastWrapperProps.current = wrapperProps;
  38. lastChildProps.current = actualChildProps;
  39. renderIsScheduled.current = false; // If the render was from a store update, clear out that reference and cascade the subscriber update
  40. if (childPropsFromStoreUpdate.current) {
  41. childPropsFromStoreUpdate.current = null;
  42. notifyNestedSubs();
  43. }
  44. }
  45. function subscribeUpdates(shouldHandleStateChanges, store, subscription, childPropsSelector, lastWrapperProps, lastChildProps, renderIsScheduled, childPropsFromStoreUpdate, notifyNestedSubs, forceComponentUpdateDispatch) {
  46. // If we're not subscribed to the store, nothing to do here
  47. if (!shouldHandleStateChanges) return; // Capture values for checking if and when this component unmounts
  48. var didUnsubscribe = false;
  49. var lastThrownError = null; // We'll run this callback every time a store subscription update propagates to this component
  50. var checkForUpdates = function checkForUpdates() {
  51. if (didUnsubscribe) {
  52. // Don't run stale listeners.
  53. // Redux doesn't guarantee unsubscriptions happen until next dispatch.
  54. return;
  55. }
  56. var latestStoreState = store.getState();
  57. var newChildProps, error;
  58. try {
  59. // Actually run the selector with the most recent store state and wrapper props
  60. // to determine what the child props should be
  61. newChildProps = childPropsSelector(latestStoreState, lastWrapperProps.current);
  62. } catch (e) {
  63. error = e;
  64. lastThrownError = e;
  65. }
  66. if (!error) {
  67. lastThrownError = null;
  68. } // If the child props haven't changed, nothing to do here - cascade the subscription update
  69. if (newChildProps === lastChildProps.current) {
  70. if (!renderIsScheduled.current) {
  71. notifyNestedSubs();
  72. }
  73. } else {
  74. // Save references to the new child props. Note that we track the "child props from store update"
  75. // as a ref instead of a useState/useReducer because we need a way to determine if that value has
  76. // been processed. If this went into useState/useReducer, we couldn't clear out the value without
  77. // forcing another re-render, which we don't want.
  78. lastChildProps.current = newChildProps;
  79. childPropsFromStoreUpdate.current = newChildProps;
  80. renderIsScheduled.current = true; // If the child props _did_ change (or we caught an error), this wrapper component needs to re-render
  81. forceComponentUpdateDispatch({
  82. type: 'STORE_UPDATED',
  83. payload: {
  84. error: error
  85. }
  86. });
  87. }
  88. }; // Actually subscribe to the nearest connected ancestor (or store)
  89. subscription.onStateChange = checkForUpdates;
  90. subscription.trySubscribe(); // Pull data from the store after first render in case the store has
  91. // changed since we began.
  92. checkForUpdates();
  93. var unsubscribeWrapper = function unsubscribeWrapper() {
  94. didUnsubscribe = true;
  95. subscription.tryUnsubscribe();
  96. subscription.onStateChange = null;
  97. if (lastThrownError) {
  98. // It's possible that we caught an error due to a bad mapState function, but the
  99. // parent re-rendered without this component and we're about to unmount.
  100. // This shouldn't happen as long as we do top-down subscriptions correctly, but
  101. // if we ever do those wrong, this throw will surface the error in our tests.
  102. // In that case, throw the error from here so it doesn't get lost.
  103. throw lastThrownError;
  104. }
  105. };
  106. return unsubscribeWrapper;
  107. }
  108. var initStateUpdates = function initStateUpdates() {
  109. return [null, 0];
  110. };
  111. function connectAdvanced(
  112. /*
  113. selectorFactory is a func that is responsible for returning the selector function used to
  114. compute new props from state, props, and dispatch. For example:
  115. export default connectAdvanced((dispatch, options) => (state, props) => ({
  116. thing: state.things[props.thingId],
  117. saveThing: fields => dispatch(actionCreators.saveThing(props.thingId, fields)),
  118. }))(YourComponent)
  119. Access to dispatch is provided to the factory so selectorFactories can bind actionCreators
  120. outside of their selector as an optimization. Options passed to connectAdvanced are passed to
  121. the selectorFactory, along with displayName and WrappedComponent, as the second argument.
  122. Note that selectorFactory is responsible for all caching/memoization of inbound and outbound
  123. props. Do not use connectAdvanced directly without memoizing results between calls to your
  124. selector, otherwise the Connect component will re-render on every state or props change.
  125. */
  126. selectorFactory, // options object:
  127. _ref) {
  128. if (_ref === void 0) {
  129. _ref = {};
  130. }
  131. var _ref2 = _ref,
  132. _ref2$getDisplayName = _ref2.getDisplayName,
  133. getDisplayName = _ref2$getDisplayName === void 0 ? function (name) {
  134. return "ConnectAdvanced(" + name + ")";
  135. } : _ref2$getDisplayName,
  136. _ref2$methodName = _ref2.methodName,
  137. methodName = _ref2$methodName === void 0 ? 'connectAdvanced' : _ref2$methodName,
  138. _ref2$renderCountProp = _ref2.renderCountProp,
  139. renderCountProp = _ref2$renderCountProp === void 0 ? undefined : _ref2$renderCountProp,
  140. _ref2$shouldHandleSta = _ref2.shouldHandleStateChanges,
  141. shouldHandleStateChanges = _ref2$shouldHandleSta === void 0 ? true : _ref2$shouldHandleSta,
  142. _ref2$storeKey = _ref2.storeKey,
  143. storeKey = _ref2$storeKey === void 0 ? 'store' : _ref2$storeKey,
  144. _ref2$withRef = _ref2.withRef,
  145. withRef = _ref2$withRef === void 0 ? false : _ref2$withRef,
  146. _ref2$forwardRef = _ref2.forwardRef,
  147. forwardRef = _ref2$forwardRef === void 0 ? false : _ref2$forwardRef,
  148. _ref2$context = _ref2.context,
  149. context = _ref2$context === void 0 ? _Context.ReactReduxContext : _ref2$context,
  150. connectOptions = (0, _objectWithoutPropertiesLoose2["default"])(_ref2, _excluded);
  151. if (process.env.NODE_ENV !== 'production') {
  152. if (renderCountProp !== undefined) {
  153. throw new Error("renderCountProp is removed. render counting is built into the latest React Dev Tools profiling extension");
  154. }
  155. if (withRef) {
  156. throw new Error('withRef is removed. To access the wrapped instance, use a ref on the connected component');
  157. }
  158. var customStoreWarningMessage = 'To use a custom Redux store for specific components, create a custom React context with ' + "React.createContext(), and pass the context object to React Redux's Provider and specific components" + ' like: <Provider context={MyContext}><ConnectedComponent context={MyContext} /></Provider>. ' + 'You may also pass a {context : MyContext} option to connect';
  159. if (storeKey !== 'store') {
  160. throw new Error('storeKey has been removed and does not do anything. ' + customStoreWarningMessage);
  161. }
  162. }
  163. var Context = context;
  164. return function wrapWithConnect(WrappedComponent) {
  165. if (process.env.NODE_ENV !== 'production' && !(0, _reactIs.isValidElementType)(WrappedComponent)) {
  166. throw new Error("You must pass a component to the function returned by " + (methodName + ". Instead received " + stringifyComponent(WrappedComponent)));
  167. }
  168. var wrappedComponentName = WrappedComponent.displayName || WrappedComponent.name || 'Component';
  169. var displayName = getDisplayName(wrappedComponentName);
  170. var selectorFactoryOptions = (0, _extends2["default"])({}, connectOptions, {
  171. getDisplayName: getDisplayName,
  172. methodName: methodName,
  173. renderCountProp: renderCountProp,
  174. shouldHandleStateChanges: shouldHandleStateChanges,
  175. storeKey: storeKey,
  176. displayName: displayName,
  177. wrappedComponentName: wrappedComponentName,
  178. WrappedComponent: WrappedComponent
  179. });
  180. var pure = connectOptions.pure;
  181. function createChildSelector(store) {
  182. return selectorFactory(store.dispatch, selectorFactoryOptions);
  183. } // If we aren't running in "pure" mode, we don't want to memoize values.
  184. // To avoid conditionally calling hooks, we fall back to a tiny wrapper
  185. // that just executes the given callback immediately.
  186. var usePureOnlyMemo = pure ? _react.useMemo : function (callback) {
  187. return callback();
  188. };
  189. function ConnectFunction(props) {
  190. var _useMemo = (0, _react.useMemo)(function () {
  191. // Distinguish between actual "data" props that were passed to the wrapper component,
  192. // and values needed to control behavior (forwarded refs, alternate context instances).
  193. // To maintain the wrapperProps object reference, memoize this destructuring.
  194. var reactReduxForwardedRef = props.reactReduxForwardedRef,
  195. wrapperProps = (0, _objectWithoutPropertiesLoose2["default"])(props, _excluded2);
  196. return [props.context, reactReduxForwardedRef, wrapperProps];
  197. }, [props]),
  198. propsContext = _useMemo[0],
  199. reactReduxForwardedRef = _useMemo[1],
  200. wrapperProps = _useMemo[2];
  201. var ContextToUse = (0, _react.useMemo)(function () {
  202. // Users may optionally pass in a custom context instance to use instead of our ReactReduxContext.
  203. // Memoize the check that determines which context instance we should use.
  204. return propsContext && propsContext.Consumer && (0, _reactIs.isContextConsumer)( /*#__PURE__*/_react["default"].createElement(propsContext.Consumer, null)) ? propsContext : Context;
  205. }, [propsContext, Context]); // Retrieve the store and ancestor subscription via context, if available
  206. var contextValue = (0, _react.useContext)(ContextToUse); // The store _must_ exist as either a prop or in context.
  207. // We'll check to see if it _looks_ like a Redux store first.
  208. // This allows us to pass through a `store` prop that is just a plain value.
  209. var didStoreComeFromProps = Boolean(props.store) && Boolean(props.store.getState) && Boolean(props.store.dispatch);
  210. var didStoreComeFromContext = Boolean(contextValue) && Boolean(contextValue.store);
  211. if (process.env.NODE_ENV !== 'production' && !didStoreComeFromProps && !didStoreComeFromContext) {
  212. throw new Error("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."));
  213. } // Based on the previous check, one of these must be true
  214. var store = didStoreComeFromProps ? props.store : contextValue.store;
  215. var childPropsSelector = (0, _react.useMemo)(function () {
  216. // The child props selector needs the store reference as an input.
  217. // Re-create this selector whenever the store changes.
  218. return createChildSelector(store);
  219. }, [store]);
  220. var _useMemo2 = (0, _react.useMemo)(function () {
  221. if (!shouldHandleStateChanges) return NO_SUBSCRIPTION_ARRAY; // This Subscription's source should match where store came from: props vs. context. A component
  222. // connected to the store via props shouldn't use subscription from context, or vice versa.
  223. // This Subscription's source should match where store came from: props vs. context. A component
  224. // connected to the store via props shouldn't use subscription from context, or vice versa.
  225. var subscription = (0, _Subscription.createSubscription)(store, didStoreComeFromProps ? null : contextValue.subscription); // `notifyNestedSubs` is duplicated to handle the case where the component is unmounted in
  226. // the middle of the notification loop, where `subscription` will then be null. This can
  227. // probably be avoided if Subscription's listeners logic is changed to not call listeners
  228. // that have been unsubscribed in the middle of the notification loop.
  229. // `notifyNestedSubs` is duplicated to handle the case where the component is unmounted in
  230. // the middle of the notification loop, where `subscription` will then be null. This can
  231. // probably be avoided if Subscription's listeners logic is changed to not call listeners
  232. // that have been unsubscribed in the middle of the notification loop.
  233. var notifyNestedSubs = subscription.notifyNestedSubs.bind(subscription);
  234. return [subscription, notifyNestedSubs];
  235. }, [store, didStoreComeFromProps, contextValue]),
  236. subscription = _useMemo2[0],
  237. notifyNestedSubs = _useMemo2[1]; // Determine what {store, subscription} value should be put into nested context, if necessary,
  238. // and memoize that value to avoid unnecessary context updates.
  239. var overriddenContextValue = (0, _react.useMemo)(function () {
  240. if (didStoreComeFromProps) {
  241. // This component is directly subscribed to a store from props.
  242. // We don't want descendants reading from this store - pass down whatever
  243. // the existing context value is from the nearest connected ancestor.
  244. return contextValue;
  245. } // Otherwise, put this component's subscription instance into context, so that
  246. // connected descendants won't update until after this component is done
  247. return (0, _extends2["default"])({}, contextValue, {
  248. subscription: subscription
  249. });
  250. }, [didStoreComeFromProps, contextValue, subscription]); // We need to force this wrapper component to re-render whenever a Redux store update
  251. // causes a change to the calculated child component props (or we caught an error in mapState)
  252. var _useReducer = (0, _react.useReducer)(storeStateUpdatesReducer, EMPTY_ARRAY, initStateUpdates),
  253. _useReducer$ = _useReducer[0],
  254. previousStateUpdateResult = _useReducer$[0],
  255. forceComponentUpdateDispatch = _useReducer[1]; // Propagate any mapState/mapDispatch errors upwards
  256. if (previousStateUpdateResult && previousStateUpdateResult.error) {
  257. throw previousStateUpdateResult.error;
  258. } // Set up refs to coordinate values between the subscription effect and the render logic
  259. var lastChildProps = (0, _react.useRef)();
  260. var lastWrapperProps = (0, _react.useRef)(wrapperProps);
  261. var childPropsFromStoreUpdate = (0, _react.useRef)();
  262. var renderIsScheduled = (0, _react.useRef)(false);
  263. var actualChildProps = usePureOnlyMemo(function () {
  264. // Tricky logic here:
  265. // - This render may have been triggered by a Redux store update that produced new child props
  266. // - However, we may have gotten new wrapper props after that
  267. // If we have new child props, and the same wrapper props, we know we should use the new child props as-is.
  268. // But, if we have new wrapper props, those might change the child props, so we have to recalculate things.
  269. // So, we'll use the child props from store update only if the wrapper props are the same as last time.
  270. if (childPropsFromStoreUpdate.current && wrapperProps === lastWrapperProps.current) {
  271. return childPropsFromStoreUpdate.current;
  272. } // TODO We're reading the store directly in render() here. Bad idea?
  273. // This will likely cause Bad Things (TM) to happen in Concurrent Mode.
  274. // Note that we do this because on renders _not_ caused by store updates, we need the latest store state
  275. // to determine what the child props should be.
  276. return childPropsSelector(store.getState(), wrapperProps);
  277. }, [store, previousStateUpdateResult, wrapperProps]); // We need this to execute synchronously every time we re-render. However, React warns
  278. // about useLayoutEffect in SSR, so we try to detect environment and fall back to
  279. // just useEffect instead to avoid the warning, since neither will run anyway.
  280. useIsomorphicLayoutEffectWithArgs(captureWrapperProps, [lastWrapperProps, lastChildProps, renderIsScheduled, wrapperProps, actualChildProps, childPropsFromStoreUpdate, notifyNestedSubs]); // Our re-subscribe logic only runs when the store/subscription setup changes
  281. useIsomorphicLayoutEffectWithArgs(subscribeUpdates, [shouldHandleStateChanges, store, subscription, childPropsSelector, lastWrapperProps, lastChildProps, renderIsScheduled, childPropsFromStoreUpdate, notifyNestedSubs, forceComponentUpdateDispatch], [store, subscription, childPropsSelector]); // Now that all that's done, we can finally try to actually render the child component.
  282. // We memoize the elements for the rendered child component as an optimization.
  283. var renderedWrappedComponent = (0, _react.useMemo)(function () {
  284. return /*#__PURE__*/_react["default"].createElement(WrappedComponent, (0, _extends2["default"])({}, actualChildProps, {
  285. ref: reactReduxForwardedRef
  286. }));
  287. }, [reactReduxForwardedRef, WrappedComponent, actualChildProps]); // If React sees the exact same element reference as last time, it bails out of re-rendering
  288. // that child, same as if it was wrapped in React.memo() or returned false from shouldComponentUpdate.
  289. var renderedChild = (0, _react.useMemo)(function () {
  290. if (shouldHandleStateChanges) {
  291. // If this component is subscribed to store updates, we need to pass its own
  292. // subscription instance down to our descendants. That means rendering the same
  293. // Context instance, and putting a different value into the context.
  294. return /*#__PURE__*/_react["default"].createElement(ContextToUse.Provider, {
  295. value: overriddenContextValue
  296. }, renderedWrappedComponent);
  297. }
  298. return renderedWrappedComponent;
  299. }, [ContextToUse, renderedWrappedComponent, overriddenContextValue]);
  300. return renderedChild;
  301. } // If we're in "pure" mode, ensure our wrapper component only re-renders when incoming props have changed.
  302. var Connect = pure ? _react["default"].memo(ConnectFunction) : ConnectFunction;
  303. Connect.WrappedComponent = WrappedComponent;
  304. Connect.displayName = ConnectFunction.displayName = displayName;
  305. if (forwardRef) {
  306. var forwarded = _react["default"].forwardRef(function forwardConnectRef(props, ref) {
  307. return /*#__PURE__*/_react["default"].createElement(Connect, (0, _extends2["default"])({}, props, {
  308. reactReduxForwardedRef: ref
  309. }));
  310. });
  311. forwarded.displayName = displayName;
  312. forwarded.WrappedComponent = WrappedComponent;
  313. return (0, _hoistNonReactStatics["default"])(forwarded, WrappedComponent);
  314. }
  315. return (0, _hoistNonReactStatics["default"])(Connect, WrappedComponent);
  316. };
  317. }