1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-25 23:02:35 +03:00

GH-405 Reduce some boiler plate

This commit is contained in:
Tae Won Ha 2017-04-22 11:31:42 +02:00
parent f13a765979
commit f1b72d7d44
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44

View File

@ -16,7 +16,6 @@ class Context {
self.appState = state
self.stateSource = self.stateSubject.asObservable()
let actionSource = self.actionEmitter.observable
self.httpService = HttpServerService(port: baseServerUrl.port!)
@ -30,32 +29,21 @@ class Context {
// AppState
Observable
.of(
actionSource
.mapOmittingNil { $0 as? AppDelegate.Action }
.map { self.appStateActionPair(for: $0) }
.reduce(by: AppDelegateReducer(baseServerUrl: baseServerUrl))
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<MainWindow.Action> }
.map { self.appStateActionPair(for: $0) }
.reduce(by: UiRootReducer())
.reduce(by: openQuicklyReducer.forMainWindow)
.filter { $0.modified }
.apply(to: prefService.forMainWindow)
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? FileMonitor.Action }
.map { self.appStateActionPair(for: $0) }
.reduce(by: FileMonitorReducer())
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? OpenQuicklyWindow.Action }
.map { self.appStateActionPair(for: $0) }
.reduce(by: openQuicklyReducer.forOpenQuicklyWindow)
.filter { $0.modified }
.map { $0.state }
self.actionSourceForAppState()
.reduce(by: AppDelegateReducer(baseServerUrl: baseServerUrl))
.filterMapPair(),
self.actionSourceForAppState()
.reduce(by: UiRootReducer())
.reduce(by: openQuicklyReducer.forMainWindow)
.filter { $0.modified }
.apply(to: prefService.forMainWindow)
.map { $0.state },
self.actionSourceForAppState()
.reduce(by: FileMonitorReducer())
.filterMapPair(),
self.actionSourceForAppState()
.reduce(by: openQuicklyReducer.forOpenQuicklyWindow)
.filterMapPair()
)
.merge()
.subscribe(onNext: self.emitAppState)
@ -64,42 +52,30 @@ class Context {
// MainWindow.State
Observable
.of(
actionSource
.mapOmittingNil { $0 as? UuidAction<MainWindow.Action> }
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
.reduce(by: MainWindowReducer())
.reduce(by: markdownReducer.forMainWindow)
.filter { $0.modified }
.apply(to: previewService.forMainWindow)
.apply(to: self.httpService.forMainWindow)
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<PreviewTool.Action> }
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
.reduce(by: PreviewToolReducer(baseServerUrl: baseServerUrl))
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<HtmlPreviewTool.Action> }
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
.reduce(by: htmlPreviewToolReducer)
.filter { $0.modified }
.apply(to: self.httpService.forHtmlPreviewTool)
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<FileBrowser.Action> }
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
.reduce(by: FileBrowserReducer())
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? UuidAction<OpenedFileList.Action> }
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
.reduce(by: OpenedFileListReducer())
.reduce(by: markdownReducer.forOpenedFileList)
.filter { $0.modified }
.apply(to: previewService.forOpenedFileList)
.map { $0.state }
self.actionSourceForMainWindow()
.reduce(by: MainWindowReducer())
.reduce(by: markdownReducer.forMainWindow)
.filter { $0.modified }
.apply(to: previewService.forMainWindow)
.apply(to: self.httpService.forMainWindow)
.map { $0.state },
self.actionSourceForMainWindow()
.reduce(by: PreviewToolReducer(baseServerUrl: baseServerUrl))
.filterMapPair(),
self.actionSourceForMainWindow()
.reduce(by: htmlPreviewToolReducer)
.filter { $0.modified }
.apply(to: self.httpService.forHtmlPreviewTool)
.map { $0.state },
self.actionSourceForMainWindow()
.reduce(by: FileBrowserReducer())
.filterMapPair(),
self.actionSourceForMainWindow()
.reduce(by: OpenedFileListReducer())
.reduce(by: markdownReducer.forOpenedFileList)
.filter { $0.modified }
.apply(to: previewService.forOpenedFileList)
.map { $0.state }
)
.merge()
.subscribe(onNext: self.emitAppState)
@ -108,31 +84,19 @@ class Context {
// Preferences
Observable
.of(
actionSource
.mapOmittingNil { $0 as? PrefWindow.Action }
.map { self.appStateActionPair(for: $0) }
.reduce(by: PrefWindowReducer())
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? GeneralPref.Action }
.map { self.appStateActionPair(for: $0) }
.reduce(by: GeneralPrefReducer())
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? AppearancePref.Action }
.map { self.appStateActionPair(for: $0) }
.reduce(by: AppearancePrefReducer())
.filter { $0.modified }
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? AdvancedPref.Action }
.map { self.appStateActionPair(for: $0) }
.reduce(by: AdvancedPrefReducer())
.filter { $0.modified }
.map { $0.state }
)
self.actionSourceForAppState()
.reduce(by: PrefWindowReducer())
.filterMapPair(),
self.actionSourceForAppState()
.reduce(by: GeneralPrefReducer())
.filterMapPair(),
self.actionSourceForAppState()
.reduce(by: AppearancePrefReducer())
.filterMapPair(),
self.actionSourceForAppState()
.reduce(by: AdvancedPrefReducer())
.filterMapPair()
)
.merge()
.apply(to: prefService.forPrefPanes)
.subscribe(onNext: self.emitAppState)
@ -140,13 +104,7 @@ class Context {
#if DEBUG
// actionSource.debug().subscribe().addDisposableTo(self.disposeBag)
// stateSource
// .filter { $0.mainWindows.values.count > 0 }
// .map { Array($0.mainWindows.values)[0].preview }
// .debug()
// .subscribe(onNext: { state in
// })
// .addDisposableTo(self.disposeBag)
// stateSource.debug().subscribe().addDisposableTo(self.disposeBag)
#endif
}
@ -183,6 +141,18 @@ class Context {
}
}
fileprivate func actionSourceForAppState<T>() -> Observable<StateActionPair<AppState, T>> {
return self.actionEmitter.observable
.mapOmittingNil { $0 as? T }
.map { self.appStateActionPair(for: $0) }
}
fileprivate func actionSourceForMainWindow<T>() -> Observable<StateActionPair<UuidState<MainWindow.State>, T>> {
return self.actionEmitter.observable
.mapOmittingNil { $0 as? UuidAction<T> }
.mapOmittingNil { self.mainWindowStateActionPair(for: $0) }
}
fileprivate func appStateActionPair<ActionType>(for action: ActionType) -> StateActionPair<AppState, ActionType> {
return StateActionPair(state: self.appState, action: action, modified: false)
}
@ -201,17 +171,10 @@ class Context {
extension Observable {
fileprivate func reduce<T:Reducer>(by transformer: T) -> Observable<Element> where T.Pair == Element {
return transformer.reduce(self)
fileprivate func reduce<T:Reducer>(by reducer: T) -> Observable<Element> where T.Pair == Element {
return reducer.reduce(self)
}
// 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)
// }
// }
fileprivate func apply<S:Service>(to service: S) -> Observable<Element> where S.Pair == Element {
return self.do(onNext: service.apply)
}
@ -219,4 +182,10 @@ extension Observable {
fileprivate func apply<S:StateService>(to service: S) -> Observable<Element> where S.StateType == Element {
return self.do(onNext: service.apply)
}
fileprivate func filterMapPair<S, A>() -> Observable<S> where Element == StateActionPair<S, A> {
return self
.filter { $0.modified }
.map { $0.state }
}
}