Skip to content

Commit 0da8bb1

Browse files
committed
wip
1 parent 4f48d8a commit 0da8bb1

File tree

5 files changed

+285
-38
lines changed

5 files changed

+285
-38
lines changed

OpenTable/AppDelegate.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {
6161

6262
// Check if main window is being closed
6363
if isMainWindow(window) {
64-
// CRITICAL: Save tab state SYNCHRONOUSLY before any async operations
65-
// Otherwise sessions might be cleared before we save
66-
saveAllTabStates()
67-
68-
// NOW disconnect sessions asynchronously (after save is complete)
64+
// CRITICAL: Post notification FIRST to allow MainContentView to flush pending saves
65+
// This ensures query text is saved before SwiftUI tears down the view
66+
NotificationCenter.default.post(name: .mainWindowWillClose, object: nil)
67+
68+
// Small delay to allow notification handlers to complete
69+
// This is critical - without it, saves may not complete before view is destroyed
70+
Thread.sleep(forTimeInterval: 0.05) // 50ms
71+
72+
// NOTE: We do NOT call saveAllTabStates() here because:
73+
// 1. MainContentView already flushed the correct state via the notification above
74+
// 2. By this point, SwiftUI may have torn down views and session.tabs could be stale/empty
75+
// 3. Saving again would risk overwriting the good state with bad/empty state
76+
77+
// Disconnect sessions asynchronously (after save is complete)
6978
Task { @MainActor in
7079
await DatabaseManager.shared.disconnectAll()
7180
}

OpenTable/Core/Storage/TabStateStorage.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,27 @@ final class TabStateStorage {
2929
let persistedTabs = tabs.map { $0.toPersistedTab() }
3030
let tabState = TabState(tabs: persistedTabs, selectedTabId: selectedTabId)
3131

32+
#if DEBUG
33+
print("[TabStateStorage] Saving \(tabs.count) tabs for connection \(connectionId)")
34+
for (index, tab) in tabs.enumerated() {
35+
print("[TabStateStorage] Tab \(index): \"\(tab.title)\" - Query: \"\(tab.query.prefix(50))\"")
36+
37+
// DEBUG: Print call stack when saving empty query
38+
if tab.query.isEmpty {
39+
print("[TabStateStorage] ⚠️ WARNING: Saving empty query! Call stack:")
40+
Thread.callStackSymbols.prefix(10).forEach { print(" \($0)") }
41+
}
42+
}
43+
#endif
44+
3245
do {
3346
let encoder = JSONEncoder()
3447
let data = try encoder.encode(tabState)
3548
let key = tabStateKey(for: connectionId)
3649
defaults.set(data, forKey: key)
50+
#if DEBUG
51+
print("[TabStateStorage] ✅ Saved to disk successfully")
52+
#endif
3753
} catch {
3854
#if DEBUG
3955
print("[TabStateStorage] Failed to encode tab state: \(error.localizedDescription)")

OpenTable/OpenTableApp.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ extension Notification.Name {
270270

271271
// History panel notifications
272272
static let toggleHistoryPanel = Notification.Name("toggleHistoryPanel")
273+
274+
// Window lifecycle notifications
275+
static let mainWindowWillClose = Notification.Name("mainWindowWillClose")
273276
}
274277

275278
// MARK: - Open Window Handler

0 commit comments

Comments
 (0)