Git Product home page Git Product logo

go-statemachine's Introduction

go-statemachine

A generic state machine

Table of Contents

Description

The module provides tools for defining and tracking generic state machines. For a more specific implementation that specifically defines finite state machines, see the FSM module, that provides additional tools on top of the generic machines defined here

State machines:

  • Receive Events
  • Modify State
  • Take further actions based on new state, possibly generating more events

Usage

A state machine is defined in terms of a Planner.

It has the following signature:

type Planner func(events []Event, user interface{}) (nextActions interface{}, eventsProcessed uint64, err error)

A planner receives a series of events and a pointer to the current state (represented by user)

The planner generally:

  • processes one or more events that mutate state
  • constructs a function that will perform additional actions based on the new state

It returns:

  1. The next actions handler-- should have the signature func(ctx Context, st ) error), where is the typeOf(user) param
  2. The number of events processed
  3. An error if occured

As a general rule, you mutate inside the planner function, while you perform side effects outside the planner in the return handler, which only receives a value for state and cannot mutate it (but can dispatch more events).

Interaction with statestore

The go-statemachine is designed to be used with a list of data structures, persisted to a datastore through the go-statestore module. When working with statemachines this way, you define a StateHandler with a Plan function that works like a Planner.

You initialize a new set of statemachines for a given type of state as follows:

var ds datastore.Batching
var stateHandler StateHandler

var stateMachines = statemachine.New(ds, stateHandler, StateType{})

This creates a machine for the given data store that will process data of type StateType with the given stateHandler.

You can now dispatch events to a state machine with:

var id interface{} // some identifier of a tracked state
var evt Event // some event type
stateMachines.Send(id, evt)

The state machine group will initialize a new statemachine if it is not already tracking data for the given identifier

License

Dual-licensed under MIT + Apache 2.0

go-statemachine's People

Contributors

aarshkshah1992 avatar gammazero avatar hannahhoward avatar ingar avatar laser avatar magik6k avatar stebalien avatar tchardin 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.