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 Jan 7, 2024
1 parent 8be5b73 commit 9c38e11
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 54 deletions.
30 changes: 15 additions & 15 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"dist/typesafe-actions.cjs.development.js": {
"bundled": 9908,
"minified": 5455,
"gzipped": 1373
"bundled": 9523,
"minified": 5402,
"gzipped": 1351
},
"dist/typesafe-actions.cjs.production.js": {
"bundled": 9908,
"minified": 5455,
"gzipped": 1373
"bundled": 9523,
"minified": 5402,
"gzipped": 1351
},
"dist/typesafe-actions.es.production.js": {
"bundled": 9718,
"minified": 5282,
"gzipped": 1336,
"bundled": 9333,
"minified": 5229,
"gzipped": 1315,
"treeshaked": {
"rollup": {
"code": 0,
Expand All @@ -24,13 +24,13 @@
}
},
"dist/typesafe-actions.umd.development.js": {
"bundled": 11390,
"minified": 4233,
"gzipped": 1349
"bundled": 10981,
"minified": 4180,
"gzipped": 1331
},
"dist/typesafe-actions.umd.production.js": {
"bundled": 11390,
"minified": 4233,
"gzipped": 1349
"bundled": 10981,
"minified": 4180,
"gzipped": 1331
}
}
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
27 changes: 5 additions & 22 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,15 +109,12 @@ 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,
};

const initializationActionTypes = /@@redux\/INIT.*/;

const rootReducer: Reducer<TState, TRootAction> = (
state = initialState,
action: TRootAction
Expand All @@ -131,12 +127,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) {
return defaultReducer(state, action);
} else {
return state;
Expand Down Expand Up @@ -179,16 +170,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 9c38e11

Please sign in to comment.