Skip to content

Commit

Permalink
add/update docblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Nov 24, 2019
1 parent 379c6f1 commit a66f478
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/createReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,21 @@ export function createReducer<
S,
CR extends CaseReducers<S, any> = CaseReducers<S, any>
>(initialState: S, actionsMap: CR): Reducer<S>

/**
* A utility function that allows defining a reducer as a mapping from action
* type to *case reducer* functions that handle these action types. The
* reducer's initial state is passed as the first argument.
*
* The body of every case reducer is implicitly wrapped with a call to
* `produce()` from the [immer](https://github.com/mweststrate/immer) library.
* This means that rather than returning a new state object, you can also
* mutate the passed-in state object directly; these mutations will then be
* automatically and efficiently translated into copies, giving you both
* convenience and immutability.
* @param initialState The initial state to be returned by the reducer.
* @param builderCallback A callback that receives a *builder* object to define
* case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
*/
export function createReducer<S>(
initialState: S,
builderCallback: (builder: ActionReducerMapBuilder<S>) => void
Expand Down
2 changes: 2 additions & 0 deletions src/createSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export interface CreateSliceOptions<
* A mapping from action types to action-type-specific *case reducer*
* functions. These reducers should have existing action types used
* as the keys, and action creators will _not_ be generated.
* Alternatively, a callback that receives a *builder* object to define
* case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
*/
extraReducers?:
| CaseReducers<NoInfer<State>, any>
Expand Down
13 changes: 13 additions & 0 deletions src/mapBuilders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ export interface TypedActionCreator<Type extends string> {
type: Type
}

/**
* A builder for an action <-> reducer map.
*/
export interface ActionReducerMapBuilder<State> {
/**
* Add a case reducer for actions created by this action creator.
* @param actionCreator
* @param reducer
*/
addCase<ActionCreator extends TypedActionCreator<string>>(
actionCreator: ActionCreator,
reducer: CaseReducer<State, ReturnType<ActionCreator>>
): ActionReducerMapBuilder<State>
/**
* Add a case reducer for actions with the specified type.
* @param type
* @param reducer
*/
addCase<Type extends string, A extends Action<Type>>(
type: Type,
reducer: CaseReducer<State, A>
Expand Down

0 comments on commit a66f478

Please sign in to comment.