Swift Flux Framework
- Swift 3.1 or lator
- Xcode 8.3.3 or lator
- iOS 9.0 or later
github "rb-de0/Fluxer"
Fluxer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Fluxer'
Store maintains the status of applications and screens. Please define as follows.
class MainStore: Store {
let value = ObservableValue(0)
required init(with dispatcher: Dispatcher) {
}
}
let mainStore = MainStore(with: dispatcher)
In Store initializer, please register ActionHandler in dispatcher. The description of ActionHandler is below.
Dispatcher manages multiple ActionHandlers and tells Actions to Action. Please instantiate as follows.
let dispacher = Dispatcher()
ActionHandler changes a Store from a dispatched Action. Action has data to change Store. Please register as follows.
struct HogeAction: Action {}
class MainStore: Store {
let value = ObservableValue(0)
required init(with dispatcher: Dispatcher) {
let token = dispacher.register { [weak self] action in
self?.value.value = 10
}
}
}
let dispacher = Dispatcher()
let mainStore = MainStore(with: dispatcher)
dispatcher.dispatch(HogeAction())
Dispatcher can dispatch actions asynchronously. Please register as follows.
dispatcher.dispatch { callback in
// do background tasks
callback(HogeAction())
}
By using waitFor method in a Dispatcher, you can control the order of calling ActionHandler.
In Fluxer, we use Observable to subscribe states of a Store.
let value = ObservableValue(0)
ObservableValue is a Observable that holds the value. You need to change the value property.
let value = ObservableValue(0)
let disposable = value.asRender().subscribe { _ in
print(Thread.isMainThread) // => true
}
DispatchQueue.global().async {
value.value = 10
}
Render is an Observable that can be generated from other Observable. Methods that handle a Render changes are always executed in UI threads.
Disposable is an object that manages Observable subscriptions. In Fluxer, you can use two Disposables.
Disposable executing Block when dispose is called.
Disposable which manages multiple Disposables.
DisposeBag manages Disposable according to the life cycle.
var disposeBag = DisposeBag()
let value = ObservableValue(0)
value.subscribe {
print($0)
}.addTo(disposeBag)
disposeBag = DisposeBag() // dispose
Fluxer provides a few operators for Observable. Operators will be added in the future.
Map is an operator that converts Observable values to arbitrary types.
let value = ObservableValue(0)
value.map { $0 + 1 }.subscribe {
print($0)
}
Filter is an operator that flows only values that satisfy the specified conditions.
let value = ObservableValue(0)
value.filter { $0 >= 0 }.subscribe {
print($0)
}
Map and Filter can be used in combination.
let value = ObservableValue(0)
value.map { $0 + 1 }.filter { $0 >= 0 }.subscribe {
print($0)
}
- Docs
- SomeOperator
- Swift Package Manager Tests
Fluxer is available under the MIT license. See the LICENSE file for more info.