Skip to content

Commit dc8e773

Browse files
authored
refac: Improved internal APIs for engine and callbacks registry (#459)
1 parent 61ccc1f commit dc8e773

4 files changed

Lines changed: 8 additions & 12 deletions

File tree

statemachine/callbacks.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
from bisect import insort
3-
from collections import Counter
43
from collections import defaultdict
54
from collections import deque
65
from enum import IntEnum
@@ -335,7 +334,7 @@ def all(self, *args, **kwargs):
335334
class CallbacksRegistry:
336335
def __init__(self) -> None:
337336
self._registry: Dict[str, CallbacksExecutor] = defaultdict(CallbacksExecutor)
338-
self._method_types: Counter = Counter()
337+
self.has_async_callbacks: bool = False
339338

340339
def clear(self):
341340
self._registry.clear()
@@ -357,6 +356,6 @@ def check(self, specs: CallbackSpecList):
357356
)
358357

359358
def async_or_sync(self):
360-
self._method_types.update(
359+
self.has_async_callbacks = any(
361360
callback._iscoro for executor in self._registry.values() for callback in executor
362361
)

statemachine/engines/async_.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ async def activate_initial_state(self):
3131
Given how async works on python, there's no built-in way to activate the initial state that
3232
may depend on async code from the StateMachine.__init__ method.
3333
"""
34-
return await self._processing_loop()
34+
return await self.processing_loop()
3535

36-
async def _processing_loop(self):
36+
async def processing_loop(self):
3737
"""Process event triggers.
3838
3939
The simplest implementation is the non-RTC (synchronous),

statemachine/engines/sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def activate_initial_state(self):
2929
Given how async works on python, there's no built-in way to activate the initial state that
3030
may depend on async code from the StateMachine.__init__ method.
3131
"""
32-
return self._processing_loop()
32+
return self.processing_loop()
3333

34-
def _processing_loop(self):
34+
def processing_loop(self):
3535
"""Process event triggers.
3636
3737
The simplest implementation is the non-RTC (synchronous),

statemachine/statemachine.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def __init__(
106106
self._engine = self._get_engine(rtc)
107107

108108
def _get_engine(self, rtc: bool):
109-
if self._callbacks_registry._method_types[True] > 0:
109+
if self._callbacks_registry.has_async_callbacks:
110110
return AsyncEngine(self, rtc=rtc)
111111
else:
112112
return SyncEngine(self, rtc=rtc)
@@ -118,7 +118,7 @@ def activate_initial_state(self):
118118
return run_async_from_sync(result)
119119

120120
def _processing_loop(self):
121-
return self._engine._processing_loop()
121+
return self._engine.processing_loop()
122122

123123
def __init_subclass__(cls, strict_states: bool = False):
124124
cls._strict_states = strict_states
@@ -303,9 +303,6 @@ def _put_nonblocking(self, trigger_data: TriggerData):
303303
def send(self, event: str, *args, **kwargs):
304304
"""Send an :ref:`Event` to the state machine.
305305
306-
This is a thin wrapper around :meth:`async_send` to allow synchronous
307-
code to send events.
308-
309306
.. seealso::
310307
311308
See: :ref:`triggering events`.

0 commit comments

Comments
 (0)