Skip to content

参考了alibaba/COLA/cola-component-statemachine 的实现,Golang 版状态机组件

Notifications You must be signed in to change notification settings

GoCarnival/go-statemachine

Repository files navigation

go-statemachine

参考了alibaba/COLA/cola-component-statemachine 的实现

Install

go get github.com/GoCarnival/go-statemachine

Usage

package main

import (
	"github.com/GoCarnival/go-statemachine"
	"fmt"
)

func main() {
	builder := statemachine.Builder[string, string, any]{}
	builder.ExternalTransition().From("foo").To("bar").On("ping").When(func(ctx *any) bool {
		return true
	}).Perform(func(from string, to string, event string, context *any) {
		fmt.Println("do something")
	})
	var fetcher statemachine.CurrentStateFetcher[string, any] = func(ctx *any) string {
		return "foo"
	}
	builder.SetCurrentStateFetcher(fetcher)
	s := builder.Build("test")
	target := s.FireEvent("foo", "ping", nil)
	fmt.Println(target)
	s.Verify("foo", "ping")
	s.FireEventByFetcher("ping", nil)
	s.VerifyWithFetcher("ping", nil)
}

About

参考了alibaba/COLA/cola-component-statemachine 的实现,Golang 版状态机组件

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages