Giter VIP home page Giter VIP logo

statemachine-2's Introduction

Build Status   CocoaPod platform   CocoaPod version   Carthage compatible

StateMachine

A state machine implemented in Swift.

Installation

CocoaPods

  pod 'JCStateMachine', '~> 1.0.0'

Carthage

  carthage 'jbaptistecastro/StateMachine' "1.0.0"

Classes

State

A state only needs a name.

Create a State object
let myState = State("MyState")

Transition

A transition needs a name, a "from" state and a "to" state.

Create a Transition object
let fromState = State("FromState")
let toState = State("ToState")

let myTransition = Transition("MyTransition", from: fromState, to: toState)

Lifecycle Events

Before a specific transition
beforeTransition(transition)
On leaving a specific state
leaveState(state)
On entering in a specific state
onState(state)
On entering in a specific transition
onTransition(transition)

StateMachine

A state machine takes an initial State and a Transition array.

Create a StateMachine object
let stateA = State("StateA")
let stateB = State("StateB")

let transitionA = Transition("TransitionA", from: stateA, to: stateB)
let transitionB = Transition("TransitionB", from: stateB, to: stateA)

let stateMachine = StateMachine(initialState: stateA, transitions: [transitionA, transitionB]
Fire a Transition
do {
    stateMachine.fire(transition: transitionB, userInfo: nil)
} catch TransitionError.unknown {
    print("Transition unknown)
} catch TransitionError.notAllowed {
    print("Transition not allowed")
} 
Observe a lifecycle event

An event can be observed on a specific queue and can be triggered with a "userInfo" dictionary.

stateMachine.on(.onState(stateB), queue: observeQueue) { (userInfo) in
            
}

Helper functions

Compare a state with the current state
let isCurrent = stateMachine.isCurrent(state: myState)
If a transition can be fired
let canFire = stateMachine.canFire(transition: myTransition)
Get allowed transitions from the current state
let allowedTransitions = stateMachine.allowedTransitions()
Get a state from a specific name
let stateA = stateMachine.state(name: "stateA")
Get registered states
let states = stateMachine.allStates()
Get a transition from a specific name
let transitionA = stateMachine.transition(name: "transitionA")
Get registered transitions
let transitions = stateMachine.allTransitions()

Objective C compatibility

Actually, StateMachine doesn't support Objective C and there is no plan to support it.

statemachine-2's People

Contributors

jbaptistecastro avatar

Watchers

Carabineiro 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.