Giter VIP home page Giter VIP logo

finite's Introduction

CocoaPods CocoaPods Install License

Finite

Finite is a simple, pure Swift finite state machine. Only exlicitly allowed transitions between states are allowed, otherwise an error will be thrown.

Finite Swift
2.0.0 2.2 and 3.0 Beta
3.x.x 3.0 and 4.0
4.x.x 5.0

Installation

Finite has no external dependencies and supports Swift Package Manager, Carthage and CocoaPods.

Swift Package Manager

import PackageDescription

let package = Package(
    name: "YourPackage",
    dependencies: [
        .package(url: "https://github.com/vknabel/Finite.git", from: "4.0.0")
    ],
    targets: [
        .target(name: "YourTarget", dependencies: ["Finite"]),
    ]
)

Carthage

github "vknabel/Finite"

CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

pod 'Finite', '~> 4.0.0'

Introduction

It operates on a given type, where each value represents an internal state of the machine. A StateMachine is defined by providing all allowed state transitions.

enum Test: Int {
    case saving, fetching, deleting
    case ready, fail
}

var machine = StateMachine<Test>(initial: .ready) { c in
    c.allow(from: [.saving, .fetching, .deleting], to: [.ready, .fail])
    c.allow(from: .ready, to: [.saving, .fetching, .deleting])
}

It is possible to provide callbacks, that will be called once certain transitions will happen.

machine.onTransitions {
    println("Successfully triggered transition!")
}
machine.onTransitions(from: .ready) {
    println("From Ready: show activity indicator")
}
machine.onTransitions(to: .ready) {
    println("To Ready: hide activity indicator")
}
machine.onTransitions(to: .saving) {
    println("To: save")
}

let subscription = machine.subscribeTransitions(to: .saving) {
    println("Only triggered as long as you keep `subscription`")
}

Once the StateMachine has been set up, you may trigger all transitions you have declared above.

try machine.transition(to: .saving) {
    println("Triggered: save")
}

// this will throw TransitionError<Test>.denied(from: .saving, to: .fetching)
try machine.transition(to: .fetching)

Author

Valentin Knabel, [email protected]

Special thanks to @snofla for allowing to export state machines to graphviz.

License

Finite is available under the MIT license.

finite's People

Contributors

snofla avatar vknabel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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