Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
19 changes: 14 additions & 5 deletions OpenTable/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {

// Check if main window is being closed
if isMainWindow(window) {
// CRITICAL: Save tab state SYNCHRONOUSLY before any async operations
// Otherwise sessions might be cleared before we save
saveAllTabStates()

// NOW disconnect sessions asynchronously (after save is complete)
// CRITICAL: Post notification FIRST to allow MainContentView to flush pending saves
// This ensures query text is saved before SwiftUI tears down the view
NotificationCenter.default.post(name: .mainWindowWillClose, object: nil)

// Small delay to allow notification handlers to complete
// This is critical - without it, saves may not complete before view is destroyed
Thread.sleep(forTimeInterval: 0.05) // 50ms
Comment thread
datlechin marked this conversation as resolved.
Outdated
Comment thread
datlechin marked this conversation as resolved.
Outdated

// NOTE: We do NOT call saveAllTabStates() here because:
// 1. MainContentView already flushed the correct state via the notification above
// 2. By this point, SwiftUI may have torn down views and session.tabs could be stale/empty
// 3. Saving again would risk overwriting the good state with bad/empty state

// Disconnect sessions asynchronously (after save is complete)
Task { @MainActor in
await DatabaseManager.shared.disconnectAll()
}
Expand Down
8 changes: 2 additions & 6 deletions OpenTable/Core/Storage/TabStateStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ final class TabStateStorage {
let key = tabStateKey(for: connectionId)
defaults.set(data, forKey: key)
} catch {
#if DEBUG
print("[TabStateStorage] Failed to encode tab state: \(error.localizedDescription)")
#endif
// Silent failure - encoding errors are rare and non-critical
}
}

Expand All @@ -53,9 +51,7 @@ final class TabStateStorage {
let decoder = JSONDecoder()
return try decoder.decode(TabState.self, from: data)
} catch {
#if DEBUG
print("[TabStateStorage] Failed to decode tab state: \(error.localizedDescription)")
#endif
// Silent failure - decoding errors return nil
return nil
}
}
Expand Down
3 changes: 3 additions & 0 deletions OpenTable/OpenTableApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ extension Notification.Name {

// History panel notifications
static let toggleHistoryPanel = Notification.Name("toggleHistoryPanel")

// Window lifecycle notifications
static let mainWindowWillClose = Notification.Name("mainWindowWillClose")
}

// MARK: - Open Window Handler
Expand Down
Loading