(initialState: () => State, reducers: RH)
| 53 | * Utility function to create a reducer and its actions. |
| 54 | */ |
| 55 | export function createReducer< |
| 56 | State, |
| 57 | RH extends ReducerHandlers<NoInfer<State>>, |
| 58 | >(initialState: () => State, reducers: RH): ReducerCreatorResult<State, RH> { |
| 59 | return { |
| 60 | reducer: (state, action: ReducerActionOf<RH>) => { |
| 61 | state = state || initialState(); |
| 62 | if (action.type in reducers) { |
| 63 | return reducers[action.type](state, action.payload); |
| 64 | } |
| 65 | |
| 66 | Logger.error(`Action type ${action.type} is not defined in reducers.`); |
| 67 | return state; |
| 68 | }, |
| 69 | createActions: (dispatch: Dispatch<RH>) => { |
| 70 | const actions = {} as ReducerActions<RH>; |
| 71 | for (const type in reducers) { |
| 72 | (actions as any)[type] = (payload: any) => { |
| 73 | dispatch({ type, payload } as any); |
| 74 | }; |
| 75 | } |
| 76 | return actions; |
| 77 | }, |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | type Middleware<State, RH> = ( |
| 82 | prevState: State, |
no test coverage detected
searching dependent graphs…