Skip to content

Commit c60f7fe

Browse files
authored
docs: add explanation for action return values (#378)
* docs: minor fix for consistency * docs: add explanation for action return values
1 parent 0a15aa7 commit c60f7fe

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

docs/actions.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,48 @@ Actions and Guards will be executed in the following order:
333333
- `after_transition()`
334334

335335

336+
## Return values
337+
338+
Currently only certain actions' return values will be combined as a list and returned for
339+
a triggered transition:
340+
341+
- `before_transition()`
342+
343+
- `before_<event>()`
344+
345+
- `on_transition()`
346+
347+
- `on_<event>()`
348+
349+
Note that `None` will be used if the action callback does not return anything, but only when it is
350+
defined explicitly. The following provides an example:
351+
352+
```py
353+
>>> class ExampleStateMachine(StateMachine):
354+
... initial = State(initial=True)
355+
...
356+
... loop = initial.to.itself()
357+
...
358+
... def before_loop(self):
359+
... return "Before loop"
360+
...
361+
... def on_transition(self):
362+
... pass
363+
...
364+
... def on_loop(self):
365+
... return "On loop"
366+
...
367+
368+
>>> sm = ExampleStateMachine()
369+
370+
>>> sm.loop()
371+
['Before loop', None, 'On loop']
372+
373+
```
374+
375+
For {ref}`RTC model`, only the main event will get its value list, while the chained ones simply get
376+
`None` returned. For {ref}`Non-RTC model`, results for every event will always be collected and returned.
377+
336378

337379
(dynamic-dispatch)=
338380
## Dynamic dispatch
@@ -341,7 +383,7 @@ Actions and Guards will be executed in the following order:
341383
Guards. This means that you can declare an arbitrary number of `*args` and `**kwargs`, and the
342384
library will match your method signature of what's expected to receive with the provided arguments.
343385

344-
This means that if on your `on_enter_<state.id>()` or `on_execute_<event>()` method, you need to know
386+
This means that if on your `on_enter_<state.id>()` or `on_<event>()` method, you need to know
345387
the `source` ({ref}`state`), or the `event` ({ref}`event`), or access a keyword
346388
argument passed with the trigger, just add this parameter to the method and It will be passed
347389
by the dispatch mechanics.

0 commit comments

Comments
 (0)