You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The action will be registered for every {ref}`transition` associated with the event.
219
+
220
+
221
+
```py
222
+
>>>from statemachine import StateMachine, State
223
+
224
+
>>>classExampleStateMachine(StateMachine):
225
+
... initial = State("Initial", initial=True)
226
+
...
227
+
... loop = initial.to.itself()
228
+
...
229
+
...@loop.before
230
+
...defjust_before(self):
231
+
...pass
232
+
...
233
+
...@loop.on
234
+
...defits_happening(self):
235
+
...pass
236
+
...
237
+
...@loop.after
238
+
...defloop_completed(self):
239
+
...pass
30
240
31
-
-`on_enter_<state_identifier>(event_data)`
241
+
```
32
242
33
-
-`on_exit_<state_identifier>(event_data)`
243
+
### Declare an event while also giving an "on" action using the decorator syntax
34
244
245
+
You can also declare an event while also adding a callback:
35
246
36
-
The initial {ref}`state` is entered when the machine starts and the corresponding actions `on_enter_state` or `on_enter_<state>` are called if defined.
247
+
```py
248
+
>>>from statemachine import StateMachine, State
37
249
38
-
## Event actions
250
+
>>>classExampleStateMachine(StateMachine):
251
+
... initial = State("Initial", initial=True)
252
+
...
253
+
...@initial.to.itself()
254
+
...defloop(self):
255
+
...print("On loop")
256
+
...return42
257
+
258
+
```
39
259
40
-
For each event, you can register `before_<event>` and `after_<event>`
260
+
Note that with this syntax, the result `loop` that is present on the `ExampleStateMachine.loop`
261
+
namespacte is not a simple method, but an {ref}`event` trigger. So it only executes if the
262
+
StateMachine is on the right state.
41
263
42
-
-`before_<event>(event_data)`
264
+
So, you can use the event-oriented approach:
43
265
44
-
-`after_<event>(event_data)`
266
+
```py
267
+
>>> sm = ExampleStateMachine()
268
+
269
+
>>> sm.send("loop")
270
+
On loop
271
+
42
272
+
273
+
```
45
274
46
275
47
276
## Other callbacks
@@ -93,7 +322,7 @@ python-statemachine implements a custom dispatch mechanism on all those availabl
93
322
Guards, this means that you can declare an arbitrary number of `*args` and `**kwargs`, and the
94
323
library will match your method signature of what's expect to receive with the provided arguments.
95
324
96
-
This means that if on your `on_enter_<state>()` or `on_execute_<event>()` method, you need to know
325
+
This means that if on your `on_enter_<state.id>()` or `on_execute_<event>()` method, you need to know
97
326
the `source` ({ref}`state`), or the `event` ({ref}`event`), or access a keyword
98
327
argument passed with the trigger, just add this parameter to the method and It will be passed
0 commit comments