Swiftpack.co - Package - vknabel/Finite

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 |

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", majorVersion: 3)
    ]
)

Carthage

github "vknabel/Finite"

CocoaPods

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

pod 'Finite'

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(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")
}

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 an error
try machine.transition(to: .fetching)

Author

Valentin Knabel, dev@vknabel.com

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

License

Finite is available under the MIT license.

Github

link
Stars: 13
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

3.0.3 - Nov 8, 2017

Released: 2017-10-06

Other Changes

Readme updates - @vknabel Proven Swift 4.0 support - @vknabel

3.0.2 - Sep 26, 2016

Released: 2016-09-26

Other Changes

  • Fixes Testing error - @vknabel

3.0.1 - Sep 26, 2016

Released: 2016-09-26

Other Changes

  • Added support for Travis builds - @vknabel
  • Added test support for Linux - @vknabel
  • Updated docs - @vknabel

3.0.0 - Sep 8, 2016

3.0.0

Released: 2016-09-08

Breaking Changes:

  • Dropped Swift 2.2 and 2.3 support - @vknabel

2.0.0 - Aug 22, 2016

2.0.0

Released: 2016-08-22

Breaking Changes:

  • Renamed Project from StateMachine to Finite - @vknabel
  • Renamed StateMachine.triggerTransition(to:) to StateMachine.transition(to:) - @vknabel
  • StateMachine.transition(to:) throws TransitionError and rethrows - @vknabel

API Additions:

  • Operations may now throw - @vknabel
  • Added TransitionError - @vknabel
  • Added Swift 3.0 Support - @vknabel
  • Added generated Docs - @vknabel

Other Changes:

  • Added CocoaPods and Swift Package Manager support - @vknabel
  • Started this CHANGELOG.