From 511e3685abfd1041490f4abd4412190648e88e13 Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Mon, 6 Feb 2017 18:57:50 +0100 Subject: [PATCH] Show no preview initially --- VimR/AppDelegate.swift | 7 +++++-- VimR/AppDelegateTransformer.swift | 7 +++++++ VimR/Context.swift | 13 +++++++------ VimR/HttpServerService.swift | 6 +++--- VimR/States.swift | 7 ++++--- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/VimR/AppDelegate.swift b/VimR/AppDelegate.swift index 4a63b51d..9ad4706d 100644 --- a/VimR/AppDelegate.swift +++ b/VimR/AppDelegate.swift @@ -52,10 +52,13 @@ class AppDelegate: NSObject, NSApplicationDelegate { fileprivate var launching = true override init() { + let initialAppState = AppState.default + self.stateContext = StateContext(initialAppState) + let source = self.stateContext.stateSource self.uiRoot = UiRoot(source: source, emitter: self.stateContext.actionEmitter, - state: AppState.default) // FIXME + state: initialAppState) // FIXME self.actionSink = self.actionSubject.asObservable() self.changeSink = self.changeSubject.asObservable() @@ -140,7 +143,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } - fileprivate let stateContext = StateContext() + fileprivate let stateContext: StateContext fileprivate let uiRoot: UiRoot fileprivate var hasDirtyWindows = true fileprivate var hasMainWindows = false diff --git a/VimR/AppDelegateTransformer.swift b/VimR/AppDelegateTransformer.swift index 697f6166..ba0f181b 100644 --- a/VimR/AppDelegateTransformer.swift +++ b/VimR/AppDelegateTransformer.swift @@ -10,6 +10,10 @@ class AppDelegateTransformer: Transformer { typealias Pair = StateActionPair + init(baseServerUrl: URL) { + self.baseServerUrl = baseServerUrl + } + func transform(_ source: Observable) -> Observable { return source.map { pair in var state = pair.state @@ -21,6 +25,7 @@ class AppDelegateTransformer: Transformer { mainWindow.uuid = UUID().uuidString mainWindow.urlsToOpen = urls.toDict { url in MainWindow.OpenMode.default } mainWindow.cwd = cwd + mainWindow.preview.server = self.baseServerUrl.appendingPathComponent("/tools/preview/none") state.mainWindows[mainWindow.uuid] = mainWindow @@ -33,4 +38,6 @@ class AppDelegateTransformer: Transformer { } } } + + fileprivate let baseServerUrl: URL } diff --git a/VimR/Context.swift b/VimR/Context.swift index 5bdb11e4..14dcf9ba 100644 --- a/VimR/Context.swift +++ b/VimR/Context.swift @@ -11,13 +11,14 @@ class StateContext { let stateSource: Observable let actionEmitter = Emitter() - init() { + init(_ initialState: AppState) { + self.appState = initialState self.stateSource = self.stateSubject.asObservable() let actionSource = self.actionEmitter.observable - let port = NetUtils.openPort() - self.previewTransformer = PreviewTransformer(baseServerUrl: URL(string: "http://localhost:\(port)")!) - self.httpServerService = HttpServerService(port: port) + self.appDelegateTransformer = AppDelegateTransformer(baseServerUrl: initialState.baseServerUrl) + self.previewTransformer = PreviewTransformer(baseServerUrl: initialState.baseServerUrl) + self.httpServerService = HttpServerService(port: initialState.baseServerUrl.port ?? 0) Observable .of( @@ -74,9 +75,9 @@ class StateContext { fileprivate let scheduler = SerialDispatchQueueScheduler(qos: .userInitiated) fileprivate let disposeBag = DisposeBag() - fileprivate var appState = AppState.default + fileprivate var appState: AppState - fileprivate let appDelegateTransformer = AppDelegateTransformer() + fileprivate let appDelegateTransformer: AppDelegateTransformer fileprivate let uiRootTransformer = UiRootTransformer() fileprivate let mainWindowTransformer = MainWindowTransformer() fileprivate let previewTransformer: PreviewTransformer diff --git a/VimR/HttpServerService.swift b/VimR/HttpServerService.swift index b80e0dcf..fdeea7f7 100644 --- a/VimR/HttpServerService.swift +++ b/VimR/HttpServerService.swift @@ -24,10 +24,10 @@ class HttpServerService: Service { typealias Element = StateActionPair, MainWindow.Action> - init(port: in_port_t) { + init(port: Int) { self.port = port do { - try self.server.start(port) + try self.server.start(in_port_t(port)) NSLog("server started on http://localhost:\(port)") self.server["/tools/preview/error"] = { r in .ok(.html("ERROR!")) } @@ -64,5 +64,5 @@ class HttpServerService: Service { fileprivate let server = HttpServer() fileprivate let resourceBaesUrl = Bundle.main.resourceURL!.appendingPathComponent("markdown") - fileprivate let port: in_port_t + fileprivate let port: Int } diff --git a/VimR/States.swift b/VimR/States.swift index 9953bc3f..903baefa 100644 --- a/VimR/States.swift +++ b/VimR/States.swift @@ -98,15 +98,16 @@ protocol Service { struct AppState { - static let `default` = AppState(mainWindow: MainWindow.State.default) + static let `default` = AppState(baseServerUrl: URL(string: "http://localhost:\(NetUtils.openPort())")!, + mainWindow: MainWindow.State.default) var currentMainWindow: MainWindow.State var mainWindows: [String: MainWindow.State] = [:] let baseServerUrl: URL - init(mainWindow: MainWindow.State) { - self.baseServerUrl = URL(string: "http://localhost:\(NetUtils.openPort())")! + init(baseServerUrl: URL, mainWindow: MainWindow.State) { + self.baseServerUrl = baseServerUrl self.currentMainWindow = mainWindow } }