1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-28 02:54:31 +03:00
vimr/VimR/Context.swift

192 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>()
init(baseServerUrl: URL, state: AppState) {
2017-02-20 20:04:45 +03:00
2017-02-28 12:42:34 +03:00
self.appState = state
self.stateSource = self.stateSubject.asObservable()
2017-04-02 18:27:12 +03:00
self.httpService = HttpServerService(port: baseServerUrl.port!)
2017-03-29 21:10:11 +03:00
let openQuicklyReducer = OpenQuicklyReducer()
let markdownReducer = MarkdownReducer(baseServerUrl: baseServerUrl)
2017-02-28 12:20:25 +03:00
let prefService = PrefService()
let htmlPreviewToolReducer = HtmlPreviewToolReducer(baseServerUrl: baseServerUrl)
2017-02-28 12:20:25 +03:00
let previewService = PreviewService()
2017-02-28 12:42:34 +03:00
// AppState
2017-01-22 16:22:05 +03:00
Observable
.of(
2017-04-22 12:31:42 +03:00
self.actionSourceForAppState()
.reduce(by: AppDelegateReducer(baseServerUrl: baseServerUrl))
.filterMapPair(),
self.actionSourceForAppState()
.reduce(by: UiRootReducer())
.reduce(by: openQuicklyReducer.forMainWindow)
.filter { $0.modified }
.apply(to: prefService.forMainWindow)
.map { $0.state },
self.actionSourceForAppState()
.reduce(by: FileMonitorReducer())
.filterMapPair(),
self.actionSourceForAppState()
.reduce(by: openQuicklyReducer.forOpenQuicklyWindow)
.filterMapPair()
2017-01-22 16:22:05 +03:00
)
.merge()
.subscribe(onNext: self.emitAppState)
.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(
2017-04-22 12:31:42 +03:00
self.actionSourceForMainWindow()
.reduce(by: MainWindowReducer())
.reduce(by: markdownReducer.forMainWindow)
.filter { $0.modified }
.apply(to: previewService.forMainWindow)
.apply(to: self.httpService.forMainWindow)
.map { $0.state },
self.actionSourceForMainWindow()
.reduce(by: PreviewToolReducer(baseServerUrl: baseServerUrl))
.filterMapPair(),
self.actionSourceForMainWindow()
.reduce(by: htmlPreviewToolReducer)
.filter { $0.modified }
.apply(to: self.httpService.forHtmlPreviewTool)
.map { $0.state },
self.actionSourceForMainWindow()
.reduce(by: FileBrowserReducer())
.filterMapPair(),
self.actionSourceForMainWindow()
.reduce(by: OpenedFileListReducer())
.reduce(by: markdownReducer.forOpenedFileList)
.filter { $0.modified }
.apply(to: previewService.forOpenedFileList)
.map { $0.state }
2017-02-24 02:24:30 +03:00
)
2017-02-12 13:24:15 +03:00
.merge()
.subscribe(onNext: self.emitAppState)
.addDisposableTo(self.disposeBag)
2017-02-28 12:42:34 +03:00
// Preferences
2017-02-28 00:45:26 +03:00
Observable
.of(
2017-04-22 12:31:42 +03:00
self.actionSourceForAppState()
.reduce(by: PrefWindowReducer())
.filterMapPair(),
self.actionSourceForAppState()
.reduce(by: GeneralPrefReducer())
.filterMapPair(),
self.actionSourceForAppState()
.reduce(by: AppearancePrefReducer())
.filterMapPair(),
self.actionSourceForAppState()
.reduce(by: AdvancedPrefReducer())
.filterMapPair()
)
2017-02-28 00:45:26 +03:00
.merge()
.apply(to: prefService.forPrefPanes)
.subscribe(onNext: self.emitAppState)
2017-02-28 00:45:26 +03:00
.addDisposableTo(self.disposeBag)
2017-02-24 02:24:30 +03:00
2017-02-03 00:39:05 +03:00
#if DEBUG
// actionSource.debug().subscribe().addDisposableTo(self.disposeBag)
2017-04-22 12:31:42 +03:00
// stateSource.debug().subscribe().addDisposableTo(self.disposeBag)
2017-02-03 00:39:05 +03:00
#endif
}
deinit {
self.stateSubject.onCompleted()
}
2017-04-02 18:27:12 +03:00
fileprivate let httpService: HttpServerService
2017-03-24 19:47:08 +03:00
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 emitAppState(_ mainWindow: UuidState<MainWindow.State>) {
self.appState.mainWindows[mainWindow.uuid] = mainWindow.payload
self.stateSubject.onNext(self.appState)
self.cleanUpAppState()
}
fileprivate func emitAppState(_ appState: AppState) {
self.appState = appState
self.stateSubject.onNext(self.appState)
self.cleanUpAppState()
}
fileprivate func cleanUpAppState() {
self.appState.mainWindows.keys.forEach { uuid in
2017-03-31 20:32:01 +03:00
self.appState.mainWindows[uuid]?.viewToBeFocused = nil
self.appState.mainWindows[uuid]?.urlsToOpen.removeAll()
}
}
2017-04-22 12:31:42 +03:00
fileprivate func actionSourceForAppState<T>() -> Observable<StateActionPair<AppState, T>> {
return self.actionEmitter.observable
.mapOmittingNil { $0 as? T }
.map { self.appStateActionPair(for: $0) }
}
fileprivate func actionSourceForMainWindow<T>() -> Observable<StateActionPair<UuidState<MainWindow.State>, T>> {
return self.actionEmitter.observable
.mapOmittingNil { $0 as? UuidAction<T> }
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
}
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-04-22 12:31:42 +03:00
fileprivate func reduce<T:Reducer>(by reducer: T) -> Observable<Element> where T.Pair == Element {
return reducer.reduce(self)
}
2017-03-29 21:23:04 +03:00
fileprivate func apply<S:Service>(to service: S) -> Observable<Element> where S.Pair == Element {
2017-02-03 00:39:05 +03:00
return self.do(onNext: service.apply)
}
fileprivate func apply<S:StateService>(to service: S) -> Observable<Element> where S.StateType == Element {
return self.do(onNext: service.apply)
}
2017-04-22 12:31:42 +03:00
fileprivate func filterMapPair<S, A>() -> Observable<S> where Element == StateActionPair<S, A> {
return self
.filter { $0.modified }
.map { $0.state }
}
}