Git Product home page Git Product logo

swiftmachine's Introduction

SwiftMachine

Manage state AND state transitions in a predictable and declarative way with SwiftMachine!

Basic Example

Define your state and state machine:

enum PizzaState: StateMachineDataSource {

    case makingDough
    case addingTopping([Topping])
    case baking
    case eating
    
    static var initialState: PizzaState = .makingDough
    
    static func shouldTransition(from: PizzaState, to: PizzaState) -> Bool {
        switch (from, to) {
        case (.makingDough, addingTopping):
            return true
        case (.addingTopping, .baking):
            return true
        case (.baking, .eating):
            return true
        default:
            return false
        }
    }
}

class Pizza: StateMachine<PizzaState> {

    private(set) var topping: [Topping] = []
    
    override var state: PizzaState {
        didSet {
            // Maybe we want to persist the topping
            if case .addingTopping(let topping) = state {
                self.topping = topping
                print(self.topping) // [salami, onion]
            }
        }
    }
}

Create a new state machine and modify its state:

let pizza = Pizza()

print(pizza.state) // .makingDough since we specified this as the initial state

pizza.state = .addingTopping([salami, onion]]

print(pizza.state) // .addingTopping

pizza.state = .eating

print(pizza.state) // still .addingTopping since transition between .addTopping and .eating is not allowed, you have the bake the pizza first!

pizza.state = .baking 

print(pizza.state) // .baking

pizza.state = .eating

print(pizza.state) // .eating

Listening for state changes:

class ViewController: UIViewController {
  
  let pizza = Pizza()
  
  override func viewDidLoad() {
     super.viewDidLoad()
     pizza.addListener(self) // No need to remove listener later since its stored as a weak reference
  }
  
  func updateUI() {
     switch pizza.state {
        case .makingDough:
            handleDoughUI()
        case .addingTopping(let toppings):
            handleToppingUI(toppings)
        case .baking:
            handleBakingUI()
        case .eating:
            handleEatingUI()
     }
  }
  
}
extension ViewController: StateListener {
    func stateChanged<T>(for stateMachine: StateMachine<T>) where T : StateMachineDataSource {
        updateUI()
    }
}

Installation

Carthage:

github "bangerang/SwiftMachine"

CocoaPods:

pod 'SwiftMachine'

Credits

Thanks to @jemmons for me inspiring me to build this library, I really recommend you to read his blog about State Machines in Swift.

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.