123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- "use strict";
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
- exports.__esModule = true;
- exports["default"] = connectAdvanced;
- var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
- var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
- var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
- var _react = _interopRequireWildcard(require("react"));
- var _reactIs = require("react-is");
- var _Subscription = _interopRequireDefault(require("../utils/Subscription"));
- var _useIsomorphicLayoutEffect = require("../utils/useIsomorphicLayoutEffect");
- var _Context = require("./Context");
- var EMPTY_ARRAY = [];
- var NO_SUBSCRIPTION_ARRAY = [null, null];
- var stringifyComponent = function stringifyComponent(Comp) {
- try {
- return JSON.stringify(Comp);
- } catch (err) {
- return String(Comp);
- }
- };
- function storeStateUpdatesReducer(state, action) {
- var updateCount = state[1];
- return [action.payload, updateCount + 1];
- }
- function useIsomorphicLayoutEffectWithArgs(effectFunc, effectArgs, dependencies) {
- (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(function () {
- return effectFunc.apply(void 0, effectArgs);
- }, dependencies);
- }
- function captureWrapperProps(lastWrapperProps, lastChildProps, renderIsScheduled, wrapperProps, actualChildProps, childPropsFromStoreUpdate, notifyNestedSubs) {
-
- lastWrapperProps.current = wrapperProps;
- lastChildProps.current = actualChildProps;
- renderIsScheduled.current = false;
- if (childPropsFromStoreUpdate.current) {
- childPropsFromStoreUpdate.current = null;
- notifyNestedSubs();
- }
- }
- function subscribeUpdates(shouldHandleStateChanges, store, subscription, childPropsSelector, lastWrapperProps, lastChildProps, renderIsScheduled, childPropsFromStoreUpdate, notifyNestedSubs, forceComponentUpdateDispatch) {
-
- if (!shouldHandleStateChanges) return;
- var didUnsubscribe = false;
- var lastThrownError = null;
- var checkForUpdates = function checkForUpdates() {
- if (didUnsubscribe) {
-
-
- return;
- }
- var latestStoreState = store.getState();
- var newChildProps, error;
- try {
-
-
- newChildProps = childPropsSelector(latestStoreState, lastWrapperProps.current);
- } catch (e) {
- error = e;
- lastThrownError = e;
- }
- if (!error) {
- lastThrownError = null;
- }
- if (newChildProps === lastChildProps.current) {
- if (!renderIsScheduled.current) {
- notifyNestedSubs();
- }
- } else {
-
-
-
-
- lastChildProps.current = newChildProps;
- childPropsFromStoreUpdate.current = newChildProps;
- renderIsScheduled.current = true;
- forceComponentUpdateDispatch({
- type: 'STORE_UPDATED',
- payload: {
- error: error
- }
- });
- }
- };
- subscription.onStateChange = checkForUpdates;
- subscription.trySubscribe();
-
- checkForUpdates();
- var unsubscribeWrapper = function unsubscribeWrapper() {
- didUnsubscribe = true;
- subscription.tryUnsubscribe();
- subscription.onStateChange = null;
- if (lastThrownError) {
-
-
-
-
-
- throw lastThrownError;
- }
- };
- return unsubscribeWrapper;
- }
- var initStateUpdates = function initStateUpdates() {
- return [null, 0];
- };
- function connectAdvanced(
- selectorFactory, // options object:
- _ref) {
- if (_ref === void 0) {
- _ref = {};
- }
- var _ref2 = _ref,
- _ref2$getDisplayName = _ref2.getDisplayName,
- getDisplayName = _ref2$getDisplayName === void 0 ? function (name) {
- return "ConnectAdvanced(" + name + ")";
- } : _ref2$getDisplayName,
- _ref2$methodName = _ref2.methodName,
- methodName = _ref2$methodName === void 0 ? 'connectAdvanced' : _ref2$methodName,
- _ref2$renderCountProp = _ref2.renderCountProp,
- renderCountProp = _ref2$renderCountProp === void 0 ? undefined : _ref2$renderCountProp,
- _ref2$shouldHandleSta = _ref2.shouldHandleStateChanges,
- shouldHandleStateChanges = _ref2$shouldHandleSta === void 0 ? true : _ref2$shouldHandleSta,
- _ref2$storeKey = _ref2.storeKey,
- storeKey = _ref2$storeKey === void 0 ? 'store' : _ref2$storeKey,
- _ref2$withRef = _ref2.withRef,
- withRef = _ref2$withRef === void 0 ? false : _ref2$withRef,
- _ref2$forwardRef = _ref2.forwardRef,
- forwardRef = _ref2$forwardRef === void 0 ? false : _ref2$forwardRef,
- _ref2$context = _ref2.context,
- context = _ref2$context === void 0 ? _Context.ReactReduxContext : _ref2$context,
- connectOptions = (0, _objectWithoutPropertiesLoose2["default"])(_ref2, ["getDisplayName", "methodName", "renderCountProp", "shouldHandleStateChanges", "storeKey", "withRef", "forwardRef", "context"]);
- if ("development" !== 'production') {
- if (renderCountProp !== undefined) {
- throw new Error("renderCountProp is removed. render counting is built into the latest React Dev Tools profiling extension");
- }
- if (withRef) {
- throw new Error('withRef is removed. To access the wrapped instance, use a ref on the connected component');
- }
- 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';
- if (storeKey !== 'store') {
- throw new Error('storeKey has been removed and does not do anything. ' + customStoreWarningMessage);
- }
- }
- var Context = context;
- return function wrapWithConnect(WrappedComponent) {
- if ("development" !== 'production' && !(0, _reactIs.isValidElementType)(WrappedComponent)) {
- throw new Error("You must pass a component to the function returned by " + (methodName + ". Instead received " + stringifyComponent(WrappedComponent)));
- }
- var wrappedComponentName = WrappedComponent.displayName || WrappedComponent.name || 'Component';
- var displayName = getDisplayName(wrappedComponentName);
- var selectorFactoryOptions = (0, _extends2["default"])({}, connectOptions, {
- getDisplayName: getDisplayName,
- methodName: methodName,
- renderCountProp: renderCountProp,
- shouldHandleStateChanges: shouldHandleStateChanges,
- storeKey: storeKey,
- displayName: displayName,
- wrappedComponentName: wrappedComponentName,
- WrappedComponent: WrappedComponent
- });
- var pure = connectOptions.pure;
- function createChildSelector(store) {
- return selectorFactory(store.dispatch, selectorFactoryOptions);
- }
-
-
- var usePureOnlyMemo = pure ? _react.useMemo : function (callback) {
- return callback();
- };
- function ConnectFunction(props) {
- var _useMemo = (0, _react.useMemo)(function () {
-
-
-
- var forwardedRef = props.forwardedRef,
- wrapperProps = (0, _objectWithoutPropertiesLoose2["default"])(props, ["forwardedRef"]);
- return [props.context, forwardedRef, wrapperProps];
- }, [props]),
- propsContext = _useMemo[0],
- forwardedRef = _useMemo[1],
- wrapperProps = _useMemo[2];
- var ContextToUse = (0, _react.useMemo)(function () {
-
-
- return propsContext && propsContext.Consumer && (0, _reactIs.isContextConsumer)(_react["default"].createElement(propsContext.Consumer, null)) ? propsContext : Context;
- }, [propsContext, Context]);
- var contextValue = (0, _react.useContext)(ContextToUse);
-
-
- var didStoreComeFromProps = Boolean(props.store) && Boolean(props.store.getState) && Boolean(props.store.dispatch);
- var didStoreComeFromContext = Boolean(contextValue) && Boolean(contextValue.store);
- if ("development" !== 'production' && !didStoreComeFromProps && !didStoreComeFromContext) {
- 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."));
- }
- var store = didStoreComeFromProps ? props.store : contextValue.store;
- var childPropsSelector = (0, _react.useMemo)(function () {
-
-
- return createChildSelector(store);
- }, [store]);
- var _useMemo2 = (0, _react.useMemo)(function () {
- if (!shouldHandleStateChanges) return NO_SUBSCRIPTION_ARRAY;
-
- var subscription = new _Subscription["default"](store, didStoreComeFromProps ? null : contextValue.subscription);
-
-
-
- var notifyNestedSubs = subscription.notifyNestedSubs.bind(subscription);
- return [subscription, notifyNestedSubs];
- }, [store, didStoreComeFromProps, contextValue]),
- subscription = _useMemo2[0],
- notifyNestedSubs = _useMemo2[1];
-
- var overriddenContextValue = (0, _react.useMemo)(function () {
- if (didStoreComeFromProps) {
-
-
-
- return contextValue;
- }
-
- return (0, _extends2["default"])({}, contextValue, {
- subscription: subscription
- });
- }, [didStoreComeFromProps, contextValue, subscription]);
-
- var _useReducer = (0, _react.useReducer)(storeStateUpdatesReducer, EMPTY_ARRAY, initStateUpdates),
- _useReducer$ = _useReducer[0],
- previousStateUpdateResult = _useReducer$[0],
- forceComponentUpdateDispatch = _useReducer[1];
- if (previousStateUpdateResult && previousStateUpdateResult.error) {
- throw previousStateUpdateResult.error;
- }
- var lastChildProps = (0, _react.useRef)();
- var lastWrapperProps = (0, _react.useRef)(wrapperProps);
- var childPropsFromStoreUpdate = (0, _react.useRef)();
- var renderIsScheduled = (0, _react.useRef)(false);
- var actualChildProps = usePureOnlyMemo(function () {
-
-
-
-
-
-
- if (childPropsFromStoreUpdate.current && wrapperProps === lastWrapperProps.current) {
- return childPropsFromStoreUpdate.current;
- }
-
-
-
- return childPropsSelector(store.getState(), wrapperProps);
- }, [store, previousStateUpdateResult, wrapperProps]);
-
-
- useIsomorphicLayoutEffectWithArgs(captureWrapperProps, [lastWrapperProps, lastChildProps, renderIsScheduled, wrapperProps, actualChildProps, childPropsFromStoreUpdate, notifyNestedSubs]);
- useIsomorphicLayoutEffectWithArgs(subscribeUpdates, [shouldHandleStateChanges, store, subscription, childPropsSelector, lastWrapperProps, lastChildProps, renderIsScheduled, childPropsFromStoreUpdate, notifyNestedSubs, forceComponentUpdateDispatch], [store, subscription, childPropsSelector]);
-
- var renderedWrappedComponent = (0, _react.useMemo)(function () {
- return _react["default"].createElement(WrappedComponent, (0, _extends2["default"])({}, actualChildProps, {
- ref: forwardedRef
- }));
- }, [forwardedRef, WrappedComponent, actualChildProps]);
-
- var renderedChild = (0, _react.useMemo)(function () {
- if (shouldHandleStateChanges) {
-
-
-
- return _react["default"].createElement(ContextToUse.Provider, {
- value: overriddenContextValue
- }, renderedWrappedComponent);
- }
- return renderedWrappedComponent;
- }, [ContextToUse, renderedWrappedComponent, overriddenContextValue]);
- return renderedChild;
- }
- var Connect = pure ? _react["default"].memo(ConnectFunction) : ConnectFunction;
- Connect.WrappedComponent = WrappedComponent;
- Connect.displayName = displayName;
- if (forwardRef) {
- var forwarded = _react["default"].forwardRef(function forwardConnectRef(props, ref) {
- return _react["default"].createElement(Connect, (0, _extends2["default"])({}, props, {
- forwardedRef: ref
- }));
- });
- forwarded.displayName = displayName;
- forwarded.WrappedComponent = WrappedComponent;
- return (0, _hoistNonReactStatics["default"])(forwarded, WrappedComponent);
- }
- return (0, _hoistNonReactStatics["default"])(Connect, WrappedComponent);
- };
- }
|