Skip to content

stephansmolek/simplestate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple state machine

Overview

This is a simple state machine with generic types for event and state identifiers. Possible state transitions as well as a start state must be given. Same state transitions can be allowed or forbidden depending upon configuration. A callback after a transition is available.

How to use

  1. Create state and event types, for example:
type (
	ExampleState string
	ExampleEvent string
)
  1. Define available state identifiers:
var (
    exampleStateA1 ExampleState = "example-state-A1"
    exampleStateA2 ExampleState = "example-state-A2"
    exampleStateB ExampleState = "example-state-B"
    exampleStateC ExampleState = "example-state-C"
)
  1. Define available event identifiers:
var (
    exampleEventA ExampleEvent = "example-event-A"
    exampleEventB ExampleEvent = "example-event-B"
)
  1. Define available events:
var events = []Event[ExampleState, ExampleEvent] {
    {
        Event: exampleEventA,
        Sources: []ExampleState {
            exampleStateA1,
            exampleStateA2,
        },
        Destination: exampleStateB,
    },
    {
        Event: exampleEventB,
        Sources: []ExampleState {
            exampleStateB,
        },
        Destination: exampleStateC,
    },
}
  1. Create settings:
var settings = Settings{
		AllowSameStateTransition: false,
	}
  1. Define callback function:
var callback = func(transition Transition[ExampleState, ExampleEvent]) {
		...
	}
  1. Create state machine:
var machine = NewMachine(exampleStateA1, events, settings, callback)
  1. Start triggering events:
err := machine.Trigger(exampleEventA); if err != nil {
    ...
}

About

A Simple State Machine for Go

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages