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

124 lines
4.2 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
class StateContext {
2017-01-25 00:39:19 +03:00
let stateSource: Observable<AppState>
let actionEmitter = Emitter<Any>()
2017-02-06 20:57:50 +03:00
init(_ initialState: AppState) {
self.appDelegateTransformer = AppDelegateTransformer(baseServerUrl: initialState.baseServerUrl)
self.previewTransformer = PreviewTransformer(baseServerUrl: initialState.baseServerUrl)
self.httpServerService = HttpServerService(port: initialState.baseServerUrl.port ?? 0)
2017-01-22 16:22:05 +03:00
self.appState = initialState
self.stateSource = self.stateSubject.asObservable()
let actionSource = self.actionEmitter.observable/*.do(onNext: { _ in
self.appState.mainWindows.keys.forEach { self.appState.mainWindows[$0]?.resetInstantStates() }
})*/
2017-01-22 16:22:05 +03:00
Observable
.of(
actionSource
.mapOmittingNil { $0 as? AppDelegate.Action }
.map { StateActionPair(state: self.appState, action: $0, modified: false) }
.transform(by: self.appDelegateTransformer)
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<MainWindow.Action> }
.map { StateActionPair(state: self.appState, action: $0, modified: false) }
.transform(by: self.uiRootTransformer)
.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)
actionSource
.mapOmittingNil { $0 as? UuidAction<MainWindow.Action> }
.mapOmittingNil { action in
2017-01-22 16:22:05 +03:00
guard let mainWindowState = self.appState.mainWindows[action.uuid] else {
return nil
}
2017-01-22 16:22:05 +03:00
return StateActionPair(state: UuidState(uuid: action.uuid, state: mainWindowState),
action: action.payload,
modified: false)
}
.transform(by: self.mainWindowTransformer)
2017-01-22 16:22:05 +03:00
.transform(by: self.previewTransformer)
.filter { $0.modified }
.apply(to: self.previewService)
2017-02-03 00:39:05 +03:00
.apply(to: self.httpServerService)
2017-01-25 00:39:19 +03:00
.map { $0.state }
.subscribe(onNext: { state in
self.appState.mainWindows[state.uuid] = state.payload
self.stateSubject.onNext(self.appState)
})
.addDisposableTo(self.disposeBag)
actionSource
.mapOmittingNil { $0 as? UuidAction<PreviewTool.Action> }
.mapOmittingNil { action in
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)
}
.transform(by: self.previewToolTransformer)
.filter { $0.modified }
.map { $0.state }
.subscribe(onNext: { state in
self.appState.mainWindows[state.uuid] = state.payload
self.stateSubject.onNext(self.appState)
})
.addDisposableTo(self.disposeBag)
2017-02-03 00:39:05 +03:00
#if DEBUG
// actionSource.debug().subscribe().addDisposableTo(self.disposeBag)
// stateSource.debug().subscribe().addDisposableTo(self.disposeBag)
2017-02-03 00:39:05 +03:00
#endif
}
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-06 20:57:50 +03:00
fileprivate let appDelegateTransformer: AppDelegateTransformer
fileprivate let uiRootTransformer = UiRootTransformer()
fileprivate let mainWindowTransformer = MainWindowTransformer()
fileprivate let previewTransformer: PreviewTransformer
fileprivate let previewToolTransformer = PreviewToolTransformer()
2017-01-25 00:39:19 +03:00
fileprivate let previewService = PreviewNewService()
2017-01-25 00:39:19 +03:00
fileprivate let httpServerService: HttpServerService
}
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)
}
}