All Projects → inamiy → Reactiveautomaton

inamiy / Reactiveautomaton

Licence: mit
🤖 ReactiveCocoa + State Machine, inspired by Redux and Elm.

Programming Languages

swift
15916 projects
elm
856 projects

Projects that are alternatives of or similar to Reactiveautomaton

Swiftrex
Swift + Redux + (Combine|RxSwift|ReactiveSwift) -> SwiftRex
Stars: ✭ 267 (+30.24%)
Mutual labels:  state-machine, reactivecocoa
Fluent State Machine
Fluent API for creating state machines in C#
Stars: ✭ 195 (-4.88%)
Mutual labels:  state-machine
Nanostate
🚦- Small Finite State Machines
Stars: ✭ 151 (-26.34%)
Mutual labels:  state-machine
Rosmaro
Visual automata-based programming in functional JavaScript
Stars: ✭ 176 (-14.15%)
Mutual labels:  state-machine
Diagram Maker
A library to display an interactive editor for any graph-like data.
Stars: ✭ 2,086 (+917.56%)
Mutual labels:  state-machine
Modernavplayer
ModernAVPlayer is a persistence AVPlayer wrapper
Stars: ✭ 179 (-12.68%)
Mutual labels:  state-machine
Sm
🚀 SM – a static State Machine library
Stars: ✭ 149 (-27.32%)
Mutual labels:  state-machine
Liquidstate
Efficient asynchronous and synchronous state machines for .NET
Stars: ✭ 197 (-3.9%)
Mutual labels:  state-machine
Awesome Fsm
🤖 A curated list of awesome resources related to finite state machines and statecharts.
Stars: ✭ 189 (-7.8%)
Mutual labels:  state-machine
P
The P programming language.
Stars: ✭ 2,309 (+1026.34%)
Mutual labels:  state-machine
Stateful
Finite state machine for Go
Stars: ✭ 172 (-16.1%)
Mutual labels:  state-machine
Kzmooncommand
🔥 An awesome command for async operation with ReactiveCocoa
Stars: ✭ 164 (-20%)
Mutual labels:  reactivecocoa
Kickstarter Reactiveextensions
A collection of extensions to the ReactiveSwift framework.
Stars: ✭ 183 (-10.73%)
Mutual labels:  reactivecocoa
Swissarmylib
Collection of helpful utilities we use in our Unity projects.
Stars: ✭ 154 (-24.88%)
Mutual labels:  state-machine
Zhuishushenqi
追书神器Swift版客户端(非官方)。 不断更新中......
Stars: ✭ 196 (-4.39%)
Mutual labels:  reactivecocoa
Re Frame Async Flow Fx
A re-frame effects handler for coordinating the kind of async control flow which often happens on app startup.
Stars: ✭ 150 (-26.83%)
Mutual labels:  state-machine
Statemachine
A feature-rich, yet simple finite state machine (FSM) implementation in C
Stars: ✭ 168 (-18.05%)
Mutual labels:  state-machine
State Machine Component
⚙️ State machine -powered components in 250 bytes
Stars: ✭ 178 (-13.17%)
Mutual labels:  state-machine
Ui Router
The de-facto solution to flexible routing with nested views in AngularJS
Stars: ✭ 13,738 (+6601.46%)
Mutual labels:  state-machine
Aws Lambda Power Tuning
AWS Lambda Power Tuning is an open-source tool that can help you visualize and fine-tune the memory/power configuration of Lambda functions. It runs in your own AWS account - powered by AWS Step Functions - and it supports three optimization strategies: cost, speed, and balanced.
Stars: ✭ 3,040 (+1382.93%)
Mutual labels:  state-machine

ReactiveAutomaton

ReactiveCocoa + State Machine, inspired by Redux and Elm. A successor of SwiftState.

Example

(Demo app is available at ReactiveCocoaCatalog)

To make a state transition diagram like above with additional effects, follow these steps:

// 1. Define `State`s and `Input`s.
enum State {
    case loggedOut, loggingIn, loggedIn, loggingOut
}

enum Input {
    case login, loginOK, logout, logoutOK
    case forceLogout
}

// Additional effects (`SignalProducer`s) while state-transitioning.
// (NOTE: Use `SignalProducer.empty` for no effect)
let loginOKProducer = /* show UI, setup DB, request APIs, ..., and send `Input.loginOK` */
let logoutOKProducer = /* show UI, clear cache, cancel APIs, ..., and send `Input.logoutOK` */
let forceLogoutOKProducer = /* do something more special, ..., and send `Input.logoutOK` */

let canForceLogout: (State) -> Bool = [.loggingIn, .loggedIn].contains

// 2. Setup state-transition mappings.
let mappings: [Automaton<State, Input>.EffectMapping] = [

  /*  Input   |   fromState => toState     |      Effect       */
  /* ----------------------------------------------------------*/
    .login    | .loggedOut  => .loggingIn  | loginOKProducer,
    .loginOK  | .loggingIn  => .loggedIn   | .empty,
    .logout   | .loggedIn   => .loggingOut | logoutOKProducer,
    .logoutOK | .loggingOut => .loggedOut  | .empty,

    .forceLogout | canForceLogout => .loggingOut | forceLogoutOKProducer
]

// 3. Prepare input pipe for sending `Input` to `Automaton`.
let (inputSignal, inputObserver) = Signal<Input, NoError>.pipe()

// 4. Setup `Automaton`.
let automaton = Automaton(
    state: .loggedOut,
    input: inputSignal,
    mapping: reduce(mappings),  // combine mappings using `reduce` helper
    strategy: .latest   // NOTE: `.latest` cancels previous running effect
)

// Observe state-transition replies (`.success` or `.failure`).
automaton.replies.observeNext { reply in
    print("received reply = \(reply)")
}

// Observe current state changes.
automaton.state.producer.startWithValues { state in
    print("current state = \(state)")
}

And let's test!

let send = inputObserver.send(value:)

expect(automaton.state.value) == .loggedIn    // already logged in
send(Input.logout)
expect(automaton.state.value) == .loggingOut  // logging out...
// `logoutOKProducer` will automatically send `Input.logoutOK` later
// and transit to `State.loggedOut`.

expect(automaton.state.value) == .loggedOut   // already logged out
send(Input.login)
expect(automaton.state.value) == .loggingIn   // logging in...
// `loginOKProducer` will automatically send `Input.loginOK` later
// and transit to `State.loggedIn`.

// 👨🏽 < But wait, there's more!
// Let's send `Input.forceLogout` immediately after `State.loggingIn`.

send(Input.forceLogout)                       // 💥💣💥
expect(automaton.state.value) == .loggingOut  // logging out...
// `forceLogoutOKProducer` will automatically send `Input.logoutOK` later
// and transit to `State.loggedOut`.

Note that any sizes of State and Input will work using ReactiveAutomaton, from single state (like above example) to covering whole app's states (like React.js + Redux architecture).

References

  1. iOSDC 2016 (Tokyo, in Japanese) (2016/08/20)
  2. iOSConf SG (Singapore, in English) (2016/10/20-21)

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].