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

Improve quit handling

This commit is contained in:
Tae Won Ha 2019-03-28 09:49:01 +01:00
parent 9d35de4328
commit 4c2face3d1
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
9 changed files with 42 additions and 60 deletions

View File

@ -9,18 +9,17 @@ import RxSwift
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {}
func applicationDidFinishLaunching(_ aNotification: Notification) {
}
func applicationShouldTerminate(
_: NSApplication
) -> NSApplication.TerminateReply {
let docs = NSDocumentController.shared.documents
if docs.isEmpty { return .terminateNow }
try? Completable
.concat(docs.compactMap { ($0 as? Document)?.quitWithoutSaving() })
.wait()
NSDocumentController.shared
.documents
.compactMap { $0 as? Document }
.forEach { $0.quitWithoutSaving() }
return .terminateNow
}

View File

@ -22,10 +22,11 @@ class Document: NSDocument, NSWindowDelegate {
nvimView
.events
.observeOn(MainScheduler.instance)
.subscribe(onNext: { event in
switch event {
case .neoVimStopped: DispatchQueue.main.async { self.close() }
case .neoVimStopped: self.close()
default: break //Swift.print("Event received: \(event)")
@ -34,12 +35,13 @@ class Document: NSDocument, NSWindowDelegate {
.disposed(by: self.disposeBag)
}
func quitWithoutSaving() -> Completable {
return self.nvimView.quitNeoVimWithoutSaving()
func quitWithoutSaving() {
try? self.nvimView.quitNeoVimWithoutSaving().wait()
self.nvimView.waitTillNvimExits()
}
func windowShouldClose(_ sender: NSWindow) -> Bool {
try? self.quitWithoutSaving().wait()
self.quitWithoutSaving()
return false
}

View File

@ -46,39 +46,3 @@ extension PrimitiveSequence
}
}
}
extension PrimitiveSequence where TraitType == SingleTrait {
func syncValue() -> Element? {
var trigger = false
var value: Element?
let condition = NSCondition()
condition.lock()
defer { condition.unlock() }
let disposable = self.subscribe(onSuccess: { result in
value = result
condition.lock()
defer { condition.unlock() }
trigger = true
condition.broadcast()
}, onError: { error in
condition.lock()
defer { condition.unlock() }
trigger = true
condition.broadcast()
})
while !trigger { condition.wait(until: Date(timeIntervalSinceNow: 5)) }
disposable.dispose()
return value
}
func asCompletable() -> Completable {
return self.asObservable().ignoreElements()
}
}

View File

@ -9,6 +9,10 @@ import MessagePack
extension NvimView {
public func waitTillNvimExits() {
self.nvimExitedCondition.wait(for: 5)
}
public func enterResizeMode() {
self.currentlyResizing = true
self.markForRenderWholeView()

View File

@ -96,7 +96,7 @@ extension NvimView {
@unknown default:
self.log.error("Unknown flush data type")
}
}
@ -128,7 +128,7 @@ extension NvimView {
final func stop() {
self.bridgeLogger.debug()
try? self.api
self.api
.stop()
.andThen(Completable.create { completable in
self.eventsSubject.onNext(.neoVimStopped)
@ -138,12 +138,13 @@ extension NvimView {
return Disposables.create()
})
.andThen(self.bridge.quit())
.observeOn(MainScheduler.instance)
.wait(onCompleted: {
.subscribe(onCompleted: {
self.bridgeLogger.info("Successfully stopped the bridge.")
self.nvimExitedCondition.broadcast()
}, onError: {
self.bridgeLogger.fault("There was an error stopping the bridge: \($0)")
})
.disposed(by: self.disposeBag)
}
final func autoCommandEvent(_ value: MessagePackValue) {
@ -446,7 +447,7 @@ extension NvimView {
@unknown default:
self.log.error("Unknown fatal error from NvimServer")
}
} else {
alert.informativeText = "There was an unknown error launching the " +

View File

@ -217,6 +217,7 @@ public class NvimView: NSView,
let sourceFileUrls: [URL]
let rpcEventSubscriptionCondition = ConditionVariable()
let nvimExitedCondition = ConditionVariable()
// MARK: - Private
private var _linespacing = NvimView.defaultLinespacing

View File

@ -9,11 +9,21 @@ import RxSwift
// MARK: - NvimViewDelegate
extension MainWindow {
func neoVimStopped() {
if self.isClosing {
return
}
// Use only when Cmd-Q'ing
func waitTillNvimExits() {
self.neoVimView.waitTillNvimExits()
}
func neoVimStopped() {
if self.isClosing { return }
self.prepareClosing()
self.windowController.close()
self.set(dirtyStatus: false)
self.emit(self.uuidAction(for: .close))
}
func prepareClosing() {
self.isClosing = true
// If we close the window in the full screen mode, either by clicking the close button or by invoking :q
@ -22,10 +32,6 @@ extension MainWindow {
self.window.toggleFullScreen(nil)
}
self.windowController.close()
self.set(dirtyStatus: false)
self.emit(self.uuidAction(for: .close))
guard let cliPipePath = self.cliPipePath, FileManager.default.fileExists(atPath: cliPipePath) else {
return
}

View File

@ -224,6 +224,7 @@ class MainWindow: NSObject,
self.emit(uuidAction(for: .setTheme(theme)))
}
// MARK: - Private
private var currentBuffer: NvimView.Buffer?
private var goToLineFromCli: Marked<Int>?

View File

@ -73,9 +73,13 @@ class UiRoot: UiComponent {
// The following should only be used when Cmd-Q'ing
func prepareQuit() {
self.mainWindows.values.forEach { $0.prepareClosing() }
try? Completable
.concat(self.mainWindows.values.map { $0.quitNeoVimWithoutSaving() })
.wait()
self.mainWindows.values.forEach { $0.waitTillNvimExits() }
}
private let source: Observable<AppState>