Giter Site home page Giter Site logo

finist.lua's Introduction

Finist

Redis based Finite State Machine.

Description

Finist is a finite state machine that is defined and persisted in Redis.

Community

Meet us on IRC: #lesscode on freenode.net.

Related projects

Usage

You need to supply a Redis client. There are no restrictions regarding the type of the Redis client, but it must respond to call and the signature must be identical to that of RESP.

local resp = require("resp")
local client = resp.new("127.0.0.1", 6379)

local finist = require("finist")

-- Initialize with a Redis client, the name of the machine and the
-- initial state. In this example, the machine is called "order" and
-- the initial status is "pending". The Redis client is connected to
-- the default host (127.0.0.1:6379).
local machine = finist.new(resp, "order", "pending")

Now you can define the available transitions:

-- Available transitions are defined with the `on` method
-- `machine:on(<event>, <initial_state>, <final_state>)`
machine:on("approve", "pending", "approved")
machine:on("cancel", "pending", "cancelled")
machine:on("cancel", "approved", "cancelled")
machine:on("reset", "cancelled", "pending")

Now that the possible transitions are defined, we can check the current state:

machine:state()
# => "pending"

And we can trigger an event:

machine:trigger("approve")
# => true, "approved"

The trigger method returns two values: the first represents the current state, and the second represents whether a transition occurred.

Here's what happens if an event doesn't cause a transition:

machine:trigger("reset")
# => false, "approved"

Here's a convenient way to use this flag:

local state, changed = machine:trigger("reset")

if changed then
  print("State changed to " .. state)
end

If you need to remove all the transitions for a given event, you can use rm:

machine:rm("reset")

Note that every change is persisted in Redis.

Installation

You need to have lsocket installed, then just copy finist.lua anywhere in your package.path.

A packages file is provided in case you want to use pac to install the dependencies. Follow the instructions in pac's documentation to get started.

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.