1- import { ActionCreator , AnyAction , isType } from "typescript-fsa" ;
1+ import { Action , ActionCreator , AnyAction , isType } from "typescript-fsa" ;
22
33export interface ReducerBuilder < InS extends OutS , OutS > {
44 case < P > ( actionCreator : ActionCreator < P > , handler : Handler < InS , OutS , P > ) : ReducerBuilder < InS , OutS > ;
5+ caseWithAction < P > (
6+ actionCreator : ActionCreator < P > ,
7+ handler : Handler < InS , OutS , Action < P > > ,
8+ ) : ReducerBuilder < InS , OutS > ;
59 build ( ) : ( state : InS , action : AnyAction ) => OutS ;
610 ( state : InS , action : AnyAction ) : OutS ;
711}
@@ -24,7 +28,7 @@ export function upcastingReducer<InS extends OutS, OutS>(): ReducerBuilder<InS,
2428
2529interface Case < InS extends OutS , OutS , P > {
2630 actionCreator : ActionCreator < P > ;
27- handler : Handler < InS , OutS , P > ;
31+ handler : Handler < InS , OutS , Action < P > > ;
2832}
2933
3034type CaseList < InS extends OutS , OutS > = Array < Case < InS , OutS , any > > ;
@@ -33,14 +37,17 @@ function makeReducer<InS extends OutS, OutS>(initialState?: InS): ReducerBuilder
3337 const cases : CaseList < InS , OutS > = [ ] ;
3438 const reducer = getReducerFunction ( initialState , cases ) as ReducerBuilder < InS , OutS > ;
3539
36- reducer . case = < P > (
40+ reducer . caseWithAction = < P > (
3741 actionCreator : ActionCreator < P > ,
38- handler : Handler < InS , OutS , P >
39- ) : ReducerBuilder < InS , OutS > => {
42+ handler : Handler < InS , OutS , Action < P > > ,
43+ ) => {
4044 cases . push ( { actionCreator, handler } ) ;
4145 return reducer ;
4246 } ;
4347
48+ reducer . case = < P > ( actionCreator : ActionCreator < P > , handler : Handler < InS , OutS , P > ) =>
49+ reducer . caseWithAction ( actionCreator , ( state , action ) => handler ( state , action . payload ) ) ;
50+
4451 reducer . build = ( ) => getReducerFunction ( initialState , cases . slice ( ) ) ;
4552
4653 return reducer ;
@@ -54,7 +61,7 @@ function getReducerFunction<InS extends OutS, OutS>(
5461 for ( let i = 0 , length = cases . length ; i < length ; i ++ ) {
5562 const { actionCreator, handler } = cases [ i ] ;
5663 if ( isType ( action , actionCreator ) ) {
57- return handler ( state , action . payload ) ;
64+ return handler ( state , action ) ;
5865 }
5966 }
6067 return state ;
0 commit comments