Note
asyncmachine-go is a declarative control flow library implementing AOP and Actor Model through a clock-based state machine.
/pkg/states contains common state definitions to make state-based API easier to compose and exchange. Additionally it offers tooling for "piping" states between state machines.
- BasicStatesDef: Start, Ready, Healthcheck
- ConnectedStatesDef: Client connection in 4 states
- DisposedStatesDef: Async disposal
import ssam "github.com/pancsta/asyncmachine-go/pkg/states"
// inherit from RPC worker
ssStruct := am.StructMerge(ssam.BasicStruct, am.Struct{
"Foo": {Require: am.S{"Bar"}},
"Bar": {},
})
ssNames := am.SAdd(ssam.BasicStates.Names(), am.S{"Foo", "Bar"})
// MyMachStatesDef contains all the states of the MyMach state machine.
type MyMachStatesDef struct {
*am.StatesBase
State1 string
State2 string
// inherit from BasicStatesDef
*ss.BasicStatesDef
}
// MyMachStruct represents all relations and properties of MyMachStates.
var MyMachStruct = StructMerge(
// inherit from BasicStruct
ss.BasicStruct,
am.Struct{
ssM.State1: {},
ssM.State2: {
Multi: true,
},
})
Inherit from BasicStatesDef via the generator
$ am-gen --name MyMach \
--states State1,State2 \
--inherit basic
A "pipe" binds a handler of a source machine, to a mutation in a target machine. Currently, only final handlers are supported.
Each module can export their own pipes, like /pkg/rpc
and /pkg/node
.
BindConnected
BindErr
BindReady
import ampipe "github.com/pancsta/asyncmachine-go/pkg/states/pipes"
ampipe.BindReady(rpcClient.Mach, myMach, "RpcReady", "")
var source *am.Machine
var target *am.Machine
h := &struct {
ReadyState am.HandlerFinal
ReadyEnd am.HandlerFinal
}{
ReadyState: Add1(source, target, "Ready", "RpcReady"),
ReadyEnd: Remove1(source, target, "Ready", "RpcReady"),
}
source.BindHandlers(h)
The purpose of this package is to share reusable states, but inheriting a definition isn't enough. States can also be
extended on relations-level using helpers from states_utils.go
(generated), although sometimes it's better to override
a state (eg Ready).
import (
am "github.com/pancsta/asyncmachine-go/pkg/machine"
"github.com/pancsta/asyncmachine-go/pkg/states"
ampipe "github.com/pancsta/asyncmachine-go/pkg/states/pipes"
)
// ...
MyMychStruct = am.Struct{
// inject Client states into Connected
"Connected": StateAdd(
states.ConnectedStruct[states.ConnectedStates.Connected],
am.State{
Remove: S{"RetryingConn"},
Add: S{"Handshaking"},
}),
}
Testing, semantically versioned.
Go back to the monorepo root to continue reading.