( useValue: (props: Props) => readonly [State, Update], options?: Options<State, Update> | DeprecatedOption, )
| 33 | type DeprecatedOption = boolean; |
| 34 | |
| 35 | export const createContainer = <State, Update extends AnyFunction, Props>( |
| 36 | useValue: (props: Props) => readonly [State, Update], |
| 37 | options?: Options<State, Update> | DeprecatedOption, |
| 38 | ) => { |
| 39 | if (typeof options === 'boolean') { |
| 40 | // eslint-disable-next-line no-console |
| 41 | console.warn( |
| 42 | 'boolean option is deprecated, please specify { concurrentMode: true }', |
| 43 | ); |
| 44 | options = { concurrentMode: options }; |
| 45 | } |
| 46 | const { |
| 47 | stateContextName = 'StateContainer', |
| 48 | updateContextName = 'UpdateContainer', |
| 49 | concurrentMode, |
| 50 | } = options || {}; |
| 51 | const StateContext = createContext<State | undefined>(options?.defaultState); |
| 52 | const UpdateContext = createContextOrig<Update | undefined>( |
| 53 | options?.defaultUpdate, |
| 54 | ); |
| 55 | StateContext.displayName = stateContextName; |
| 56 | UpdateContext.displayName = updateContextName; |
| 57 | |
| 58 | const Provider = (props: Props & { children: ReactNode }) => { |
| 59 | const [state, update] = useValue(props); |
| 60 | return createElement( |
| 61 | UpdateContext.Provider, |
| 62 | { value: update }, |
| 63 | createElement( |
| 64 | StateContext.Provider as ComponentType<{ |
| 65 | value: State; |
| 66 | }>, |
| 67 | { value: state }, |
| 68 | props.children, |
| 69 | ), |
| 70 | ); |
| 71 | }; |
| 72 | |
| 73 | const useSelector = <Selected>(selector: (state: State) => Selected) => { |
| 74 | if (hasGlobalProcess && process.env.NODE_ENV !== 'production') { |
| 75 | const selectorOrig = selector; |
| 76 | selector = (state: State) => { |
| 77 | if (state === undefined) { |
| 78 | throw new Error('Please use <Provider>'); |
| 79 | } |
| 80 | return selectorOrig(state); |
| 81 | }; |
| 82 | } |
| 83 | const selected = useContextSelector( |
| 84 | StateContext as Context<State>, |
| 85 | selector, |
| 86 | ); |
| 87 | useDebugValue(selected); |
| 88 | return selected; |
| 89 | }; |
| 90 | |
| 91 | const useTrackedState = createTrackedSelector(useSelector); |
| 92 |
no test coverage detected
searching dependent graphs…