Skip to content

Commit

Permalink
fix: Fix #480, engine was not assigned on the initial callback (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
fgmacedo authored Sep 11, 2024
1 parent e43c7dc commit e4a2092
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
1 change: 1 addition & 0 deletions statemachine/engines/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

class AsyncEngine:
def __init__(self, sm: "StateMachine", rtc: bool = True):
sm._engine = self
self.sm = proxy(sm)
self._sentinel = object()
if not rtc:
Expand Down
1 change: 1 addition & 0 deletions statemachine/engines/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

class SyncEngine:
def __init__(self, sm: "StateMachine", rtc: bool = True):
sm._engine = self
self.sm = proxy(sm)
self._sentinel = object()
self._rtc = rtc
Expand Down
9 changes: 5 additions & 4 deletions statemachine/statemachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ def __init__(
)
self._put_nonblocking(trigger_data)

self._engine = self._get_engine(rtc)
self._engine: AsyncEngine | SyncEngine | None = None
self._select_engine(rtc)

def _get_engine(self, rtc: bool):
def _select_engine(self, rtc: bool):
if self._callbacks_registry.has_async_callbacks:
return AsyncEngine(self, rtc=rtc)
AsyncEngine(self, rtc=rtc)
else:
return SyncEngine(self, rtc=rtc)
SyncEngine(self, rtc=rtc)

def activate_initial_state(self):
result = self._engine.activate_initial_state()
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ class TrafficLightMachine(StateMachine):
stop = yellow.to(red)
go = red.to(green)

def _get_engine(self, rtc: bool):
return engine(self, rtc)
def _select_engine(self, rtc: bool):
engine(self, rtc)

return TrafficLightMachine

Expand Down
4 changes: 2 additions & 2 deletions tests/test_transitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ class TestStateMachine(StateMachine):

loop = initial.to.itself(internal=internal)

def _get_engine(self, rtc: bool):
return engine(self, rtc)
def _select_engine(self, rtc: bool):
engine(self, rtc)

def on_exit_initial(self):
calls.append("on_exit_initial")
Expand Down
43 changes: 43 additions & 0 deletions tests/testcases/issue480.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@


### Issue 480

A StateMachine that exercises the example given on issue
#[480](https://github.com/fgmacedo/python-statemachine/issues/480).

Should be possible to trigger an event on the initial state activation handler.

```py
>>> from statemachine import StateMachine, State
>>>
>>> class MyStateMachine(StateMachine):
... State_1 = State(initial=True)
... State_2 = State()
... Trans_1 = State_1.to(State_2)
...
... def __init__(self):
... super(MyStateMachine, self).__init__()
...
... def on_enter_State_1(self):
... print("Entering State_1 state")
... self.long_running_task()
...
... def on_exit_State_1(self):
... print("Exiting State_1 state")
...
... def on_enter_State_2(self):
... print("Entering State_2 state")
...
... def long_running_task(self):
... print("long running task process started")
... self.Trans_1()
... print("long running task process ended")
...
>>> sm = MyStateMachine()
Entering State_1 state
long running task process started
long running task process ended
Exiting State_1 state
Entering State_2 state

```

0 comments on commit e4a2092

Please sign in to comment.