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
|
|
|
|
|
|
|
|
class StateContext {
|
|
|
|
|
2017-01-25 00:39:19 +03:00
|
|
|
let stateSource: Observable<AppState>
|
2017-01-17 21:47:59 +03:00
|
|
|
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()
|
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)
|
|
|
|
|
|
|
|
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 {
|
2017-01-17 21:47:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
return StateActionPair(state: UuidState(uuid: action.uuid, state: mainWindowState),
|
|
|
|
action: action.payload,
|
|
|
|
modified: false)
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
.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)
|
2017-01-17 21:47:59 +03:00
|
|
|
})
|
|
|
|
.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-17 21:47:59 +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)
|
2017-01-17 21:47:59 +03:00
|
|
|
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
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
|
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)
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
}
|