From 090b5986e82db1de5a4fc0d010511fc856bebb72 Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Sat, 22 Apr 2017 16:28:55 +0200 Subject: [PATCH] GH-405 Add an UiComponent template --- VimR/Types.swift | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/VimR/Types.swift b/VimR/Types.swift index 5df577e4..a2051fce 100644 --- a/VimR/Types.swift +++ b/VimR/Types.swift @@ -121,3 +121,46 @@ protocol UiComponent { init(source: Observable, emitter: ActionEmitter, state: StateType) } + +class UiComponentTemplate: UiComponent { + + typealias StateType = State + + struct State { + + var someField: String + } + + enum Action { + + case doSth + } + + required init(source: Observable, 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 +} \ No newline at end of file