2017-01-22 16:22:05 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
2017-01-17 21:47:59 +03:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RxSwift
|
|
|
|
|
2017-02-28 12:43:45 +03:00
|
|
|
class Context {
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-01-25 00:39:19 +03:00
|
|
|
let stateSource: Observable<AppState>
|
2017-01-17 21:47:59 +03:00
|
|
|
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
|
2017-02-09 02:31:49 +03:00
|
|
|
|
|
|
|
self.stateSource = self.stateSubject.asObservable()
|
2017-02-12 13:24:15 +03:00
|
|
|
let actionSource = self.actionEmitter.observable
|
2017-02-09 02:31:49 +03:00
|
|
|
|
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)
|
2017-02-20 21:33:22 +03:00
|
|
|
.filter { $0.modified }
|
2017-02-28 20:53:49 +03:00
|
|
|
.apply(to: PrefService())
|
2017-02-20 21:33:22 +03:00
|
|
|
.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())
|
2017-02-20 21:33:22 +03:00
|
|
|
.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()
|
2017-01-17 21:47:59 +03:00
|
|
|
.subscribe(onNext: { state in
|
2017-01-22 16:22:05 +03:00
|
|
|
self.appState = state
|
|
|
|
self.stateSubject.onNext(self.appState)
|
2017-01-17 21:47:59 +03:00
|
|
|
})
|
|
|
|
.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 }
|
2017-02-26 16:23:23 +03:00
|
|
|
.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)
|
2017-02-26 16:23:23 +03:00
|
|
|
.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()
|
2017-02-11 20:32:22 +03:00
|
|
|
.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)
|
2017-02-28 17:52:48 +03:00
|
|
|
// stateSource
|
|
|
|
// .debug()
|
|
|
|
// .subscribe(onNext: { state in
|
|
|
|
// })
|
|
|
|
// .addDisposableTo(self.disposeBag)
|
2017-02-03 00:39:05 +03:00
|
|
|
#endif
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:53:24 +03:00
|
|
|
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)
|
2017-01-17 21:47:59 +03:00
|
|
|
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)
|
|
|
|
}
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Observable {
|
|
|
|
|
2017-03-11 19:08:50 +03:00
|
|
|
fileprivate func transform<T:Reducer>(by transformer: T) -> Observable<Element> where T.Element == Element {
|
2017-01-25 00:39:19 +03:00
|
|
|
return transformer.transform(self)
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
|
2017-03-25 12:45:39 +03:00
|
|
|
// If the following is used, the compiler does not finish...
|
|
|
|
// fileprivate func transform<T:Reducer>(by transformers: [T]) -> Observable<Element> where T.Element == Element {
|
|
|
|
// return transformers.reduce(self) { (result: Observable<Element>, transformer: T) -> Observable<Element> in
|
|
|
|
// transformer.transform(result)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
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)
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
}
|