1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-29 00:34:26 +03:00

Refactor slightly

This commit is contained in:
Tae Won Ha 2018-07-07 16:27:33 +02:00
parent 9702cee35a
commit c1280d07fa
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
2 changed files with 28 additions and 9 deletions

View File

@ -6,7 +6,7 @@
import Cocoa
import RxSwift
extension ObservableType {
extension Observable {
func mapOmittingNil<R>(_ transform: @escaping (E) throws -> R?) -> RxSwift.Observable<R> {
return self
@ -14,6 +14,31 @@ extension ObservableType {
.filter { $0 != nil }
.map { $0! }
}
func completableSubject() -> CompletableSubject<Element> {
return CompletableSubject(source: self)
}
}
class CompletableSubject<T> {
func asObservable() -> Observable<T> {
return self.subject.asObservable()
}
init(source: Observable<T>) {
let subject = PublishSubject<T>()
self.subscription = source.subscribe(subject)
self.subject = subject
}
func onCompleted() {
self.subject.onCompleted()
self.subscription.dispose()
}
private let subject: PublishSubject<T>
private let subscription: Disposable
}
extension PrimitiveSequenceType where TraitType == CompletableTrait, ElementType == Never {

View File

@ -80,25 +80,19 @@ class UiRoot: UiComponent {
private let prefWindow: PrefWindow
private var mainWindows = [String: MainWindow]()
private var subjectForMainWindows = [String: PublishSubject<MainWindow.State>]()
private var disposables = [String: Disposable]()
private var subjectForMainWindows = [String: CompletableSubject<MainWindow.State>]()
private func newMainWindow(with state: MainWindow.State) -> MainWindow {
let subject = PublishSubject<MainWindow.State>()
let source = self.source.mapOmittingNil { $0.mainWindows[state.uuid] }
let subject = self.source.mapOmittingNil { $0.mainWindows[state.uuid] }.completableSubject()
self.subjectForMainWindows[state.uuid] = subject
self.disposables[state.uuid] = source.subscribe(subject)
return MainWindow(source: subject.asObservable(), emitter: self.emitter, state: state)
}
private func removeMainWindow(with uuid: String) {
self.subjectForMainWindows[uuid]?.onCompleted()
self.disposables[uuid]?.dispose()
self.subjectForMainWindows.removeValue(forKey: uuid)
self.disposables.removeValue(forKey: uuid)
self.mainWindows.removeValue(forKey: uuid)
}
}