Finite

4.0.0

Finite is a simple, pure Swift finite state machine.
vknabel/Finite

What's New

2020-01-09T18:13:30Z

The new Finite offers you two new features: temporarily subscribing transitions and observing any transitions at once.

Use the new onTransitions(perform:) or subscribeTransitions(perform:) methods to observe all transitions.
Temporarily subscribeTransitions of your State Machines and bind the callbacks to the lifetime of your View Controllers. Just don't forget to store the subscription to bind self as weak or unowned.

Breaking Changes

  • Requires Swift 5
  • Previously passing Transition.nilTransition to StateMachine.onTransitions(like:perform:) did never trigger the perform handler and was therefore useless. Now it will always be triggered.

Additions

  • Deprecated StateMachine.onTransitions(transition:perform:) in favor of StateMachine.onTransitions(like:perform:).
  • The StateMachine.onTransitions(perform:) overload for StateMachine.onTransitions(perform:).
  • Adds temporary observation of transitions subscribeTransitions(like:perform:), subscribeTransitions(perform:), subscribeTransitions(from:perform:), subscribeTransitions(to:perform:), subscribeTransitions(from:to:perform:). All return a ReferenceDisposable which needs to be stored as a strong reference. On deinit, the handler will be freed.

Upgrading to 4.0.0

First bump your dependency version of Finite to 4.0.0.
When you compile your project, you won't experience compile errors. Instead you will receive compiler warnings whenever you used StateMachine.onTransitions(transition:perform:). If there are no warnings, you already finished the upgrade.

In case you passed .nilTransition: this did never work. Your perform operation has never been called!
Starting from 4.0.0, .nilTransition operations will always be called.

In case you did not pass a .nilTransition, apply the fix-it and use onTransitions(like:perform:) instead.

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, dev@vknabel.com

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

License

Finite is available under the MIT license.

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Oct 17 2024 21:38:16 GMT-0900 (Hawaii-Aleutian Daylight Time)