-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransition_actions.go
64 lines (48 loc) · 1.23 KB
/
transition_actions.go
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
package persephone
import (
"fmt"
)
type TransitionActions struct {
actionMap map[State]*TargetActionMap
}
func newTransitionActions() *TransitionActions {
return &TransitionActions{
actionMap: make(map[State]*TargetActionMap),
}
}
func (ta *TransitionActions) addMap(src, tgt State, act Callback) (err error) {
sta, _ := ta.getMapBySource(src)
if sta != nil {
sta.addAction(tgt, act)
return
}
m := newTargetActionMap()
m.addAction(tgt, act)
ta.actionMap[src] = m
return
}
func (ta *TransitionActions) getMapBySource(src State) (*TargetActionMap, error) {
v, ok := ta.actionMap[src]
if ok {
return v, nil
}
return nil, fmt.Errorf("No TargetAction Map found for Source State: %d", src)
}
type TargetActionMap struct {
actionMap map[State][]Callback
}
func newTargetActionMap() *TargetActionMap {
return &TargetActionMap{
actionMap: make(map[State][]Callback),
}
}
func (ta *TargetActionMap) addAction(state State, action Callback) {
ta.actionMap[state] = append(ta.actionMap[state], action)
}
func (ta *TargetActionMap) getActionsByTarget(state State) ([]Callback, error) {
v, ok := ta.actionMap[state]
if !ok {
return nil, fmt.Errorf("No TargetAction Map found for Target State: %d", state)
}
return v, nil
}