MCPcopy
hub / github.com/looplab/fsm

github.com/looplab/fsm @v1.0.3 sqlite

repository ↗ · DeepWiki ↗ · release v1.0.3 ↗
136 symbols 568 edges 18 files 45 documented · 33%
README

PkgGoDev Bulid Status Coverage Status Go Report Card

FSM for Go

FSM is a finite state machine for Go.

It is heavily based on two FSM implementations:

  • Javascript Finite State Machine, https://github.com/jakesgordon/javascript-state-machine

  • Fysom for Python, https://github.com/oxplot/fysom (forked at https://github.com/mriehl/fysom)

For API docs and examples see http://godoc.org/github.com/looplab/fsm

Basic Example

From examples/simple.go:

package main

import (
    "context"
    "fmt"

    "github.com/looplab/fsm"
)

func main() {
    fsm := fsm.NewFSM(
        "closed",
        fsm.Events{
            {Name: "open", Src: []string{"closed"}, Dst: "open"},
            {Name: "close", Src: []string{"open"}, Dst: "closed"},
        },
        fsm.Callbacks{},
    )

    fmt.Println(fsm.Current())

    err := fsm.Event(context.Background(), "open")
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(fsm.Current())

    err = fsm.Event(context.Background(), "close")
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(fsm.Current())
}

Usage as a struct field

From examples/struct.go:

package main

import (
    "context"
    "fmt"

    "github.com/looplab/fsm"
)

type Door struct {
    To  string
    FSM *fsm.FSM
}

func NewDoor(to string) *Door {
    d := &Door{
        To: to,
    }

    d.FSM = fsm.NewFSM(
        "closed",
        fsm.Events{
            {Name: "open", Src: []string{"closed"}, Dst: "open"},
            {Name: "close", Src: []string{"open"}, Dst: "closed"},
        },
        fsm.Callbacks{
            "enter_state": func(_ context.Context, e *fsm.Event) { d.enterState(e) },
        },
    )

    return d
}

func (d *Door) enterState(e *fsm.Event) {
    fmt.Printf("The door to %s is %s\n", d.To, e.Dst)
}

func main() {
    door := NewDoor("heaven")

    err := door.FSM.Event(context.Background(), "open")
    if err != nil {
        fmt.Println(err)
    }

    err = door.FSM.Event(context.Background(), "close")
    if err != nil {
        fmt.Println(err)
    }
}

License

FSM is licensed under Apache License 2.0

http://www.apache.org/licenses/LICENSE-2.0

Extension points exported contracts — how you extend this code

Callback (FuncType)
Callback is a function type that callbacks should use. Event is the current event info as the callback happens.
fsm.go

Core symbols most depended-on inside this repo

Error
called by 74
errors.go
Event
called by 68
fsm.go
Current
called by 54
fsm.go
NewFSM
called by 46
fsm.go
Err
called by 16
uncancel_context.go
Done
called by 15
uncancel_context.go
Transition
called by 7
fsm.go
Async
called by 6
event.go

Shape

Function 77
Method 36
Struct 17
TypeAlias 4
FuncType 1
Interface 1

Languages

Go100%

Modules by API surface

fsm_test.go43 symbols
fsm.go28 symbols
errors.go19 symbols
mermaid_visualizer.go8 symbols
errors_test.go8 symbols
uncancel_context.go5 symbols
graphviz_visualizer.go5 symbols
visualizer.go4 symbols
examples/struct.go4 symbols
event.go3 symbols
mermaid_visualizer_test.go2 symbols
uncancel_context_test.go1 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add fsm \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact