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() {
_ = godotenv .Load ()
}
func main() {
depGraph ()
asyncDepGraph ()
}
func depGraph() {
mach := am .New (nil , am .Schema {
"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" )
}
func asyncDepGraph() {
mach := am .New (nil , am .Schema {
"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 ) {
go func () {
time .Sleep (100 * time .Millisecond )
e .Machine ().Add1 ("A" , nil )
}()
}
func (h *asyncHandlers ) BInitState (e *am .Event ) {
go func () {
time .Sleep (100 * time .Millisecond )
e .Machine ().Add1 ("B" , nil )
}()
}
func (h *asyncHandlers ) CInitState (e *am .Event ) {
go func () {
time .Sleep (100 * time .Millisecond )
e .Machine ().Add1 ("C" , nil )
}()
}
func (h *asyncHandlers ) DInitState (e *am .Event ) {
go func () {
time .Sleep (100 * time .Millisecond )
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" )
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .