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:
parent
559209256e
commit
090b5986e8
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user