-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependency_graph.go
160 lines (135 loc) · 2.84 KB
/
dependency_graph.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Based on https://en.wikipedia.org/wiki/Dependency_graph
//
// This example shows how to construct both sync and async DAG dependency graphs using Require and Auto.
//
// It can be used to e.g. resolve blocking code dependencies in order.
package main
import (
"time"
"github.com/joho/godotenv"
amhelp "github.com/pancsta/asyncmachine-go/pkg/helpers"
am "github.com/pancsta/asyncmachine-go/pkg/machine"
)
func init() {
// load .env
_ = godotenv.Load()
// am-dbg is required for debugging, go run it
// go run github.com/pancsta/asyncmachine-go/tools/cmd/am-dbg@latest
// amhelp.EnableDebugging(false)
// amhelp.SetLogLevel(am.LogChanges)
}
func main() {
depGraph()
asyncDepGraph()
}
// SYNC
func depGraph() {
// init the state machine
mach := am.New(nil, am.Struct{
"A": {
Auto: true,
Require: am.S{"B", "C"},
},
"B": {
Auto: true,
Require: am.S{"D"},
},
"C": {Auto: true},
"D": {Auto: true},
"Start": {},
}, &am.Opts{LogLevel: am.LogChanges, ID: "sync"})
amhelp.MachDebugEnv(mach)
_ = mach.BindHandlers(&handlers{})
mach.Add1("Start", nil)
}
type handlers struct{}
func (h *handlers) AState(e *am.Event) {
println("A ok")
}
func (h *handlers) BState(e *am.Event) {
println("B ok")
}
func (h *handlers) CState(e *am.Event) {
println("C ok")
}
func (h *handlers) DState(e *am.Event) {
println("D ok")
}
// ASYNC
func asyncDepGraph() {
// init the state machine
mach := am.New(nil, am.Struct{
"AInit": {
Auto: true,
Require: am.S{"B", "C"},
},
"A": {
Require: am.S{"B", "C"},
},
"BInit": {
Auto: true,
Require: am.S{"D"},
},
"B": {
Require: am.S{"D"},
},
"CInit": {Auto: true},
"C": {},
"DInit": {Auto: true},
"D": {},
"Start": {},
}, &am.Opts{LogLevel: am.LogChanges, ID: "async"})
amhelp.MachDebugEnv(mach)
_ = mach.BindHandlers(&asyncHandlers{})
mach.Add1("Start", nil)
<-mach.When1("A", nil)
}
type asyncHandlers struct{}
func (h *asyncHandlers) AInitState(e *am.Event) {
// unblock
go func() {
// block
time.Sleep(100 * time.Millisecond)
// next
e.Machine.Add1("A", nil)
}()
}
func (h *asyncHandlers) BInitState(e *am.Event) {
// unblock
go func() {
// block
time.Sleep(100 * time.Millisecond)
// next
e.Machine.Add1("B", nil)
}()
}
func (h *asyncHandlers) CInitState(e *am.Event) {
// unblock
go func() {
// block
time.Sleep(100 * time.Millisecond)
// next
e.Machine.Add1("C", nil)
}()
}
func (h *asyncHandlers) DInitState(e *am.Event) {
// unblock
go func() {
// block
time.Sleep(100 * time.Millisecond)
// next
e.Machine.Add1("D", nil)
}()
}
func (h *asyncHandlers) AState(e *am.Event) {
println("A ok")
}
func (h *asyncHandlers) BState(e *am.Event) {
println("B ok")
}
func (h *asyncHandlers) CState(e *am.Event) {
println("C ok")
}
func (h *asyncHandlers) DState(e *am.Event) {
println("D ok")
}