You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					44 lines
				
				1.3 KiB
			
		
		
			
		
	
	
					44 lines
				
				1.3 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								import React, { useMemo } from 'react';
							 | 
						||
| 
								 | 
							
								import { ReactReduxContext } from './Context';
							 | 
						||
| 
								 | 
							
								import { createSubscription } from '../utils/Subscription';
							 | 
						||
| 
								 | 
							
								import { useIsomorphicLayoutEffect } from '../utils/useIsomorphicLayoutEffect';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								function Provider({
							 | 
						||
| 
								 | 
							
								  store,
							 | 
						||
| 
								 | 
							
								  context,
							 | 
						||
| 
								 | 
							
								  children,
							 | 
						||
| 
								 | 
							
								  serverState
							 | 
						||
| 
								 | 
							
								}) {
							 | 
						||
| 
								 | 
							
								  const contextValue = useMemo(() => {
							 | 
						||
| 
								 | 
							
								    const subscription = createSubscription(store);
							 | 
						||
| 
								 | 
							
								    return {
							 | 
						||
| 
								 | 
							
								      store,
							 | 
						||
| 
								 | 
							
								      subscription,
							 | 
						||
| 
								 | 
							
								      getServerState: serverState ? () => serverState : undefined
							 | 
						||
| 
								 | 
							
								    };
							 | 
						||
| 
								 | 
							
								  }, [store, serverState]);
							 | 
						||
| 
								 | 
							
								  const previousState = useMemo(() => store.getState(), [store]);
							 | 
						||
| 
								 | 
							
								  useIsomorphicLayoutEffect(() => {
							 | 
						||
| 
								 | 
							
								    const {
							 | 
						||
| 
								 | 
							
								      subscription
							 | 
						||
| 
								 | 
							
								    } = contextValue;
							 | 
						||
| 
								 | 
							
								    subscription.onStateChange = subscription.notifyNestedSubs;
							 | 
						||
| 
								 | 
							
								    subscription.trySubscribe();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if (previousState !== store.getState()) {
							 | 
						||
| 
								 | 
							
								      subscription.notifyNestedSubs();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return () => {
							 | 
						||
| 
								 | 
							
								      subscription.tryUnsubscribe();
							 | 
						||
| 
								 | 
							
								      subscription.onStateChange = undefined;
							 | 
						||
| 
								 | 
							
								    };
							 | 
						||
| 
								 | 
							
								  }, [contextValue, previousState]);
							 | 
						||
| 
								 | 
							
								  const Context = context || ReactReduxContext; // @ts-ignore 'AnyAction' is assignable to the constraint of type 'A', but 'A' could be instantiated with a different subtype
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  return /*#__PURE__*/React.createElement(Context.Provider, {
							 | 
						||
| 
								 | 
							
								    value: contextValue
							 | 
						||
| 
								 | 
							
								  }, children);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export default Provider;
							 |