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

92 lines
2.9 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>()
init() {
self.stateSource = self.stateSubject.asObservable()
let actionSource = self.actionEmitter.observable
2017-01-25 00:39:19 +03:00
self.httpServerService = HttpServerService(port: NetUtils.openPort())
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 }
2017-01-25 00:39:19 +03:00
.map { $0.state }
.applyState(to: self.httpServerService)
.subscribe(onNext: { state in
self.appState.mainWindows[state.uuid] = state.payload
self.stateSubject.onNext(self.appState)
})
.addDisposableTo(self.disposeBag)
2017-01-22 16:22:05 +03:00
actionSource.debug().subscribe().addDisposableTo(self.disposeBag)
stateSource.debug().subscribe().addDisposableTo(self.disposeBag)
}
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()
fileprivate var appState = AppState.default
fileprivate let appDelegateTransformer = AppDelegateTransformer()
fileprivate let uiRootTransformer = UiRootTransformer()
fileprivate let mainWindowTransformer = MainWindowTransformer()
2017-01-25 00:39:19 +03:00
fileprivate let previewTransformer = PreviewTransformer()
fileprivate let httpServerService: HttpServerService
}
extension Observable {
2017-01-25 00:39:19 +03:00
fileprivate func transform<T: Transformer>(by transformer: T) -> Observable<Element> where T.Element == Element {
return transformer.transform(self)
}
2017-01-25 00:39:19 +03:00
fileprivate func applyState<S: Service>(to service: S) -> Observable<Element> where S.StateType == Element {
return service.apply(self)
}
}