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

Add some convenience functions

This commit is contained in:
Tae Won Ha 2017-02-28 10:42:34 +01:00
parent 02ebe1e3c2
commit b17d7eaaec
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44

View File

@ -11,10 +11,10 @@ class StateContext {
let stateSource: Observable<AppState>
let actionEmitter = Emitter<Any>()
init(_ initialState: AppState) {
init(_ state: AppState) {
let baseServerUrl = URL(string: "http://localhost:\(NetUtils.openPort())")!
self.appState = initialState
self.appState = state
self.stateSource = self.stateSubject.asObservable()
let actionSource = self.actionEmitter.observable
@ -24,30 +24,31 @@ class StateContext {
let previewService = PreviewService()
// AppState
Observable
.of(
actionSource
.mapOmittingNil { $0 as? AppDelegate.Action }
.map { StateActionPair(state: self.appState, action: $0, modified: false) }
.map { self.appStateActionPair(for: $0) }
.transform(by: AppDelegateTransformer(baseServerUrl: baseServerUrl))
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<MainWindow.Action> }
.map { StateActionPair(state: self.appState, action: $0, modified: false) }
.map { self.appStateActionPair(for: $0) }
.transform(by: UiRootTransformer())
.transform(by: openQuicklyTransformer.forMainWindow)
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? FileMonitor.Action }
.map { StateActionPair(state: self.appState, action: $0, modified: false) }
.map { self.appStateActionPair(for: $0) }
.transform(by: FileMonitorTransformer())
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? OpenQuicklyWindow.Action }
.map { StateActionPair(state: self.appState, action: $0, modified: false) }
.map { self.appStateActionPair(for: $0) }
.transform(by: openQuicklyTransformer.forOpenQuicklyWindow)
.filter { $0.modified }
.map { $0.state }
@ -59,19 +60,12 @@ class StateContext {
})
.addDisposableTo(self.disposeBag)
// MainWindow.State
Observable
.of(
actionSource
.mapOmittingNil { $0 as? UuidAction<MainWindow.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)
}
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
.transform(by: MainWindowTransformer())
.transform(by: previewTransformer.forMainWindow)
.filter { $0.modified }
@ -80,43 +74,19 @@ class StateContext {
.map { $0.state },
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)
}
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
.transform(by: PreviewToolTransformer(baseServerUrl: baseServerUrl))
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<FileBrowser.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)
}
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
.transform(by: FileBrowserTransformer())
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<OpenedFileList.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)
}
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
.transform(by: OpenedFileListTransformer())
.transform(by: previewTransformer.forOpenedFileList)
.filter { $0.modified }
@ -130,23 +100,24 @@ class StateContext {
})
.addDisposableTo(self.disposeBag)
// Preferences
Observable
.of(
actionSource
.mapOmittingNil { $0 as? PrefWindow.Action }
.map { StateActionPair(state: self.appState, action: $0, modified: false) }
.map { self.appStateActionPair(for: $0) }
.transform(by: PrefWindowTransformer())
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? GeneralPref.Action }
.map { StateActionPair(state: self.appState, action: $0, modified: false) }
.map { self.appStateActionPair(for: $0) }
.transform(by: GeneralPrefTransformer())
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? AppearancePref.Action }
.map { StateActionPair(state: self.appState, action: $0, modified: false) }
.map { self.appStateActionPair(for: $0) }
.transform(by: AppearancePrefTransformer())
.filter { $0.modified }
.map { $0.state }
@ -173,6 +144,21 @@ class StateContext {
fileprivate let disposeBag = DisposeBag()
fileprivate var appState: AppState
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 {