Skip to content

Commit 7f25c46

Browse files
committed
refactor(datagrid): merge sortCache into coordinator.querySortCache
The view-side @State sortCache (in MainEditorContentView) and the coordinator's @ObservationIgnored querySortCache stored byte-identical SortedRowsCache / QuerySortCacheEntry structs keyed by tab ID. Two write paths fed them: sync inline sort (small tables, ≤1000 rows) wrote to the view-side cache; async background sort (large tables) wrote to the coordinator-side cache. The read in sortedIDsForTab checked both. The split was historical, not architectural. Both caches share the same lifecycle (window/coordinator) and the row-mutation invalidations in MainContentCoordinator+RowOperations only cleared querySortCache — meaning a sync-sorted small table would silently return a stale sortedIDs after add/delete/undo. Bug fixed as a side effect of the merge. Drops: - private struct SortedRowsCache - @State var sortCache from MainEditorContentView - The two-step cache cleanup in onChange(of: tabStructureVersion) and onTeardown - The sortCache lookup branch in sortedIDsForTab The sync sort path now writes directly to coordinator.querySortCache. cleanupSortCache and the row-mutation invalidations cover both paths uniformly. -23 LOC.
1 parent aa4a260 commit 7f25c46

1 file changed

Lines changed: 2 additions & 33 deletions

File tree

TablePro/Views/Main/Child/MainEditorContentView.swift

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ import AppKit
1010
import CodeEditSourceEditor
1111
import SwiftUI
1212

13-
/// Cache for sorted query result rows to avoid re-sorting on every SwiftUI body evaluation.
14-
/// Stores a permutation of `RowID` so the grid keeps the same display order even after
15-
/// inserts and deletes mutate the underlying TableRows storage.
16-
private struct SortedRowsCache {
17-
let sortedIDs: [RowID]
18-
let columnIndex: Int
19-
let direction: SortDirection
20-
let schemaVersion: Int
21-
}
22-
23-
/// Main editor content with tab bar and content switching
2413
struct MainEditorContentView: View {
2514
// MARK: - Dependencies
2615

@@ -58,10 +47,6 @@ struct MainEditorContentView: View {
5847
let onOffsetChange: (Int) -> Void
5948
let onPaginationGo: () -> Void
6049

61-
// MARK: - Sort Cache
62-
63-
@State private var sortCache: [UUID: SortedRowsCache] = [:]
64-
6550
@State private var cachedChangeManager: AnyChangeManager?
6651
@State private var erDiagramViewModels: [UUID: ERDiagramViewModel] = [:]
6752
@State private var serverDashboardViewModels: [UUID: ServerDashboardViewModel] = [:]
@@ -118,14 +103,7 @@ struct MainEditorContentView: View {
118103
favoriteDialogQuery = FavoriteDialogQuery(query: query)
119104
}
120105
.onChange(of: tabManager.tabStructureVersion) { _, _ in
121-
let newIds = tabManager.tabIds
122-
guard !sortCache.isEmpty || !erDiagramViewModels.isEmpty
123-
|| !serverDashboardViewModels.isEmpty else {
124-
coordinator.cleanupSortCache(openTabIds: Set(newIds))
125-
return
126-
}
127-
let openTabIds = Set(newIds)
128-
sortCache = sortCache.filter { openTabIds.contains($0.key) }
106+
let openTabIds = Set(tabManager.tabIds)
129107
coordinator.cleanupSortCache(openTabIds: openTabIds)
130108
erDiagramViewModels = erDiagramViewModels.filter { openTabIds.contains($0.key) }
131109
serverDashboardViewModels = serverDashboardViewModels.filter { openTabIds.contains($0.key) }
@@ -140,7 +118,6 @@ struct MainEditorContentView: View {
140118
refreshDataTabDelegateMutableRefs()
141119
coordinator.dataTabDelegate = dataTabDelegate
142120
coordinator.onTeardown = { [self] in
143-
sortCache.removeAll()
144121
cachedChangeManager = nil
145122
coordinator.dataTabDelegate = nil
146123
}
@@ -636,14 +613,6 @@ struct MainEditorContentView: View {
636613
return nil
637614
}
638615

639-
if let cached = sortCache[tab.id],
640-
cached.columnIndex == (tab.sortState.columnIndex ?? -1),
641-
cached.direction == tab.sortState.direction,
642-
cached.schemaVersion == tab.schemaVersion
643-
{
644-
return cached.sortedIDs
645-
}
646-
647616
let sortColumns = tab.sortState.columns
648617
let storageRows = resolvedRows.rows
649618
let sortedIndices = Array(storageRows.indices).sorted { idx1, idx2 in
@@ -666,7 +635,7 @@ struct MainEditorContentView: View {
666635
}
667636
let sortedIDs = sortedIndices.map { storageRows[$0].id }
668637

669-
sortCache[tab.id] = SortedRowsCache(
638+
coordinator.querySortCache[tab.id] = QuerySortCacheEntry(
670639
sortedIDs: sortedIDs,
671640
columnIndex: tab.sortState.columnIndex ?? -1,
672641
direction: tab.sortState.direction,

0 commit comments

Comments
 (0)