hnit-acm/hfunc

完善状态机逻辑

github-actions opened this issue · 0 comments

完善状态机逻辑

https://github.com/hnit-acm/go-common/blob/15fae116ddaefc2d45b5ea3a6c8b221b9fa9aee7/state/state.go#L7

import (
	"github.com/hnit-acm/go-common/basic"
)

// todo 完善状态机逻辑
// labels: enhancement
// assignees: nieaowei
type EventMap map[StateKey]func(key StateKey) StateFunc

// 状态key
type StateKey interface{}

// 状态实例
type StateMachineMap basic.MapFunc

type StateFunc func()

type SetStateFunc func(key StateKey, stateFunc StateFunc)
type DelStateFunc func(key StateKey)
type ExecStateFunc func(key StateKey, sync bool) bool

type StateMachineFunc func() (SetStateFunc, ExecStateFunc, DelStateFunc)

var StateMachine = StateMachineFunc(func() (SetStateFunc, ExecStateFunc, DelStateFunc) {
	// 状态维护
	get, set, del := basic.NewSyncMapFunc(1024)
	SetStateFunc := SetStateFunc(func(key StateKey, stateFunc StateFunc) {
		set(key, stateFunc)
	})

	ExecStateFunc := ExecStateFunc(func(key StateKey, sync bool) bool {
		if sync {
			f, ok := get(key)
			if ok {
				f.(StateFunc)()
			} else {
				return ok
			}
		} else {
			f, ok := get(key)
			if ok {
				f.(StateFunc)()
			} else {
				return ok
			}
		}
		return true
	})

	DelStateFunc := DelStateFunc(func(key StateKey) {
		del(key)
	})
	return SetStateFunc, ExecStateFunc, DelStateFunc
})

22c7a3f7630908386da861eb2db19c4747415406