29 lines
782 B
TypeScript
29 lines
782 B
TypeScript
import { ReduxAction } from "@appsmith/constants/ReduxActionConstants";
|
|
import produce from "immer";
|
|
|
|
export const createReducer = (
|
|
initialState: any,
|
|
handlers: { [type: string]: (state: any, action: any) => any },
|
|
) => {
|
|
return function reducer(state = initialState, action: ReduxAction<any>) {
|
|
if (handlers.hasOwnProperty(action.type)) {
|
|
return handlers[action.type](state, action);
|
|
} else {
|
|
return state;
|
|
}
|
|
};
|
|
};
|
|
|
|
export const createImmerReducer = (
|
|
initialState: any,
|
|
handlers: { [type: string]: any },
|
|
) => {
|
|
return function reducer(state = initialState, action: ReduxAction<any>) {
|
|
if (handlers.hasOwnProperty(action.type)) {
|
|
return produce(handlers[action.type])(state, action);
|
|
} else {
|
|
return state;
|
|
}
|
|
};
|
|
};
|