1- import { PROXY_TREE , VALUE } from 'proxy-state-tree'
1+ import { PROXY_TREE , VALUE , PATH } from 'proxy-state-tree'
22
33import { deepCopy } from './utils'
44import { IState } from '.'
@@ -34,18 +34,15 @@ 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 , O2 = O > (
37+ transition < T extends States [ "current" ] , O = void > (
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 ,
4242 effectsCallback ?: ( ( current : Statemachine < Base , States , States extends {
4343 current : T
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 ;
44+ } ? States : never > ) => O )
45+ ) : O ;
4946}
5047
5148export type Statemachine < Base extends IState , States extends TStates , State extends TStates = States > = Base extends never ? State & MachineMethods < States > : Base & State & MachineMethods < States , Base >
@@ -56,15 +53,20 @@ const BASE = Symbol('BASE')
5653const STATE = Symbol ( 'STATE' )
5754const CURRENT_KEYS = Symbol ( 'CURRENT_KEYS' )
5855const NO_PROP = Symbol ( 'NO_PROP' )
56+ const IS_DISPOSED = Symbol ( 'IS_DISPOSED' )
5957
6058export class StateMachine < Base extends IState , States extends TStates , State extends TStates = States , > {
6159 current : State [ "current" ]
6260 private [ INITIAL_STATE ] : State [ "current" ]
6361 private [ STATE ] : any
6462 private [ BASE ] : any
63+ private [ IS_DISPOSED ] = false
6564 private clone ( ) {
6665 return new StateMachine ( this [ TRANSITIONS ] , deepCopy ( this [ STATE ] ) , deepCopy ( this [ BASE ] ) )
6766 }
67+ private dispose ( ) {
68+ this
69+ }
6870 constructor ( transitions : StatemachineTransitions < States > , state : States , base : Base ) {
6971 this [ STATE ] = state
7072 this [ INITIAL_STATE ] = state . current
@@ -74,6 +76,13 @@ export class StateMachine<Base extends IState, States extends TStates, State ext
7476 Object . assign ( this , state , base )
7577 }
7678 matches ( states , cb ) {
79+ if ( this [ VALUE ] [ IS_DISPOSED ] ) {
80+ if ( process . env . NODE_ENV === 'development' ) {
81+ console . warn ( `Overmind - The statemachine at "${ this [ PATH ] } " has been disposed, but you tried to match on it` )
82+ }
83+ return
84+ }
85+
7786 states = Array . isArray ( states ) ? states : [ states ]
7887
7988 if ( states . includes ( this . current ) ) {
@@ -89,7 +98,14 @@ export class StateMachine<Base extends IState, States extends TStates, State ext
8998 return result
9099 }
91100 }
92- transition ( state , newState , effectsCallback , stateCallback ) {
101+ transition ( state , newState , effectsCallback ) {
102+ if ( this [ VALUE ] [ IS_DISPOSED ] ) {
103+ if ( process . env . NODE_ENV === 'development' ) {
104+ console . warn ( `Overmind - The statemachine at "${ this [ PATH ] } " has been disposed, but you tried to transition on it` )
105+ }
106+ return
107+ }
108+
93109 const transitions = this [ VALUE ] [ TRANSITIONS ]
94110
95111 if ( transitions [ this . current ] . includes ( state ) ) {
@@ -100,6 +116,9 @@ export class StateMachine<Base extends IState, States extends TStates, State ext
100116
101117 this [ VALUE ] [ CURRENT_KEYS ] . forEach ( ( key ) => {
102118 if ( ! baseKeys . includes ( key ) ) {
119+ if ( this [ key ] instanceof StateMachine ) {
120+ this [ key ] . dispose ( )
121+ }
103122 delete this [ key ]
104123 }
105124 } )
@@ -115,18 +134,6 @@ export class StateMachine<Base extends IState, States extends TStates, State ext
115134 result = effectsCallback ( this )
116135 }
117136
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- } )
128- }
129-
130137 return result
131138 } else if ( process . env . NODE_ENV === 'development' && state !== this . current ) {
132139 console . warn ( `Overmind Statemachine - You tried to transition into "${ state } ", but it is not a valid transition. The valid transitions are ${ JSON . stringify ( transitions [ this . current ] ) } ` )
0 commit comments