Fluxer 1.4.0

Fluxer 1.4.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Sep 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by rb_de0.



Fluxer 1.4.0

Fluxer

Swift Flux Framework

Requirements

  • Swift 3.1 or lator
  • Xcode 8.3.3 or lator
  • iOS 9.0 or later

Usage

Store

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

Dispatcher manages multiple ActionHandlers and tells Actions to Action. Please instantiate as follows.

let dispacher = Dispatcher()

Registration ActionHandler

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())

AsyncActionCreator

Dispatcher can dispatch actions asynchronously. Please register as follows.

dispatcher.dispatch { callback in

     // do background tasks
    callback(HogeAction())
}

waitFor

By using waitFor method in a Dispatcher, you can control the order of calling ActionHandler.

Observable

In Fluxer, we use Observable to subscribe states of a Store.

ObservableValue

let value = ObservableValue(0)

ObservableValue is a Observable that holds the value. You need to change the value property.

Render

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

Disposable is an object that manages Observable subscriptions. In Fluxer, you can use two Disposables.

BlockDisposable

Disposable executing Block when dispose is called.

CompositeDisposable

Disposable which manages multiple Disposables.

DisposeBag

DisposeBag manages Disposable according to the life cycle.

var disposeBag = DisposeBag()

let value = ObservableValue(0)

value.subscribe {
    print($0)
}.addTo(disposeBag)

disposeBag = DisposeBag() // dispose

Operator

Fluxer provides a few operators for Observable. Operators will be added in the future.

Map

Map is an operator that converts Observable values ​​to arbitrary types.

let value = ObservableValue(0)
value.map { $0 + 1 }.subscribe {
    print($0)
}

Filter

Filter is an operator that flows only values ​​that satisfy the specified conditions.

let value = ObservableValue(0)
value.filter { $0 >= 0 }.subscribe {
    print($0)
}

Combine

Map and Filter can be used in combination.

let value = ObservableValue(0)
value.map { $0 + 1 }.filter { $0 >= 0 }.subscribe {
    print($0)
}

Future Improvement

  • Docs
  • SomeOperator
  • Swift Package Manager Tests

Author

rb_de0, [email protected]

License

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