Latest 2.2.0
Homepage https://github.com/albertodebortoli/Stateful
License MIT
Platforms ios 8.0
Frameworks Foundation
Authors

Stateful 🦜

Build Status
Version
License
Platform

The easiest state machine in Swift.

Stateful is a minimalistic, thread-safe, non-boilerplate and super easy to use state machine in Swift.

Example

  • define the events and statuses you need. E.g.:
enum EventType {
    case start
    case pause
}

enum StateType {
    case idle
    case started
}
  • create a state machine with the initial state (you might want to retain it in a property)
let stateMachine = StateMachine<StateType, EventType>(initialState: .idle)

StateMachine will use the main queue to execute the transition pre and post blocks but you can optionally provide a custom one.

let dispatchQueue = DispatchQueue(label: "com.albertodebortoli.someSerialCallbackQueue")
let stateMachine = StateMachine<StateType, EventType>(initialState: StateType.idle, callbackQueue: dispatchQueue)
  • create transitions and add them to the state machine (the state machine will automatically recognize the new statuses)
let t1 = Transition<StateType, EventType>(with: .start,
                    from: .idle,
                    to: .started)

let t2 = Transition<StateType, EventType>(with: .pause,
                    from: .started,
                    to: .idle,
                    preBlock: {
                        print("Going to move from (StateType.started) to (StateType.idle)!")
                    }, postBlock: {
                        print("Just moved from (StateType.started) to (StateType.idle)!")
    })

stateMachine.add(transition: t1)
stateMachine.add(transition: t2)
  • process events like so
stateMachine.process(event: .start)
stateMachine.process(event: .pause, callback: { result in
    switch result {
    case .success:
        print("Event 'pause' was processed")
    case .failure:
        print("Event 'pause' cannot currently be processed.")
    }
})

Logging

You can optionally enable logging to print extra state change information on the console

stateMachine.enableLogging = true

Example:

[Stateful 🦜]: Processing event 'start' from 'idle'
[Stateful 🦜]: Processed pre condition for event 'start' from 'idle' to 'started'
[Stateful 🦜]: Processed state change from 'idle' to 'started'
[Stateful 🦜]: Processed post condition for event 'start' from 'idle' to 'started'
[Stateful 🦜]: Processing event 'stop' from 'started'
[Stateful 🦜]: Processed pre condition for event 'stop' from 'started' to 'idle'
[Stateful 🦜]: Processed state change from 'started' to 'idle'
[Stateful 🦜]: Processed post condition for event 'stop' from 'started' to 'idle'

Installation

Stateful is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'Stateful'

Author

Alberto De Bortoli, [email protected]

License

Stateful is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "Stateful",
    "version": "2.2.0",
    "summary": "The easiest state machine in Swift",
    "description": "A minimalistic, thread-safe, non-boilerplate and super easy to use state machine in Swift.",
    "homepage": "https://github.com/albertodebortoli/Stateful",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Alberto De Bortoli": "[email protected]"
    },
    "source": {
        "git": "https://github.com/albertodebortoli/Stateful.git",
        "tag": "2.2.0"
    },
    "social_media_url": "https://twitter.com/albertodebo",
    "platforms": {
        "ios": "8.0"
    },
    "swift_version": "5.0",
    "source_files": "Stateful/Classes/**/*.swift",
    "frameworks": "Foundation",
    "testspecs": [
        {
            "name": "StatefulUnitTests",
            "test_type": "unit",
            "source_files": "Stateful/UnitTests/**/*.swift"
        }
    ]
}

Pin It on Pinterest

Share This