-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
113 lines (99 loc) · 2.39 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package micromachine
import (
"fmt"
"sort"
)
type MicroMachine struct {
State string
TransitionsFor map[string]map[string]string
Callbacks map[string][]Callback
}
type Transitions map[string]string
type Callback func(event string, payload ...string)
func NewMicroMachine(initialState string) *MicroMachine {
m := MicroMachine{State: initialState}
m.TransitionsFor = map[string]map[string]string{}
m.Callbacks = map[string][]Callback{}
return &m
}
func (m *MicroMachine) On(key string, c Callback) {
m.Callbacks[key] = append(m.Callbacks[key], c)
}
func (m *MicroMachine) When(event string, transitions map[string]string) {
m.TransitionsFor[event] = transitions
}
func (m *MicroMachine) canTrigger(event string) bool {
if _, ok := m.TransitionsFor[event]; ok {
_, ok := m.TransitionsFor[event][m.State]
return ok
}
return false
}
func (m *MicroMachine) Trigger(event string, payload ...string) bool {
safePayload := ""
if len(payload) > 0 {
safePayload = payload[0]
}
if m.canTrigger(event) {
return m.change(event, safePayload)
} else {
return false
}
}
func (m *MicroMachine) TriggerOrRaise(event string, payload ...string) error {
safePayload := ""
if len(payload) > 0 {
safePayload = payload[0]
}
if m.Trigger(event, safePayload) {
return nil
} else {
return fmt.Errorf("Event '%s' not valid from state '%s'", event, m.State)
}
}
func (m *MicroMachine) Events() []string {
keys := make([]string, 0, len(m.TransitionsFor))
for k := range m.TransitionsFor {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func (m *MicroMachine) TriggerableEvents() (triggerables []string) {
for _, e := range m.Events() {
if m.canTrigger(e) {
triggerables = append(triggerables, e)
}
}
sort.Strings(triggerables)
return
}
func (m *MicroMachine) States() []string {
in := []string{}
for _, transitions := range m.TransitionsFor {
for a, b := range transitions {
in = append(in, a, b)
}
}
sort.Strings(in)
j := 0
for i := 1; i < len(in); i++ {
if in[j] == in[i] {
continue
}
j++
in[j] = in[i]
}
states := in[:j+1]
return states
}
func (m *MicroMachine) change(event, payload string) bool {
m.State = m.TransitionsFor[event][m.State]
var callbacks []Callback
callbacks = append(callbacks, m.Callbacks["any"]...)
callbacks = append(callbacks, m.Callbacks[m.State]...)
for _, c := range callbacks {
c(event, payload)
}
return true
}