@@ -10,7 +10,7 @@ type TStates = {
1010}
1111
1212export type StatemachineTransitions < States extends TStates , BaseState extends IState > = {
13- [ State in States [ "current" ] ] : Array < States [ "current" ] > | ( ( payload : Omit < States extends { current : State } ? States : never , 'current' > , state : Statemachine < States , BaseState > ) => undefined | Statemachine < States , BaseState > | Array < States [ "current" ] > | boolean )
13+ [ State in States [ "current" ] ] : Array < States [ "current" ] > | ( ( payload : Omit < States extends { current : State } ? States : never , 'current' > , state : Statemachine < States , BaseState > ) => States [ "current" ] | Array < States [ "current" ] > | boolean )
1414}
1515
1616export interface MachineMethods < States extends TStates , Base extends IState > {
@@ -20,6 +20,10 @@ export interface MachineMethods<States extends TStates, Base extends IState> {
2020 transition < T extends States [ "current" ] > (
2121 newState : Partial < Base > & States extends { current : T } ? States : never
2222 ) : Statemachine < States extends { current : T } ? States : never , Base > | undefined
23+ transition < T extends States [ "current" ] > (
24+ newState : T ,
25+ cb : ( state : Statemachine < States extends { current : T } ? States : never , Base > ) => void
26+ ) : Statemachine < States extends { current : T } ? States : never , Base > | undefined
2327}
2428
2529export type Statemachine < States extends TStates , Base extends IState = never > = ( [ Base ] extends [ never ] ? MachineMethods < States , { } > : Base & MachineMethods < States , Base > ) & States
@@ -54,7 +58,7 @@ export class StateMachine<Base extends IState, States extends TStates> {
5458 this [ TRANSITIONS ] = transitions
5559 Object . assign ( this , state )
5660 }
57- transition ( newState : any ) {
61+ transition ( ... args ) {
5862 if ( this [ VALUE ] [ IS_DISPOSED ] ) {
5963 if ( process . env . NODE_ENV === 'development' ) {
6064 console . warn ( `Overmind - The statemachine at "${ this [ PATH ] } " has been disposed, but you tried to transition on it` )
@@ -64,13 +68,15 @@ export class StateMachine<Base extends IState, States extends TStates> {
6468
6569 const transition = this [ VALUE ] [ CURRENT_DYNAMIC_TRANSITION ] || this [ VALUE ] [ TRANSITIONS ] [ this . current ]
6670
67- if ( transition . includes ( newState . current ) ) {
71+ const newState = args . length === 2 ? args [ 0 ] : args [ 0 ] . current
72+
73+ if ( transition . includes ( newState ) ) {
6874 let dynamicResult : any
6975
7076 this [ VALUE ] [ CURRENT_DYNAMIC_TRANSITION ] = null
7177
72- if ( typeof this [ VALUE ] [ TRANSITIONS ] [ newState . current ] === 'function' ) {
73- dynamicResult = this [ VALUE ] [ TRANSITIONS ] [ newState . current ] ( newState , this )
78+ if ( typeof this [ VALUE ] [ TRANSITIONS ] [ newState ] === 'function' ) {
79+ dynamicResult = this [ VALUE ] [ TRANSITIONS ] [ newState ] ( newState , this )
7480
7581 if ( ! dynamicResult ) {
7682 return
@@ -81,13 +87,30 @@ export class StateMachine<Base extends IState, States extends TStates> {
8187 }
8288 }
8389
84- Object . assign ( this , newState )
90+ const existingState = this . current
91+ if ( args . length === 2 ) {
92+ this . current = newState
93+ args [ 1 ] ( this )
94+ } else {
95+ Object . assign ( this , args [ 0 ] )
96+ }
97+
98+
99+ if ( typeof dynamicResult === 'string' ) {
100+ this [ VALUE ] [ CURRENT_DYNAMIC_TRANSITION ] = [ dynamicResult ]
101+ return this . transition ( { current : dynamicResult } )
102+ }
103+
104+ if ( dynamicResult === true ) {
105+ this [ VALUE ] [ CURRENT_DYNAMIC_TRANSITION ] = [ existingState ]
106+ return this . transition ( { current : existingState } )
107+ }
85108
86109 return this
87- } else if ( process . env . NODE_ENV === 'development' && newState . current !== this . current ) {
110+ } else if ( process . env . NODE_ENV === 'development' && newState !== this . current ) {
88111 console . warn ( `Overmind Statemachine - You tried to transition into "${ newState . current } ", but it is not a valid transition. The valid transitions are ${ JSON . stringify ( transition ) } ` )
89- } else if ( process . env . NODE_ENV === 'development' && newState . current === this . current ) {
90- console . warn ( `Overmind Statemachine - You tried to transition into "${ newState . current } ", but you are already in this state. Do a "match" before running this piece of logic or add it as a valid state transition for this state` )
112+ } else if ( process . env . NODE_ENV === 'development' && newState === this . current ) {
113+ console . warn ( `Overmind Statemachine - You tried to transition into "${ newState } ", but you are already in this state. Do a "match" before running this piece of logic or add it as a valid state transition for this state` )
91114 }
92115
93116 return
0 commit comments