Skip to content

dependency injection to simplify event functions #714

@patrickwolf

Description

@patrickwolf

You have an idea for a feature that would make transitions more helpful or easier to use? Great! We are looking forward to your suggestion.

Is your feature request related to a problem? Please describe.
the events passed to a function need to be parsed which adds complexity to the functions

Describe the solution you'd like
using dependency injection would allow for the parsing to be done by transitions

Additional context
I like how its approached here for python statemachine
Since its all open source maybe there can even be collaboration around the dependency injection code which would make it an easy feature

You can pass arbitrary positional or keyword arguments to the event, and they will be propagated to all actions and callbacks using something similar to dependency injection. In other words, the library will only inject the parameters declared on the callback method.

Note how before_cycle was declared:

def before_cycle(self, event: str, source: State, target: State, message: str = ""):
    message = ". " + message if message else ""
    return f"Running {event} from {source.id} to {target.id}{message}"

The params event, source, target (and others) are available built-in to be used on any action. The param message is user-defined, in our example we made it default empty so we can call cycle with or without a message parameter.

If we pass a message parameter, it will be used on the before_cycle action:

sm.send("cycle", message="Please, now slowdown.")
'Running cycle from green to yellow. Please, now slowdown.'

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions