| 989 | * Redux instrumentation store enhancer. |
| 990 | */ |
| 991 | export function instrument< |
| 992 | OptionsS, |
| 993 | OptionsA extends Action<unknown>, |
| 994 | MonitorState = null, |
| 995 | MonitorAction extends Action<unknown> = never |
| 996 | >( |
| 997 | monitorReducer: Reducer<MonitorState, MonitorAction> = (() => |
| 998 | null) as unknown as Reducer<MonitorState, MonitorAction>, |
| 999 | options: Options<OptionsS, OptionsA, MonitorState, MonitorAction> = {} |
| 1000 | ): StoreEnhancer<InstrumentExt<any, any, MonitorState>> { |
| 1001 | if (typeof options.maxAge === 'number' && options.maxAge < 2) { |
| 1002 | throw new Error( |
| 1003 | 'DevTools.instrument({ maxAge }) option, if specified, ' + |
| 1004 | 'may not be less than 2.' |
| 1005 | ); |
| 1006 | } |
| 1007 | |
| 1008 | return <NextExt, NextStateExt>( |
| 1009 | createStore: StoreEnhancerStoreCreator<NextExt, NextStateExt> |
| 1010 | ) => |
| 1011 | <S, A extends Action<unknown>>( |
| 1012 | reducer: Reducer<S, A>, |
| 1013 | initialState?: PreloadedState<S> |
| 1014 | ) => { |
| 1015 | function liftReducer(r: Reducer<S, A>) { |
| 1016 | if (typeof r !== 'function') { |
| 1017 | if (r && typeof (r as { default: unknown }).default === 'function') { |
| 1018 | throw new Error( |
| 1019 | 'Expected the reducer to be a function. ' + |
| 1020 | 'Instead got an object with a "default" field. ' + |
| 1021 | 'Did you pass a module instead of the default export? ' + |
| 1022 | 'Try passing require(...).default instead.' |
| 1023 | ); |
| 1024 | } |
| 1025 | throw new Error('Expected the reducer to be a function.'); |
| 1026 | } |
| 1027 | return liftReducerWith<S, A, MonitorState, MonitorAction>( |
| 1028 | r, |
| 1029 | initialState, |
| 1030 | monitorReducer, |
| 1031 | options as unknown as Options<S, A, MonitorState, MonitorAction> |
| 1032 | ); |
| 1033 | } |
| 1034 | |
| 1035 | const liftedStore = createStore(liftReducer(reducer)); |
| 1036 | if ( |
| 1037 | ( |
| 1038 | liftedStore as Store< |
| 1039 | LiftedState<S, A, MonitorState> & NextStateExt, |
| 1040 | LiftedAction<S, A, MonitorState> |
| 1041 | > & |
| 1042 | NextExt & { |
| 1043 | liftedStore: Store< |
| 1044 | LiftedState<S, A, MonitorState>, |
| 1045 | LiftedAction<S, A, MonitorState> |
| 1046 | >; |
| 1047 | } |
| 1048 | ).liftedStore |