Giter Club home page Giter Club logo

fluxxkit's Introduction

FluxxKit

Swift 3.0 Carthage compatible Version License Platform

Overview

Unidirectional data flow for reactive programming in iOS.

Porting facebook's flux implementation in Swift, except callback called when store changes.

Why no callback?

We have RxSwift, ReactiveSwift, ReactiveKit or something else. All the stateful things could be implemented as Observable or Stream, and ViewController could bind and react to them.

Getting Started

  1. State
import FluxxKit
import RxSwift

final class ViewModel: StateType {
  var count = Observable<Int>(0)
}
  1. Action
extension ViewModel {
  enum Action: ActionType {
    case plus
    case minus
  }
}
  1. Reducer
extension ViewModel {
  final class Reducer: FluxxKit.Reducer<ViewModel, Action> {
    override func reduce(action: Action, to state: ViewModel) {

      switch action {
      case .plus:
        state.count.value = state.count + 1
      case .minus:
        state.count.value = state.count - 1
      }
    }
  }
}
  1. View

Create store and register it to dispatcher, and bind store's state:

import FluxxKit
import RxSWift

final class ViewController: UIViewController {

  @IBOutlet var counterLabel: UILabel!
  @IBOutlet var plusButton: UIButton!
  @IBOutlet var minusButton: UIButton!
  var store = Store<ViewModel, ViewModel.Action>(
    reducer: ViewModel.Reducer()
  )

  override func viewDidLoad() {
    super.viewDidLoad()

    Dispatcher.shared.register(store: self.store)

    store.state.count.asObservable().onserveOn(MainScheduler.instance)
      .subscribe(onNext: { [weak self] count in
        self?.counterLabel.text = "\(count)"
      })
  }

  deinit {
    Dispatcher.shared.unregister(identifier: self.store.identifier)
  }
}

Dispatch action with UI action:

@IBAction
func onTouchPlusButton(sender: Any) {
  Dispatcher.shared.dispatch(action: ViewModel.Action.plus)
}

@IBAction
func onTouchMinusButton(sender: Any) {
  Dispatcher.shared.dispatch(action: ViewModel.Action.minus)
}

Scenario

(nice diagram here ๐Ÿ‘ป)

Flux

  • When a user interacts with a View(Controller), it propagates an Action
  • through a central Dispatcher,
  • to the various Stores that hold the application's data,
  • state transition occurs in some Store that could responds to dispatched Action,
  • which will emit new items to Observable property in these Store.

Reactive Programming

  • ViewController subscribes Store's Observable properties,
  • and react to it.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

More complicated real world example is here.

GIF

Requirements

iOS 9 or later. Swift3.0 or later.

Installation

FluxxKit is available through CocoaPods or Carthage.

CocoaPods

pod "FluxxKit"

Carthage

github "keitaoouchi/FluxxKit"

for detail, please follow the Carthage Instruction

Author

keitaoouchi, [email protected]

License

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

fluxxkit's People

Contributors

keitaoouchi avatar

Watchers

 avatar

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.