Git Product home page Git Product logo

swiftstate's Introduction

SwiftState

Elegant state machine for Swift.

SwiftState

Example

enum MyState: StateType {
    case State0, State1, State2
    case AnyState   // create case=Any

    init(nilLiteral: Void) {
        self = AnyState
    }
}
let machine = StateMachine<MyState, MyEvent>(state: .State0) { machine in

    machine.addRoute(.State0 => .State1)
    machine.addRoute(nil => .State2) { context in print("Any => 2, msg=\(context.userInfo)") }
    machine.addRoute(.State2 => nil) { context in print("2 => Any, msg=\(context.userInfo)") }

    // add handler (handlerContext = (event, transition, order, userInfo))
    machine.addHandler(.State0 => .State1) { context in
        print("0 => 1")
    }

    // add errorHandler
    machine.addErrorHandler { (event, transition, order, userInfo) in
        print("[ERROR] \(transition.fromState) => \(transition.toState)")
    }
}

// tryState 0 => 1 => 2 => 1 => 0
machine <- .State1
machine <- (.State2, "Hello")
machine <- (.State1, "Bye")
machine <- .State0  // fail: no 1 => 0

print("machine.state = \(machine.state)")

This will print:

0 => 1
Any => 2, msg=Hello
2 => Any, msg=Bye
[ERROR] 1 => 0
machine.state = 1

For more examples, please see XCTest cases.

Features

  • Easy Swift syntax
    • Transition: .State0 => .State1, [.State0, .State1] => .State2
    • Try transition: machine <- .State1
    • Try transition + messaging: machine <- (.State1, "GoGoGo")
    • Try event: machine <-! .Event1
  • Highly flexible transition routing
    • using Condition
    • using AnyState (nil state)
    • or both (blacklisting): nil => nil + condition
  • Success/Error/Entry/Exit handlers with order: UInt8 (no before/after handler stuff)
  • Removable routes and handlers
  • Chaining: .State0 => .State1 => .State2
  • Event: machine.addRouteEvent("WakeUp", transitions); machine <-! "WakeUp"
  • Hierarchical State Machine: #10

Terms

Term Class Description
State StateType (protocol) Mostly enum, describing each state e.g. .State0.
Event StateEventType (protocol) Name for route-group. Transition can be fired via Event instead of explicitly targeting next State.
Machine StateMachine State transition manager which can register Route and Handler separately for variety of transitions.
Transition StateTransition From- and to- states represented as .State1 => .State2. If nil is used for either state, it will be represented as .AnyState.
Route StateRoute Transition + Condition.
Condition Transition -> Bool Closure for validating transition. If condition returns false, transition will fail and associated handlers will not be invoked.
Handler HandlerContext -> Void Transition callback invoked after state has been changed.
Chain StateTransitionChain Group of continuous routes represented as .State1 => .State2 => .State3

Related Articles

  1. Swiftで有限オートマトン(ステートマシン)を作る - Qiita (Japanese)
  2. Swift+有限オートマトンでPromiseを拡張する - Qiita (Japanese)

Licence

MIT

swiftstate's People

Contributors

inamiy avatar adamyanalunas avatar nwest avatar

Watchers

James Cloos avatar Artur Cygan 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.