Giter VIP home page Giter VIP logo

go-state-machine's Introduction

Go State Machine

This repository contains a Go implementation of a state machine. The state machine is managed using a state manager that can handle multiple events and states simultaneously.

Requirements

  • Go version 1.18 or higher

Installation

As a library

go get github.com/gavinin/go-state-machine

Usage

NewStateManager()

This method is used to create and initialize a new instance of StateManager.

StateManager supports generics, the following documents will be replaced by the Example class.

stateManager := state.NewStateManager[Example]()

SetDuration(d time.Duration)

This method is used to set the duration or frequency at which the StateManager updates its states.

stateManager.SetDuration(10 * time.Millisecond)

NewStateManagerEvent(eventType EventType, id string, events chan Test, duration time.Duration, callback CallbackFunc) *Event

This method is used to create a new state event.

All data exchange of StateManager is completed through NewStateManagerEvent.

Whenever a new request is added, it needs to be completed through an event.

event := stateManager.NewStateManagerEvent(state.ADD, strconv.Itoa(i), tests, duration, 
	func(events chan Example) {
	events <- Example{
	}
})

Event Type

The following are all supported EventTypes.

const (
	ADD     EventType = "ADD"
	DEL     EventType = "DEL"
	RESET   EventType = "RESET"
	PAUSE   EventType = "PAUSE"
	RUNNING EventType = "RUNNING"
)
  • ADD: Add a status
  • DEL: Delete a status
  • RESET: Reset timer
  • PAUSE: Pause this status
  • RUNNING: This status is running

SendEvent(event Event)

This method is used to send an event to the StateManager.

stateManager.SendEvent(event)

Demo

Demo

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

go-state-machine's People

Contributors

gavinin avatar gavinswork avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.