Skip to content

Commit

Permalink
feat(states): add pkg/states (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
pancsta authored Sep 11, 2024
1 parent ea47293 commit 5175956
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions pkg/states/ss_basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package states

import am "github.com/pancsta/asyncmachine-go/pkg/machine"

// S is a type alias for a list of state names.
type S = am.S

// State is a type alias for a single state definition.
type State = am.State

// SAdd is a func alias for adding lists of states to a list of states.
var SAdd = am.SAdd

// SExtend is a func alias for extending an existing state definition.
var SExtend = am.StateAdd

// StructMerge is a func alias for extending an existing state structure.
var StructMerge = am.StructMerge

// States map defines relations and properties of states.
var States = am.Struct{
// Errors

ErrNetwork: {Require: S{am.Exception}},

Start: {},
Ready: {},

// Disconnected -> Connected

Connecting: {
Require: S{Start},
Remove: GroupConnected,
},
Connected: {
Require: S{Start},
Remove: GroupConnected,
},
Disconnecting: {Remove: GroupConnected},
Disconnected: {
Auto: true,
Remove: GroupConnected,
},
}

// Groups of mutually exclusive states.

var (
GroupConnected = S{Connecting, Connected, Disconnecting, Disconnected}
)

// #region boilerplate defs

// Names of all the states (pkg enum).

const (
ErrNetwork = "ErrNetwork"

Start = "Start"
Ready = "Ready"

Connecting = "Connecting"
Connected = "Connected"
Disconnecting = "Disconnecting"
Disconnected = "Disconnected"
)

// Names is an ordered list of all the state names.
var Names = S{
am.Exception,

ErrNetwork,

Start,
Ready,

Connecting,
Connected,
Disconnecting,
Disconnected,
}

// #endregion

0 comments on commit 5175956

Please sign in to comment.