TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Oct 2017 |
SwiftSwift Version | 4.0 |
SPMSupports SPM | ✗ |
Maintained by Keita Oouchi.
Unidirectional data flow for reactive programming in iOS.
Porting facebook’s flux implementation in Swift, except callback called when store changes.
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.
To run the example project, clone the repo, and run pod install
from the Example directory first.
More complicated real world example like below is here.
import FluxxKit
import RxSwift
final class ViewModel: StateType {
var count = Observable<Int>(0)
}
extension ViewModel {
enum Action: ActionType {
case plus
case minus
}
}
extension ViewModel {
final class Reducer: FluxxKit.Reducer<ViewModel, Action> {
override func reduce(state: ViewModel, action: Action) {
switch action {
case .plus:
state.count.value = state.count + 1
case .minus:
state.count.value = state.count - 1
}
}
}
}
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)
}
(
View -> Action -> Dispatcher -> (Middleware) -> Store -> Reducer -> Observable
Action
Dispatcher
,Store
s that hold the application’s data,state transition
occurs in some Store
that could responds to dispatched Action
,Observable
property in these Store
.Observable ---> View
Observable
properties,iOS 9 or later. Swift3.0 or later.
FluxxKit is available through CocoaPods or Carthage.
keitaoouchi, [email protected]
FluxxKit is available under the MIT license. See the LICENSE file for more info.