Skip to content

Commit 5d6e954

Browse files
committed
refactor(datagrid): collapse cachedTableRows mirror into tableRowsProvider() reads
Removes the cachedTableRows stored property on TableViewCoordinator. It mirrored what tableRowsProvider() returns and was kept in sync via four writers (initial load, updateNSView, updateCache, releaseData). The two-sources-of-truth setup was a parking-lot item from PR #931 — under it, every reader had to trust the cache had been refreshed, and forgetting to refresh produced stale-value bugs. Each reader now captures rows once at the top of its scope: - persistColumnLayoutToStorage - updateCache (the only true cache update; cachedRowCount/Count still derived) - releaseData (just drops the now-removed field) - DataGridView+CellPaste (anchor column count check) - DataGridView+Sort (sortDescriptorsDidChange + menuNeedsUpdate header context menu) - DataGridView+RowActions (undoInsertRow no longer needs the explicit refresh) Net -6 LOC, one fewer field on the coordinator, and no possibility of cache/source drift. Smoke-tested: column header context menu, click-to-sort, cell paste, undo-insert all behave as before.
1 parent 5343e27 commit 5d6e954

5 files changed

Lines changed: 13 additions & 19 deletions

File tree

TablePro/Views/Results/DataGridCoordinator.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ final class TableViewCoordinator: NSObject, NSTableViewDelegate, NSTableViewData
99
{
1010
var tableRowsProvider: @MainActor () -> TableRows = { TableRows() }
1111
var tableRowsMutator: @MainActor (@MainActor (inout TableRows) -> Void) -> Void = { _ in }
12-
var cachedTableRows: TableRows = TableRows()
1312
var changeManager: AnyChangeManager
1413
var isEditable: Bool
1514
var sortedIDs: [RowID]?
@@ -30,14 +29,15 @@ final class TableViewCoordinator: NSObject, NSTableViewDelegate, NSTableViewData
3029
func persistColumnLayoutToStorage() {
3130
guard tabType == .table else { return }
3231
guard let tableView, let connectionId, let tableName, !tableName.isEmpty else { return }
33-
guard !cachedTableRows.columns.isEmpty else { return }
32+
let tableRows = tableRowsProvider()
33+
guard !tableRows.columns.isEmpty else { return }
3434

3535
var widths: [String: CGFloat] = [:]
3636
var order: [String] = []
3737
for column in tableView.tableColumns where column.identifier.rawValue != "__rowNumber__" {
3838
guard let colIndex = DataGridView.dataColumnIndex(from: column.identifier),
39-
colIndex < cachedTableRows.columns.count else { continue }
40-
let name = cachedTableRows.columns[colIndex]
39+
colIndex < tableRows.columns.count else { continue }
40+
let name = tableRows.columns[colIndex]
4141
widths[name] = column.width
4242
order.append(name)
4343
}
@@ -170,7 +170,6 @@ final class TableViewCoordinator: NSObject, NSTableViewDelegate, NSTableViewData
170170

171171
private func releaseData() {
172172
overlayEditor?.dismiss(commit: false)
173-
cachedTableRows = TableRows()
174173
rowVisualStateCache.removeAll()
175174
displayCache.removeAll()
176175
columnDisplayFormats = []
@@ -204,9 +203,9 @@ final class TableViewCoordinator: NSObject, NSTableViewDelegate, NSTableViewData
204203
}
205204

206205
func updateCache() {
207-
cachedTableRows = tableRowsProvider()
208-
cachedRowCount = sortedIDs?.count ?? cachedTableRows.count
209-
cachedColumnCount = cachedTableRows.columns.count
206+
let tableRows = tableRowsProvider()
207+
cachedRowCount = sortedIDs?.count ?? tableRows.count
208+
cachedColumnCount = tableRows.columns.count
210209
}
211210

212211
func applyInsertedRows(_ indices: IndexSet) {

TablePro/Views/Results/DataGridView+RowActions.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ extension TableViewCoordinator {
2929
tableRowsMutator { rows in
3030
_ = rows.remove(at: IndexSet(integer: index))
3131
}
32-
cachedTableRows = tableRowsProvider()
3332
updateCache()
3433
tableView?.reloadData()
3534
}

TablePro/Views/Results/DataGridView.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ struct DataGridView: NSViewRepresentable {
108108
rowNumberColumn.isHidden = !configuration.showRowNumbers
109109

110110
let initialRows = tableRowsProvider()
111-
context.coordinator.cachedTableRows = initialRows
112111

113112
context.coordinator.isRebuildingColumns = true
114113
for (index, columnName) in initialRows.columns.enumerated() {
@@ -226,7 +225,6 @@ struct DataGridView: NSViewRepresentable {
226225
}
227226

228227
let latestRows = tableRowsProvider()
229-
coordinator.cachedTableRows = latestRows
230228
let rowDisplayCount = sortedIDs?.count ?? latestRows.count
231229
let columnCount = latestRows.columns.count
232230

@@ -627,7 +625,6 @@ struct DataGridView: NSViewRepresentable {
627625
coordinator.themeObserver = nil
628626
}
629627
coordinator.tableRowsController.detach()
630-
coordinator.cachedTableRows = TableRows()
631628
}
632629

633630
func makeCoordinator() -> TableViewCoordinator {

TablePro/Views/Results/Extensions/DataGridView+CellPaste.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension TableViewCoordinator {
1616
guard !grid.isEmpty, grid[0].count > 1 || grid.count > 1 else { return false }
1717

1818
let maxRow = min(anchorRow + grid.count, cachedRowCount)
19-
let maxCol = min(anchorColumn + (grid.first?.count ?? 0), cachedTableRows.columns.count)
19+
let maxCol = min(anchorColumn + (grid.first?.count ?? 0), tableRowsProvider().columns.count)
2020
guard anchorRow < maxRow, anchorColumn < maxCol else { return false }
2121

2222
let undoManager = tableView?.window?.undoManager

TablePro/Views/Results/Extensions/DataGridView+Sort.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension TableViewCoordinator {
1515
guard let sortDescriptor = tableView.sortDescriptors.first,
1616
let key = sortDescriptor.key,
1717
let columnIndex = DataGridView.dataColumnIndex(from: NSUserInterfaceItemIdentifier(key)),
18-
columnIndex >= 0 && columnIndex < cachedTableRows.columns.count else {
18+
columnIndex >= 0 && columnIndex < tableRowsProvider().columns.count else {
1919
return
2020
}
2121

@@ -62,11 +62,11 @@ extension TableViewCoordinator {
6262
let column = tableView.tableColumns[columnIndex]
6363
if column.identifier.rawValue == "__rowNumber__" { return }
6464

65-
// Derive base column name from stable identifier (avoids sort indicator in title)
65+
let tableRows = tableRowsProvider()
6666
let baseName: String = {
6767
if let idx = DataGridView.dataColumnIndex(from: column.identifier),
68-
idx < cachedTableRows.columns.count {
69-
return cachedTableRows.columns[idx]
68+
idx < tableRows.columns.count {
69+
return tableRows.columns[idx]
7070
}
7171
return column.title
7272
}()
@@ -103,9 +103,8 @@ extension TableViewCoordinator {
103103
filterItem.target = self
104104
menu.addItem(filterItem)
105105

106-
// "Display As" submenu for value display format overrides
107106
if let dataColumnIndex = DataGridView.dataColumnIndex(from: column.identifier) {
108-
let columnType = dataColumnIndex < cachedTableRows.columnTypes.count ? cachedTableRows.columnTypes[dataColumnIndex] : nil
107+
let columnType = dataColumnIndex < tableRows.columnTypes.count ? tableRows.columnTypes[dataColumnIndex] : nil
109108
let applicableFormats = ValueDisplayFormat.applicableFormats(for: columnType)
110109
if applicableFormats.count > 1 {
111110
let displaySubmenu = NSMenu()

0 commit comments

Comments
 (0)