1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 11:37:32 +03:00
vimr/VimR/Context.swift

180 lines
6.1 KiB
Swift
Raw Normal View History

2017-01-22 16:22:05 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Foundation
import RxSwift
2017-02-28 12:43:45 +03:00
class Context {
2017-01-25 00:39:19 +03:00
let stateSource: Observable<AppState>
let actionEmitter = Emitter<Any>()
2017-02-28 12:42:34 +03:00
init(_ state: AppState) {
2017-02-20 20:04:45 +03:00
let baseServerUrl = URL(string: "http://localhost:\(NetUtils.openPort())")!
2017-02-28 12:42:34 +03:00
self.appState = state
self.stateSource = self.stateSubject.asObservable()
2017-02-12 13:24:15 +03:00
let actionSource = self.actionEmitter.observable
2017-02-28 12:20:25 +03:00
let openQuicklyTransformer = OpenQuicklyTransformer()
let previewTransformer = PreviewTransformer(baseServerUrl: baseServerUrl)
let previewService = PreviewService()
2017-02-28 12:42:34 +03:00
// AppState
2017-01-22 16:22:05 +03:00
Observable
.of(
actionSource
.mapOmittingNil { $0 as? AppDelegate.Action }
2017-02-28 12:42:34 +03:00
.map { self.appStateActionPair(for: $0) }
2017-02-28 12:20:25 +03:00
.transform(by: AppDelegateTransformer(baseServerUrl: baseServerUrl))
2017-01-22 16:22:05 +03:00
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<MainWindow.Action> }
2017-02-28 12:42:34 +03:00
.map { self.appStateActionPair(for: $0) }
2017-02-28 11:53:27 +03:00
.transform(by: UiRootTransformer())
2017-02-28 12:20:25 +03:00
.transform(by: openQuicklyTransformer.forMainWindow)
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? FileMonitor.Action }
2017-02-28 12:42:34 +03:00
.map { self.appStateActionPair(for: $0) }
2017-02-28 11:53:27 +03:00
.transform(by: FileMonitorTransformer())
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? OpenQuicklyWindow.Action }
2017-02-28 12:42:34 +03:00
.map { self.appStateActionPair(for: $0) }
2017-02-28 12:20:25 +03:00
.transform(by: openQuicklyTransformer.forOpenQuicklyWindow)
2017-01-22 16:22:05 +03:00
.filter { $0.modified }
.map { $0.state }
)
.merge()
.subscribe(onNext: { state in
2017-01-22 16:22:05 +03:00
self.appState = state
self.stateSubject.onNext(self.appState)
})
.addDisposableTo(self.disposeBag)
2017-02-28 12:42:34 +03:00
// MainWindow.State
2017-02-12 13:24:15 +03:00
Observable
2017-02-24 02:24:30 +03:00
.of(
actionSource
.mapOmittingNil { $0 as? UuidAction<MainWindow.Action> }
2017-02-28 12:42:34 +03:00
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
2017-02-28 12:20:25 +03:00
.transform(by: MainWindowTransformer())
.transform(by: previewTransformer.forMainWindow)
2017-02-24 02:24:30 +03:00
.filter { $0.modified }
2017-02-28 12:20:25 +03:00
.apply(to: previewService.forMainWindow)
.apply(to: HttpServerService(port: baseServerUrl.port ?? 0))
2017-02-24 02:24:30 +03:00
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<PreviewTool.Action> }
2017-02-28 12:42:34 +03:00
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
2017-02-28 12:20:25 +03:00
.transform(by: PreviewToolTransformer(baseServerUrl: baseServerUrl))
2017-02-24 02:24:30 +03:00
.filter { $0.modified }
.map { $0.state },
actionSource
2017-02-24 12:51:24 +03:00
.mapOmittingNil { $0 as? UuidAction<FileBrowser.Action> }
2017-02-28 12:42:34 +03:00
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
2017-02-28 12:20:25 +03:00
.transform(by: FileBrowserTransformer())
2017-02-24 02:24:30 +03:00
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<OpenedFileList.Action> }
2017-02-28 12:42:34 +03:00
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
2017-02-28 12:20:25 +03:00
.transform(by: OpenedFileListTransformer())
.transform(by: previewTransformer.forOpenedFileList)
.filter { $0.modified }
2017-02-28 12:20:25 +03:00
.apply(to: previewService.forOpenedFileList)
2017-02-24 02:24:30 +03:00
.map { $0.state }
)
2017-02-12 13:24:15 +03:00
.merge()
.subscribe(onNext: { state in
self.appState.mainWindows[state.uuid] = state.payload
self.stateSubject.onNext(self.appState)
})
.addDisposableTo(self.disposeBag)
2017-02-28 12:42:34 +03:00
// Preferences
2017-02-28 00:45:26 +03:00
Observable
.of(
actionSource
.mapOmittingNil { $0 as? PrefWindow.Action }
2017-02-28 12:42:34 +03:00
.map { self.appStateActionPair(for: $0) }
2017-02-28 11:53:27 +03:00
.transform(by: PrefWindowTransformer())
2017-02-28 00:45:26 +03:00
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? GeneralPref.Action }
2017-02-28 12:42:34 +03:00
.map { self.appStateActionPair(for: $0) }
2017-02-28 11:53:27 +03:00
.transform(by: GeneralPrefTransformer())
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? AppearancePref.Action }
2017-02-28 12:42:34 +03:00
.map { self.appStateActionPair(for: $0) }
2017-02-28 11:53:27 +03:00
.transform(by: AppearancePrefTransformer())
2017-02-28 00:45:26 +03:00
.filter { $0.modified }
2017-02-28 13:10:04 +03:00
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? AdvancedPref.Action }
.map { self.appStateActionPair(for: $0) }
.transform(by: AdvancedPrefTransformer())
.filter { $0.modified }
2017-02-28 00:45:26 +03:00
.map { $0.state }
)
.merge()
.subscribe(onNext: { state in
self.appState = state
self.stateSubject.onNext(self.appState)
})
.addDisposableTo(self.disposeBag)
2017-02-24 02:24:30 +03:00
2017-02-03 00:39:05 +03:00
#if DEBUG
2017-02-24 02:24:30 +03:00
// actionSource.debug().subscribe().addDisposableTo(self.disposeBag)
// stateSource.debug().subscribe().addDisposableTo(self.disposeBag)
2017-02-03 00:39:05 +03:00
#endif
}
deinit {
self.stateSubject.onCompleted()
}
2017-01-25 00:39:19 +03:00
fileprivate let stateSubject = PublishSubject<AppState>()
2017-01-22 16:22:05 +03:00
fileprivate let scheduler = SerialDispatchQueueScheduler(qos: .userInitiated)
fileprivate let disposeBag = DisposeBag()
2017-02-06 20:57:50 +03:00
fileprivate var appState: AppState
2017-02-28 12:42:34 +03:00
fileprivate func appStateActionPair<ActionType>(for action: ActionType) -> StateActionPair<AppState, ActionType> {
return StateActionPair(state: self.appState, action: action, modified: false)
}
fileprivate func mainWindowStateActionPair<ActionType>(for action: UuidAction<ActionType>)
-> StateActionPair<UuidState<MainWindow.State>, ActionType>? {
guard let mainWindowState = self.appState.mainWindows[action.uuid] else {
return nil
}
return StateActionPair(state: UuidState(uuid: action.uuid, state: mainWindowState),
action: action.payload,
modified: false)
}
}
extension Observable {
2017-02-03 00:39:05 +03:00
fileprivate func transform<T:Transformer>(by transformer: T) -> Observable<Element> where T.Element == Element {
2017-01-25 00:39:19 +03:00
return transformer.transform(self)
}
2017-02-05 17:17:25 +03:00
fileprivate func apply<S:Service>(to service: S) -> Observable<Element> where S.Element == Element {
2017-02-03 00:39:05 +03:00
return self.do(onNext: service.apply)
}
}