Skip to content

Commit a66f478

Browse files
committed
add/update docblocks
1 parent 379c6f1 commit a66f478

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/createReducer.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,21 @@ export function createReducer<
5656
S,
5757
CR extends CaseReducers<S, any> = CaseReducers<S, any>
5858
>(initialState: S, actionsMap: CR): Reducer<S>
59-
59+
/**
60+
* A utility function that allows defining a reducer as a mapping from action
61+
* type to *case reducer* functions that handle these action types. The
62+
* reducer's initial state is passed as the first argument.
63+
*
64+
* The body of every case reducer is implicitly wrapped with a call to
65+
* `produce()` from the [immer](https://github.com/mweststrate/immer) library.
66+
* This means that rather than returning a new state object, you can also
67+
* mutate the passed-in state object directly; these mutations will then be
68+
* automatically and efficiently translated into copies, giving you both
69+
* convenience and immutability.
70+
* @param initialState The initial state to be returned by the reducer.
71+
* @param builderCallback A callback that receives a *builder* object to define
72+
* case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
73+
*/
6074
export function createReducer<S>(
6175
initialState: S,
6276
builderCallback: (builder: ActionReducerMapBuilder<S>) => void

src/createSlice.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export interface CreateSliceOptions<
7676
* A mapping from action types to action-type-specific *case reducer*
7777
* functions. These reducers should have existing action types used
7878
* as the keys, and action creators will _not_ be generated.
79+
* Alternatively, a callback that receives a *builder* object to define
80+
* case reducers via calls to `builder.addCase(actionCreatorOrType, reducer)`.
7981
*/
8082
extraReducers?:
8183
| CaseReducers<NoInfer<State>, any>

src/mapBuilders.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,24 @@ export interface TypedActionCreator<Type extends string> {
66
type: Type
77
}
88

9+
/**
10+
* A builder for an action <-> reducer map.
11+
*/
912
export interface ActionReducerMapBuilder<State> {
13+
/**
14+
* Add a case reducer for actions created by this action creator.
15+
* @param actionCreator
16+
* @param reducer
17+
*/
1018
addCase<ActionCreator extends TypedActionCreator<string>>(
1119
actionCreator: ActionCreator,
1220
reducer: CaseReducer<State, ReturnType<ActionCreator>>
1321
): ActionReducerMapBuilder<State>
22+
/**
23+
* Add a case reducer for actions with the specified type.
24+
* @param type
25+
* @param reducer
26+
*/
1427
addCase<Type extends string, A extends Action<Type>>(
1528
type: Type,
1629
reducer: CaseReducer<State, A>

0 commit comments

Comments
 (0)