MCPcopy
hub / github.com/marimo-team/marimo / createReducer

Function createReducer

frontend/src/utils/createReducer.ts:55–79  ·  view source on GitHub ↗
(initialState: () => State, reducers: RH)

Source from the content-addressed store, hash-verified

53 * Utility function to create a reducer and its actions.
54 */
55export 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
81type Middleware<State, RH> = (
82 prevState: State,

Callers 2

createReducerAndAtomsFunction · 0.85

Calls 3

initialStateFunction · 0.50
dispatchFunction · 0.50
errorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…