Giter VIP home page Giter VIP logo

rxautomaton's Introduction

NOTE: This repository has been discontinued in favor of Actomaton.

RxAutomaton

RxSwift port of ReactiveAutomaton (State Machine).

Terminology

Whenever the word "signal" or "(signal) producer" appears (derived from ReactiveCocoa), they mean "hot-observable" and "cold-observable".

Example

(Demo app is bundled in the project)

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 (`Observable`s) while state-transitioning.
// (NOTE: Use `Observable.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) = Observable<Input>.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.subscribe(next: { reply in
    print("received reply = \(reply)")
})

// Observe current state changes.
automaton.state.asObservable().subscribe(next: { state in
    print("current state = \(state)")
})

And let's test!

let send = inputObserver.onNext

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`.

License

MIT

rxautomaton's People

Contributors

andrewsb avatar crazyfanfan avatar haritowa avatar ilyapuchka avatar inamiy avatar jwhitley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rxautomaton's Issues

Input with parameters?

Hey @inamiy! I'm testing out RxAutomaton in favor of SwiftState for a state machine that models the CoreBluetooth's CBCentralManager.

The issue I'm running into is handling an input with parameters. For example, my "discovered bluetooth peripheral" input takes an argument rssi: Int (case discovered(rssi: Int)).
(Here are all of my inputs):
screen shot 2016-09-26 at 4 23 21 pm

If I try to compose my mappings, I get an error saying expects argument of type (rssi: Int):
screen shot 2016-09-26 at 4 21 10 pm

Which I can fulfill by explicitly specifying some integer amount, but I'd like to be allow the mapping to take effect for every .discovered input, regardless of integer amount. Is that currently possible with RxAutomaton?

Producer being called multiple times for one input

@inamiy I'm running into an interesting problem. I've noticed that on every state change, my observable is subscribed to twice, executing the code inside the observable twice.

To illustrate the problem, I forked the demo, and added some logs: https://github.com/AndrewSB/RxAutomaton/blob/swift/3.0/Demo/ViewController.swift#L42

Let me know if you need anything else to repro the issue I'm having ๐Ÿ‘

Not sure why it's happening currently - maybe somewhere inside the source the producer is subscribed to twice?

Pass input parameters to producer in mappings

Along the lines of #1, I'd like to be able to pass the parameters from the input into the producer. For example, if I have a producer: discoverCharacteristicsProducer: ([CBService]) -> Observable<Event>, I know I can call it by using a closure as my NextMapping
screen shot 2016-09-26 at 5 06 40 pm

in the last element ^ I capture the services from the .discoveredServices event, and I then pass it into the discoverCharacteristicsProducer

Is it possible to do capture the param using the pipe mapping syntax? I'm sure theres some way to compose the operation, I'm not able to think of the type signature for a new | operator that would do this

RxSwift5

Can you release an new version to support RxSwift v5

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.