mirror of
https://github.com/qvacua/vimr.git
synced 2024-12-29 00:34:26 +03:00
Refactor slightly
This commit is contained in:
parent
9702cee35a
commit
c1280d07fa
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user