Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cascadeReducer): Quick feedback #427

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions client/src/reducers/cascade.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import { AnyAction } from "redux";
import { AnyAction, Reducer } from "redux";
import type { RootState } from ".";

export type ReducerFunction = (
prevStateForKey: any,
prevStateForKey: RootState[keyof RootState],
action: AnyAction,
nexState?: RootState,
prevState?: RootState
) => any;
) => RootState;

type CascadedReducers =
| [string, ReducerFunction][]
| Map<string, ReducerFunction>;

export default function cascadeReducers(arg: CascadedReducers) {
/*
Combine a set of cascading reducers into a single reducer. Cascading
reducers are reducers which may rely on state computed by another reducer.
Therefore, they:
- must be composed in a particular order (currently, this is a simple
linear list of reducers, run in list order)
- must have access to partially updated "next state" so they can further
derive state.

Parameter is one of:
- a Map object
- an array of tuples, [ [key1, reducer1], [key2, reducer2], ... ]
Ie, cascadeReducers([ ["a", reduceA], ["b", reduceB] ])

Each reducer will be called with the signature:
(prevState, action, sharedNextState, sharedPrevState) => newState

cascadeReducers will build a composite newState object, much
like combinedReducers. Additional semantics:
- reducers guaranteed to be called in order
- each reducer will receive shared objects
*/
export default function cascadeReducers(arg: CascadedReducers): Reducer {
/**
* Combine a set of cascading reducers into a single reducer. Cascading
* reducers are reducers which may rely on state computed by another reducer.
* Therefore, they:
* - must be composed in a particular order (currently, this is a simple
* linear list of reducers, run in list order)
* - must have access to partially updated "next state" so they can further
* derive state.
*
* Parameter is one of:
* - a Map object
* - an array of tuples, [ [key1, reducer1], [key2, reducer2], ... ]
* Ie, cascadeReducers([ ["a", reduceA], ["b", reduceB] ])
*
* Each reducer will be called with the signature:
* (prevState, action, sharedNextState, sharedPrevState) => newState
* cascadeReducers will build a composite newState object, much
* like combinedReducers. Additional semantics:
* - reducers guaranteed to be called in order
* - each reducer will receive shared objects
*/
const reducers = arg instanceof Map ? arg : new Map(arg);
const reducerKeys = [...reducers.keys()];

Expand Down