package main
import (
"context"
"time"
"github.com/pancsta/asyncmachine-go/examples/basic/states"
amhelp "github.com/pancsta/asyncmachine-go/pkg/helpers"
am "github.com/pancsta/asyncmachine-go/pkg/machine"
)
var ss = states .States
func main() {
ctx := context .Background ()
mach := am .New (nil , states .Schema , nil )
mach .BindHandlers (&Handlers {
Filename : "README.md" ,
Mach : mach ,
})
mach .Add1 (ss .ProcessingFile , nil )
err := amhelp .WaitForErrAll (ctx , 5 *time .Second , mach ,
mach .When1 (ss .FileProcessed , nil ))
if ctx .Err () != nil {
return
} else if err != nil {
println ("err: " , err )
} else {
println ("done" )
}
}
type Handlers struct {
Mach *am .Machine
Filename string
}
func (h *Handlers ) ProcessingFileEnter (e *am .Event ) bool {
return true
}
func (h *Handlers ) ProcessingFileState (e *am .Event ) {
mach := h .Mach
ctx := mach .NewStateCtx (ss .ProcessingFile )
mach .Fork (ctx , e , func () {
err := processFile (ctx , h .Filename )
if ctx .Err () != nil {
return
}
if err != nil {
mach .AddErr (err , nil )
return
}
mach .Add1 (ss .FileProcessed , nil )
})
}
func processFile(_ context .Context , _ string ) error {
time .Sleep (2 * time .Second )
return nil
}
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 .