Skip to content

Commit

Permalink
Remove init action handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jin60641 committed Dec 19, 2023
1 parent 8be5b73 commit 050e64f
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 37 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,6 @@ store.dispatch(add(3));
store.dispatch(reset());
```
The `defaultHandler` isn't invoked at initialization of the redux store (default behavior).
To enforce invoking the `defaultHandler` at initialization of the redux store, supply `true` as second argument to the `defaultHandler`.
Note, that the initialization action doesn't have a payload.
```ts
const counterReducer = createReducer(0)
.handleAction(add, (state, action) => state + action.payload)
.defaultHandler((state, action) => state = 0, true);
```
#### Alternative usage with regular switch reducer
First we need to start by generating a **tagged union type** of actions (`TodosAction`). It's very easy to do by using `ActionType` **type-helper**.
Expand Down
4 changes: 0 additions & 4 deletions src/create-reducer.spec.snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ const initialState = 0;
fn(0, add(4)); // => 4
});
// @dts-jest:pass
counterReducer5(0, reduxInit()); // => 0
// @dts-jest:pass
counterReducer5(0, {} as any); // => 1
// @dts-jest:pass
counterReducer5(0, add(4)); // => 4
Expand Down Expand Up @@ -299,8 +297,6 @@ const initialState = 0;
fn(0, add(4)); // => 4
});
// @dts-jest:pass
counterReducer5(0, reduxInit()); // => 0;
// @dts-jest:pass
counterReducer5(0, increment()); // => 1;
// @dts-jest:pass
counterReducer5(0, add(4)); // => 4;
Expand Down
4 changes: 0 additions & 4 deletions src/create-reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ const initialState = 0;
fn(0, add(4)); // => 4
});
// @dts-jest:pass
counterReducer5(0, reduxInit()); // => 0
// @dts-jest:pass
counterReducer5(0, {} as any); // => 1
// @dts-jest:pass
counterReducer5(0, add(4)); // => 4
Expand Down Expand Up @@ -299,8 +297,6 @@ const initialState = 0;
fn(0, add(4)); // => 4
});
// @dts-jest:pass
counterReducer5(0, reduxInit()); // => 0;
// @dts-jest:pass
counterReducer5(0, increment()); // => 1;
// @dts-jest:pass
counterReducer5(0, add(4)); // => 4;
Expand Down
25 changes: 5 additions & 20 deletions src/create-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ type HandleDefaultActionChainApi<
THandledAction extends ReturnType<TActionCreator>,
TOutputAction extends Exclude<TInputAction, THandledAction>
>(
reducer: (state: TState, action: THandledAction) => TState,
executeAtInitialization?: boolean
reducer: (state: TState, action: THandledAction) => TState
) => [TOutputAction] extends [Action]
? Reducer<TState, TRootAction> & {
handlers: Record<
Expand Down Expand Up @@ -110,8 +109,7 @@ type RootAction = Types extends { RootAction: infer T } ? T : any;
export function createReducer<TState, TRootAction extends Action = RootAction>(
initialState: TState,
initialHandlers: InitialHandler<TState, TRootAction> = {},
defaultReducer?: Reducer<TState, TRootAction>,
defaultReducerExecutedAtInitialization: boolean = false
defaultReducer?: Reducer<TState, TRootAction>
) {
const handlers: any = {
...initialHandlers,
Expand All @@ -131,12 +129,7 @@ export function createReducer<TState, TRootAction extends Action = RootAction>(
);
}
return reducer(state, action);
} else if (
defaultReducer &&
(defaultReducerExecutedAtInitialization ||
(!defaultReducerExecutedAtInitialization &&
!initializationActionTypes.test(action.type)))
) {
} else if (defaultReducer && !initializationActionTypes.test(action.type)) {
return defaultReducer(state, action);
} else {
return state;
Expand Down Expand Up @@ -179,16 +172,8 @@ export function createReducer<TState, TRootAction extends Action = RootAction>(
| HandleActionChainApi<TState, TRootAction, TRootAction>
| HandleTypeChainApi<TState, TRootAction, TRootAction>;

const defaultHandler = ((
reducer: any,
executeAtInitialization: boolean = false
) => {
return createReducer<TState, TRootAction>(
initialState,
handlers,
reducer,
executeAtInitialization
);
const defaultHandler = ((reducer: any) => {
return createReducer<TState, TRootAction>(initialState, handlers, reducer);
}) as HandleDefaultActionChainApi<TState, TRootAction, TRootAction>;

const chainApi = Object.assign(rootReducer, {
Expand Down

0 comments on commit 050e64f

Please sign in to comment.