File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1515
1616class AsyncEngine :
1717 def __init__ (self , sm : "StateMachine" , rtc : bool = True ):
18+ sm ._engine = self
1819 self .sm = proxy (sm )
1920 self ._sentinel = object ()
2021 if not rtc :
Original file line number Diff line number Diff line change 1313
1414class SyncEngine :
1515 def __init__ (self , sm : "StateMachine" , rtc : bool = True ):
16+ sm ._engine = self
1617 self .sm = proxy (sm )
1718 self ._sentinel = object ()
1819 self ._rtc = rtc
Original file line number Diff line number Diff line change @@ -105,13 +105,14 @@ def __init__(
105105 )
106106 self ._put_nonblocking (trigger_data )
107107
108- self ._engine = self ._get_engine (rtc )
108+ self ._engine : AsyncEngine | SyncEngine | None = None
109+ self ._select_engine (rtc )
109110
110- def _get_engine (self , rtc : bool ):
111+ def _select_engine (self , rtc : bool ):
111112 if self ._callbacks_registry .has_async_callbacks :
112- return AsyncEngine (self , rtc = rtc )
113+ AsyncEngine (self , rtc = rtc )
113114 else :
114- return SyncEngine (self , rtc = rtc )
115+ SyncEngine (self , rtc = rtc )
115116
116117 def activate_initial_state (self ):
117118 result = self ._engine .activate_initial_state ()
Original file line number Diff line number Diff line change @@ -135,8 +135,8 @@ class TrafficLightMachine(StateMachine):
135135 stop = yellow .to (red )
136136 go = red .to (green )
137137
138- def _get_engine (self , rtc : bool ):
139- return engine (self , rtc )
138+ def _select_engine (self , rtc : bool ):
139+ engine (self , rtc )
140140
141141 return TrafficLightMachine
142142
Original file line number Diff line number Diff line change @@ -252,8 +252,8 @@ class TestStateMachine(StateMachine):
252252
253253 loop = initial .to .itself (internal = internal )
254254
255- def _get_engine (self , rtc : bool ):
256- return engine (self , rtc )
255+ def _select_engine (self , rtc : bool ):
256+ engine (self , rtc )
257257
258258 def on_exit_initial (self ):
259259 calls .append ("on_exit_initial" )
Original file line number Diff line number Diff line change 1+
2+
3+ ### Issue 480
4+
5+ A StateMachine that exercises the example given on issue
6+ #[ 480] ( https://github.com/fgmacedo/python-statemachine/issues/480 ) .
7+
8+ Should be possible to trigger an event on the initial state activation handler.
9+
10+ ``` py
11+ >> > from statemachine import StateMachine, State
12+ >> >
13+ >> > class MyStateMachine (StateMachine ):
14+ ... State_1 = State(initial = True )
15+ ... State_2 = State()
16+ ... Trans_1 = State_1.to(State_2)
17+ ...
18+ ... def __init__ (self ):
19+ ... super (MyStateMachine, self ).__init__ ()
20+ ...
21+ ... def on_enter_State_1 (self ):
22+ ... print (" Entering State_1 state" )
23+ ... self .long_running_task()
24+ ...
25+ ... def on_exit_State_1 (self ):
26+ ... print (" Exiting State_1 state" )
27+ ...
28+ ... def on_enter_State_2 (self ):
29+ ... print (" Entering State_2 state" )
30+ ...
31+ ... def long_running_task (self ):
32+ ... print (" long running task process started" )
33+ ... self .Trans_1()
34+ ... print (" long running task process ended" )
35+ ...
36+ >> > sm = MyStateMachine()
37+ Entering State_1 state
38+ long running task process started
39+ long running task process ended
40+ Exiting State_1 state
41+ Entering State_2 state
42+
43+ ```
You can’t perform that action at this time.
0 commit comments