-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathtransition.py
More file actions
75 lines (63 loc) · 2.04 KB
/
transition.py
File metadata and controls
75 lines (63 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from typing import TYPE_CHECKING, Any, Callable, List, NamedTuple, Optional, Union
from xstate.action import Action
from xstate.event import Event
if TYPE_CHECKING:
from xstate.state_node import StateNode
CondFunction = Callable[[Any, Event], bool]
class TransitionConfig(NamedTuple):
target: List[str]
class Transition:
event: str
source: "StateNode"
config: Union[str, "StateNode", TransitionConfig]
actions: List[Action]
cond: Optional[CondFunction]
order: int
# "internal" or "external"
type: str
def __init__(
self,
config,
source: "StateNode",
event: str,
order: int,
cond: Optional[CondFunction] = None,
):
self.event = event
self.config = config
self.source = source
self.type = "external"
self.cond = config.get("cond", None) if isinstance(config, dict) else None
self.order = order
self.actions = (
(
[
Action(type=action.get("type"), data=action)
for action in config.get("actions", [])
]
)
if isinstance(config, dict)
else []
)
@property
def target(self) -> List["StateNode"]:
if isinstance(self.config, str):
return [self.source._get_relative(self.config)]
elif isinstance(self.config, dict):
if isinstance(self.config["target"], str):
return [self.source._get_relative(self.config["target"])]
return [self.source._get_relative(v) for v in self.config["target"]]
else:
return [self.config] if self.config else []
def __repr__(self) -> str:
return repr(
{
"event": self.event,
"source": self.source.id,
"target": [f"#{t.id}" for t in self.target],
"cond": self.cond,
"actions": self.actions,
"type": self.type,
"order": self.order,
}
)