1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-10-27 10:23:12 +03:00

GH-405 Add an UiComponent template

This commit is contained in:
Tae Won Ha 2017-04-22 16:28:55 +02:00
parent 559209256e
commit 090b5986e8
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44

View File

@ -121,3 +121,46 @@ protocol UiComponent {
init(source: Observable<StateType>, emitter: ActionEmitter, state: StateType)
}
class UiComponentTemplate: UiComponent {
typealias StateType = State
struct State {
var someField: String
}
enum Action {
case doSth
}
required init(source: Observable<StateType>, emitter: ActionEmitter, state: StateType) {
// set the typed action emit function
self.emit = emitter.typedEmitFunction()
// init the component with the initial state "state"
self.someField = state.someField
// react to the new state
source
.observeOn(MainScheduler.instance)
.subscribe(
onNext: { [unowned self] state in
print("Hello, \(self.someField)")
}
)
.disposed(by: self.disposeBag)
}
func someFunction() {
// when the user does something, emit an action
self.emit(.doSth)
}
fileprivate let emit: (Action) -> Void
fileprivate let disposeBag = DisposeBag()
fileprivate let someField: String
}