package statesimport (am)// DisposedStatesDef contains all the states of the Disposed state machine.// One a machine implements this state mixing, it HAS TO be disposed using the// Disposing state (instead of [am.Machine.Dispose]).//// Required states:// - StarttypeDisposedStatesDefstruct { *am.StatesBase// RegisterDisposal registers a disposal handler passed under the // DisposedArgHandler key. Requires [DisposedHandlers] to be bound prior to // the registration. Handlers registered via RegisterDisposal can block. RegisterDisposal string// Disposing starts the machine disposal - first state-based and then calls // [am.Machine.Dispose]. Disposing string// Disposed indicates that the machine has disposed allocated resources // and is ready to be garbage collected by calling [am.Machine.Dispose]. Disposed string}// DisposedGroupsDef contains all the state groups Disposed state machine.typeDisposedGroupsDefstruct{}// DisposedSchema represents all relations and properties of DisposedStates.varDisposedSchema = am.Schema{ssD.RegisterDisposal: {Multi: true},ssD.Disposing: {Remove: S{ssB.Start}},ssD.Disposed: {Remove: S{ssD.Disposing, ssB.Start}},}// EXPORTS AND GROUPSvar ( ssD = am.NewStates(DisposedStatesDef{}) sgD = am.NewStateGroups(DisposedGroupsDef{})// DisposedStates contains all the states for the Disposed machine.DisposedStates = ssD// DisposedGroups contains all the state groups for the Disposed machine.DisposedGroups = sgD)// handlers// DisposedArgHandler is the key for the disposal handler passed to the// RegisterDisposal state. It needs to contain the EXPLICIT type of// am.HandlerDispose, eg//// var dispose am.HandlerDispose = func(id string, ctx *am.StateCtx) {// // ...// }varDisposedArgHandler = "DisposedArgHandler"// DisposedHandlers handle disposal and need to be initialized manually.typeDisposedHandlersstruct {// DisposedHandlers is a list of handler for pkg/states.DisposedStates DisposedHandlers []am.HandlerDispose}func ( *DisposedHandlers) ( *am.Event) bool { , := .Args[DisposedArgHandler].(am.HandlerDispose) := && != nil// avoid errs on check mutationsif ! && !.IsCheck { := fmt.Errorf("%w: DisposedArgHandler invalid", am.ErrInvalidArgs) .Machine().AddErr(, nil) }return}func ( *DisposedHandlers) ( *am.Event) {// TODO ability to deregister a disposal handler (by ref) // TODO typed args? := .Args[DisposedArgHandler].(am.HandlerDispose) .DisposedHandlers = append(.DisposedHandlers, )}func ( *DisposedHandlers) ( *am.Event) { := .Machine() := .NewStateCtx(ssD.Disposing)// unblockgofunc() {for , := range .DisposedHandlers {if .Err() != nil {return// expired } (.Id(), ) }// TODO retries? .EvAdd1(, ssD.Disposed, nil) }()}func ( *DisposedHandlers) ( *am.Event) {go .Machine().Dispose()}
The pages are generated with Goldsv0.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.