|
| 1 | + |
| 2 | +(observers)= |
| 3 | +# Listeners |
| 4 | + |
| 5 | +Listeners are a way to generically add behavior to a state machine without |
| 6 | +changing its internal implementation. |
| 7 | + |
| 8 | +One possible use case is to add a listener that prints a log message when the SM runs a |
| 9 | +transition or enters a new state. |
| 10 | + |
| 11 | +Giving the {ref}`sphx_glr_auto_examples_traffic_light_machine.py` as example: |
| 12 | + |
| 13 | + |
| 14 | +```py |
| 15 | +>>> from tests.examples.traffic_light_machine import TrafficLightMachine |
| 16 | + |
| 17 | +>>> class LogListener(object): |
| 18 | +... def __init__(self, name): |
| 19 | +... self.name = name |
| 20 | +... |
| 21 | +... def after_transition(self, event, source, target): |
| 22 | +... print(f"{self.name} after: {source.id}--({event})-->{target.id}") |
| 23 | +... |
| 24 | +... def on_enter_state(self, target, event): |
| 25 | +... print(f"{self.name} enter: {target.id} from {event}") |
| 26 | + |
| 27 | + |
| 28 | +>>> sm = TrafficLightMachine(listeners=[LogListener("Paulista Avenue")]) |
| 29 | +Paulista Avenue enter: green from __initial__ |
| 30 | + |
| 31 | +>>> sm.cycle() |
| 32 | +Paulista Avenue enter: yellow from cycle |
| 33 | +Paulista Avenue after: green--(cycle)-->yellow |
| 34 | +'Running cycle from green to yellow' |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | +## Adding listeners to an instance |
| 39 | + |
| 40 | +Attach listeners to an already running state machine instance using `add_listener`. |
| 41 | + |
| 42 | +Exploring our example, imagine that you can implement the LED panel as a listener, that |
| 43 | +reacts to state changes and turn on/off automatically. |
| 44 | + |
| 45 | + |
| 46 | +``` py |
| 47 | +>>> class LedPanel: |
| 48 | +... |
| 49 | +... def __init__(self, color: str): |
| 50 | +... self.color = color |
| 51 | +... |
| 52 | +... def on_enter_state(self, target: State): |
| 53 | +... if target.id == self.color: |
| 54 | +... print(f"{self.color} turning on") |
| 55 | +... |
| 56 | +... def on_exit_state(self, source: State): |
| 57 | +... if source.id == self.color: |
| 58 | +... print(f"{self.color} turning off") |
| 59 | + |
| 60 | +``` |
| 61 | + |
| 62 | +Adding a listener for each traffic light indicator |
| 63 | + |
| 64 | +``` |
| 65 | +>>> sm.add_listener(LedPanel("green"), LedPanel("yellow"), LedPanel("red")) # doctest: +ELLIPSIS |
| 66 | +TrafficLightMachine... |
| 67 | +
|
| 68 | +``` |
| 69 | + |
| 70 | +Now each "LED panel" reacts to changes in state from the state machine: |
| 71 | + |
| 72 | +```py |
| 73 | +>>> sm.cycle() |
| 74 | +yellow turning off |
| 75 | +Paulista Avenue enter: red from cycle |
| 76 | +red turning on |
| 77 | +Don't move. |
| 78 | +Paulista Avenue after: yellow--(cycle)-->red |
| 79 | +'Running cycle from yellow to red' |
| 80 | + |
| 81 | +>>> sm.cycle() |
| 82 | +red turning off |
| 83 | +Go ahead! |
| 84 | +Paulista Avenue enter: green from cycle |
| 85 | +green turning on |
| 86 | +Paulista Avenue after: red--(cycle)-->green |
| 87 | +'Running cycle from red to green' |
| 88 | + |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | +```{hint} |
| 93 | +The `StateMachine` itself is registered as a listener, so by using `listeners` an |
| 94 | +external object can have the same level of functionalities provided to the built-in class. |
| 95 | +``` |
| 96 | + |
| 97 | +```{tip} |
| 98 | +{ref}`domain models` are also registered as a listener. |
| 99 | +``` |
| 100 | + |
| 101 | + |
| 102 | +```{seealso} |
| 103 | +See {ref}`actions`, {ref}`validators and guards` for a list of possible callbacks. |
| 104 | +
|
| 105 | +And also {ref}`dynamic-dispatch` to know more about how the lib calls methods to match |
| 106 | +their signature. |
| 107 | +``` |
0 commit comments