-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_subscriptions.go
99 lines (82 loc) · 1.97 KB
/
example_subscriptions.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
package main
import (
"context"
"errors"
"fmt"
"sync"
"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() {
ctx := context.Background()
// init state machines
mach := am.New(ctx, am.Struct{
"Foo": {},
"Bar": {},
"Healthcheck": {Multi: true},
}, &am.Opts{LogLevel: am.LogOps, ID: "source"})
amhelp.MachDebugEnv(mach)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
go wg.Done()
// wait until FileDownloaded becomes active
<-mach.When1("Foo", nil)
fmt.Println("1 - Foo activated")
}()
wg.Add(1)
go func() {
go wg.Done()
// wait until FileDownloaded becomes inactive
<-mach.WhenNot1("Bar", nil)
fmt.Println("2 - Bar deactivated")
}()
wg.Add(1)
go func() {
go wg.Done()
// wait for EventConnected to be activated with an arg ID=123
<-mach.WhenArgs("Bar", am.A{"ID": 123}, nil)
fmt.Println("3 - Bar activated with ID=123")
}()
wg.Add(1)
go func() {
go wg.Done()
// wait for Foo to have a tick >= 1 and Bar tick >= 3
<-mach.WhenTime(am.S{"Foo", "Bar"}, am.Time{1, 3}, nil)
fmt.Println("4 - Foo tick >= 1 and Bar tick >= 3")
}()
wg.Add(1)
go func() {
go wg.Done()
// wait for DownloadingFile to have a tick increased by 2 since now
<-mach.WhenTicks("Foo", 2, nil)
fmt.Println("5 - Foo tick increased by 2 since now")
}()
wg.Add(1)
go func() {
go wg.Done()
// wait for an error
<-mach.WhenErr(ctx)
fmt.Println("6 - Error")
}()
wg.Wait()
mach.Add1("Foo", nil)
mach.Add1("Bar", nil)
mach.Remove1("Bar", nil)
mach.Remove1("Foo", nil)
mach.Add1("Foo", nil)
mach.Add1("Bar", am.A{"ID": 123})
mach.AddErr(errors.New("err"), nil)
// wait
time.Sleep(time.Second)
}