@@ -34,15 +34,18 @@ export interface MachineMethods<States extends TStates, Base extends IState = {}
3434 matches < T extends States [ "current" ] , O = void > ( state : T | T [ ] , cb : ( current : Statemachine < Base , States , States extends {
3535 current : T
3636 } ? States : never > ) => O ) : O ;
37- transition < T extends States [ "current" ] , O = void > (
37+ transition < T extends States [ "current" ] , O = void , O2 = O > (
3838 state : T ,
3939 newState : States extends {
4040 current : T
4141 } ? Exact < Omit < States , 'current' > , { } > extends never ? Omit < States , 'current' > & Partial < Base > : Exact < Partial < Base > , { } > extends never ? Partial < Base > : { [ NO_PROP ] ?: true } : never ,
42- callback ?: ( ( current : Statemachine < Base , States , States extends {
42+ effectsCallback ?: ( ( current : Statemachine < Base , States , States extends {
4343 current : T
44- } ? States : never > ) => O )
45- ) : O ;
44+ } ? States : never > ) => O ) ,
45+ stateCallback ?: ( ( current : Statemachine < Base , States , States extends {
46+ current : T
47+ } ? States : never > , value : O extends Promise < infer V > ? V : O ) => O2 )
48+ ) : O extends Promise < any > ? Promise < O2 > : O2 ;
4649}
4750
4851export type Statemachine < Base extends IState , States extends TStates , State extends TStates = States > = Base extends never ? State & MachineMethods < States > : Base & State & MachineMethods < States , Base >
@@ -86,7 +89,7 @@ export class StateMachine<Base extends IState, States extends TStates, State ext
8689 return result
8790 }
8891 }
89- transition ( state , newState , callback ) {
92+ transition ( state , newState , effectsCallback , stateCallback ) {
9093 const transitions = this [ VALUE ] [ TRANSITIONS ]
9194
9295 if ( transitions [ this . current ] . includes ( state ) ) {
@@ -108,8 +111,20 @@ export class StateMachine<Base extends IState, States extends TStates, State ext
108111 tree . blockMutations ( )
109112
110113 let result
111- if ( callback ) {
112- result = callback ( this )
114+ if ( effectsCallback ) {
115+ result = effectsCallback ( this )
116+ }
117+
118+ if ( stateCallback && result instanceof Promise ) {
119+ return result . then ( ( value ) => {
120+ if ( this . current !== state ) {
121+ return value
122+ }
123+ tree . enableMutations ( )
124+ const result = stateCallback ( this , value )
125+ tree . blockMutations ( )
126+ return result
127+ } )
113128 }
114129
115130 return result
0 commit comments