# go.mod -rw-r--r-- 87 bytes View raw
                                                                                
1
2
3
4
5
6
module blubb

go 1.17

require git.sr.ht/~minus/fsm v0.0.0-20200805181917-e88e4360950d
# go.sum -rw-r--r-- 215 bytes View raw
                                                                                
1
2
3
git.sr.ht/~minus/fsm v0.0.0-20200805181917-e88e4360950d h1:zNKWTyvreR0XZYMaY7do3uJM6PXnZNa7QRTqEznnGt0=
git.sr.ht/~minus/fsm v0.0.0-20200805181917-e88e4360950d/go.mod h1:T+K2DMhG5tCbHSsJIHd2+E51N7I4YOwR+NZVPvN8DLI=
# Makefile -rw-r--r-- 29 bytes View raw
                                                                                
1
2
3
all:
	go run -gcflags=-G=3 .
# main.go -rw-r--r-- 559 bytes View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main

import (
	"fmt"
	"git.sr.ht/~minus/fsm"
)

func cast[A any](handler func(event string, arg A)) func(event string, arg interface{}) {
	return func(event string, arg interface{}) {
		handler(event, arg.(A))
	}
}

func main() {
	m := fsm.FSM{
		State: "stopped",
		Transitions: []fsm.Transition{
			{"stopped", "start", "started", cast(func(event string, arg int) { x := arg + 1; fmt.Printf("starting... %d\n", x) })},
		},
	}
	newState, err := m.Process("start", 1337)
	if err != nil {
		panic(err)
	}
	fmt.Printf("final state: %v\n", newState)
}