-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow using events on callbacks
- Loading branch information
Showing
7 changed files
with
179 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,77 @@ | ||
from dataclasses import dataclass | ||
from dataclasses import field | ||
from typing import TYPE_CHECKING | ||
from typing import Any | ||
|
||
if TYPE_CHECKING: | ||
from .state import State | ||
from .statemachine import StateMachine | ||
from .transition import Transition | ||
|
||
|
||
@dataclass | ||
class TriggerData: | ||
machine: "StateMachine" | ||
event: str | ||
"""The Event that was triggered.""" | ||
|
||
model: Any = field(init=False) | ||
"""A reference to the underlying model that holds the current State.""" | ||
|
||
args: tuple = field(default_factory=tuple) | ||
"""All positional arguments provided on the Event.""" | ||
|
||
kwargs: dict = field(default_factory=dict) | ||
"""All keyword arguments provided on the Event.""" | ||
|
||
def __post_init__(self): | ||
self.model = self.machine.model | ||
|
||
|
||
@dataclass | ||
class EventData: | ||
def __init__(self, machine: "StateMachine", event: str, *args, **kwargs): | ||
self.machine = machine | ||
self.event = event | ||
self.source = kwargs.get("source", None) | ||
self.state = kwargs.get("state", None) | ||
self.model = kwargs.get("model", None) | ||
self.executed = False | ||
self.transition: Transition | None = None | ||
self.target = None | ||
self._set_transition(kwargs.get("transition", None)) | ||
|
||
# runtime and error | ||
self.args = args | ||
self.kwargs = kwargs | ||
self.error = None | ||
self.result = None | ||
|
||
def __repr__(self): | ||
return f"{type(self).__name__}({self.__dict__!r})" | ||
|
||
def _set_transition(self, transition: "Transition"): | ||
self.transition = transition | ||
self.target = getattr(transition, "target", None) | ||
trigger_data: TriggerData | ||
transition: "Transition" | ||
"""The Transition instance that was activated by the Event.""" | ||
|
||
state: "State" = field(init=False) | ||
"""The current State of the state machine.""" | ||
|
||
source: "State" = field(init=False) | ||
"""The State the state machine was in when the Event started.""" | ||
|
||
target: "State" = field(init=False) | ||
"""The destination State of the transition.""" | ||
|
||
result: Any | None = None | ||
executed: bool = False | ||
|
||
def __post_init__(self): | ||
self.state = self.transition.source | ||
self.source = self.transition.source | ||
self.target = self.transition.target | ||
|
||
@property | ||
def machine(self): | ||
return self.trigger_data.machine | ||
|
||
@property | ||
def event(self): | ||
return self.trigger_data.event | ||
|
||
@property | ||
def args(self): | ||
return self.trigger_data.args | ||
|
||
@property | ||
def extended_kwargs(self): | ||
kwargs = self.kwargs.copy() | ||
kwargs = self.trigger_data.kwargs.copy() | ||
kwargs["event_data"] = self | ||
kwargs["event"] = self.event | ||
kwargs["source"] = self.source | ||
kwargs["state"] = self.state | ||
kwargs["model"] = self.model | ||
kwargs["machine"] = self.trigger_data.machine | ||
kwargs["event"] = self.trigger_data.event | ||
kwargs["model"] = self.trigger_data.model | ||
kwargs["transition"] = self.transition | ||
kwargs["state"] = self.state | ||
kwargs["source"] = self.source | ||
kwargs["target"] = self.target | ||
return kwargs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters