Giter Club home page Giter Club logo

state-machine's Introduction

State Machine

Dead simple state machines in Swift.

Usage

Setup your states:

enum FetchStates: StateList {
    case idle, fetching, error
}

and your actions:

enum FetchActions: ActionList {
    case fetch, success, failure, retry, reset
}

Then create your machine:

var machine: StateMachine<FetchStates, FetchActions> = .idle <> .fetching <> .error <|> .idle

Bind your actions:

try machine.bindAction(.fetch, to: .idle)
try machine.bindAction(.success, to: .fetching, .cycle) // Go back to beginning of cycle (first <> before current state)
try machine.bindAction(.failure, to: .fetching)
try machine.bindAction(.retry, to: .error, .cycle)  // Go back to beginning of cycle (first <> before current state)
try machine.bindAction(.reset, to: .error)

Add events for your states and/or actions:

machine.addEvent(on: [.fetch, .retry]) {
    // Perform fetch operation
}
machine.addEvent(on: .success) {
    // Show data
}
machine.addEvent(on: .error) {
    // Show error
}

Send events:

try machine.send(.fetch)

And that's all it takes to get a working state machine πŸŽ‰

This state machine is equivalent to:

β”Œβ”€β”€β”€β”€β” ----fetch---> β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β” --failure-> β”Œβ”€β”€β”€β”€β”€β”
β”‚idleβ”‚               β”‚fetchingβ”‚             β”‚errorβ”‚
β””β”€β”€β”€β”€β”˜ <--success--- β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ <--retry--- β””β”€β”€β”€β”€β”€β”˜
  ⬑-------------------reset--------------------β”˜

Operators

  • |> One way state direction.

    Always goes to the next state

  • <> Two way state direction.

    Can either go to the next state, or go back to the first link in cycle (.first <> .second <> .third, cycle from .second or .third would go to .first)

  • <|> Loop state direction.

    Returns to the initial state

state-machine's People

Contributors

carson-katri avatar

Stargazers

 avatar Simon Tsai avatar Pho Hale avatar Manolis Pahlke avatar Eric Anderson avatar  avatar Hubert Schipkowski avatar Ankit Singh avatar Vedanta-krit das (Alex Vedmedenko) avatar Aidar Nugmanoff avatar Cezar Mauricio avatar  avatar

Watchers

James Cloos avatar  avatar  avatar Hubert Schipkowski avatar  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.