|
1 |
| -import React, { PropsWithChildren, ReactElement } from 'react'; |
2 |
| -import { Store } from 'redux'; |
| 1 | +import PropTypes from 'prop-types'; |
| 2 | +import React from 'react'; |
3 | 3 | import { Provider, useStore } from 'react-redux';
|
4 | 4 |
|
| 5 | +import { createPauseableStore, PauseableStoreInstance } from 'redux-pauseable-store'; |
| 6 | + |
5 | 7 | import { PauseableContainerProps } from './types';
|
6 | 8 |
|
7 |
| -const PauseableReduxContainer: React.FC<PauseableContainerProps> = ({ |
8 |
| - shouldUpdate, |
9 |
| - children, |
10 |
| -}: PropsWithChildren<PauseableContainerProps>): ReactElement | null => { |
11 |
| - const store = useStore(); |
12 |
| - const staticStoreRef = React.useRef<Store>(); |
13 |
| - const wasActiveRef = React.useRef<boolean>(); |
14 |
| - |
15 |
| - const stateWhenLastActive = React.useRef<Store>(); |
16 |
| - |
17 |
| - if (shouldUpdate) { |
18 |
| - // Track stuff for when we go inactive |
19 |
| - stateWhenLastActive.current = store.getState(); |
20 |
| - } else { |
21 |
| - if (wasActiveRef.current) { |
22 |
| - // We're going inactive: freeze the store contents to the last-active state |
23 |
| - staticStoreRef.current = { |
24 |
| - ...store, |
25 |
| - getState: (): ReturnType<typeof store.getState> => stateWhenLastActive.current, |
26 |
| - }; |
27 |
| - } else { |
28 |
| - // We're somehow being rendered in an initially-inactive state: that can't be right |
29 |
| - if (process.env.NODE_ENV !== 'production') { |
30 |
| - console.warn( |
31 |
| - 'PauseableReduxContainer is being mounted with shouldUpdate=false: this is probably a bug', |
32 |
| - ); |
33 |
| - } |
34 |
| - return null; |
35 |
| - } |
36 |
| - } |
37 |
| - |
38 |
| - wasActiveRef.current = shouldUpdate; |
39 |
| - return ( |
40 |
| - <Provider store={shouldUpdate ? store : (staticStoreRef.current as Store)}>{children}</Provider> |
| 9 | +export interface PauseableReduxContainerProps extends PauseableContainerProps { |
| 10 | + children: React.ReactNode; |
| 11 | + dispatchWhenPaused?: boolean | null; |
| 12 | +} |
| 13 | + |
| 14 | +const PauseableReduxContainer: React.FC<PauseableReduxContainerProps> = (props) => { |
| 15 | + const { dispatchWhenPaused, shouldUpdate, children } = props; |
| 16 | + |
| 17 | + const parentStore = useStore(); |
| 18 | + const pauseableStore = React.useMemo<PauseableStoreInstance>( |
| 19 | + () => |
| 20 | + createPauseableStore(parentStore, { |
| 21 | + // A change to the `shouldUpdate` prop will already cause a rerender, so we don't need an extra notification |
| 22 | + notifyListersOnUnpause: false, |
| 23 | + }), |
| 24 | + [parentStore], |
41 | 25 | );
|
| 26 | + |
| 27 | + pauseableStore.setPaused(!shouldUpdate); |
| 28 | + pauseableStore.setDispatch(dispatchWhenPaused); |
| 29 | + |
| 30 | + return <Provider store={pauseableStore}>{children}</Provider>; |
| 31 | +}; |
| 32 | + |
| 33 | +PauseableReduxContainer.defaultProps = { |
| 34 | + dispatchWhenPaused: null, |
| 35 | +}; |
| 36 | + |
| 37 | +PauseableReduxContainer.propTypes = { |
| 38 | + children: PropTypes.node.isRequired, |
| 39 | + dispatchWhenPaused: PropTypes.bool, |
| 40 | + shouldUpdate: PropTypes.bool.isRequired, |
42 | 41 | };
|
43 | 42 |
|
44 | 43 | export default PauseableReduxContainer;
|
0 commit comments