-
-
Notifications
You must be signed in to change notification settings - Fork 288
fix(datagrid): resolve default sort before dispatching the first table query #1605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import Foundation | ||
|
|
||
| @MainActor | ||
| final class SchemaColumnStore { | ||
| typealias Entry = (columns: [String], primaryKeys: [String]) | ||
|
|
||
| private var entries: [String: Entry] = [:] | ||
| private var loads: [String: Task<Void, Never>] = [:] | ||
| private var generation = 0 | ||
|
|
||
| func cached(_ key: String) -> Entry? { | ||
| entries[key] | ||
| } | ||
|
|
||
| func store(_ entry: Entry, for key: String) { | ||
| entries[key] = entry | ||
| } | ||
|
|
||
| func load(_ key: String, fetch: @escaping () async -> Entry?) async { | ||
| if entries[key] != nil { return } | ||
| if let inFlight = loads[key] { | ||
| await inFlight.value | ||
| return | ||
| } | ||
|
|
||
| let task = Task { | ||
| if let entry = await fetch() { | ||
| self.entries[key] = entry | ||
| } | ||
| } | ||
| loads[key] = task | ||
| let startedGeneration = generation | ||
| await task.value | ||
| if generation == startedGeneration { | ||
| loads[key] = nil | ||
| } | ||
| } | ||
|
|
||
| func removeAll() { | ||
| generation += 1 | ||
| for task in loads.values { task.cancel() } | ||
| loads.removeAll() | ||
| entries.removeAll() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 0 additions & 65 deletions
65
TablePro/Views/Main/Extensions/MainContentCoordinator+DefaultSort.swift
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
TablePro/Views/Main/Extensions/MainContentCoordinator+TableFirstLoad.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // | ||
| // MainContentCoordinator+TableFirstLoad.swift | ||
| // TablePro | ||
| // | ||
|
|
||
| import Foundation | ||
| import TableProPluginKit | ||
|
|
||
| extension MainContentCoordinator { | ||
| func openTableTabQuery(tabId: UUID) async { | ||
| guard await prepareTableTabFirstLoad(tabId: tabId) else { return } | ||
| executeTableTabQueryDirectly() | ||
| } | ||
|
|
||
| @discardableResult | ||
| func prepareTableTabFirstLoad(tabId: UUID) async -> Bool { | ||
| guard tabManager.selectedTabId == tabId, | ||
| let tab = tabManager.tabs.first(where: { $0.id == tabId }), | ||
| tab.tabType == .table, | ||
| let tableName = tab.tableContext.tableName, !tableName.isEmpty else { return false } | ||
|
|
||
| let hint = PluginManager.shared.defaultSortHint(for: connection.type, table: tableName) | ||
| guard firstLoadNeedsSchemaColumns(for: tab, hint: hint) else { return true } | ||
|
|
||
| await loadSchemaColumns(for: tableName, schema: tab.tableContext.schemaName) | ||
|
|
||
| guard !Task.isCancelled, | ||
| tabManager.selectedTabId == tabId, | ||
| let index = tabManager.tabs.firstIndex(where: { $0.id == tabId }), | ||
| tabManager.tabs[index].tableContext.tableName == tableName else { return false } | ||
|
|
||
| let sortApplied = applyResolvedDefaultSort(at: index, hint: hint) | ||
| if sortApplied || !tabManager.tabs[index].columnLayout.hiddenColumns.isEmpty { | ||
| filterCoordinator.rebuildTableQuery(at: index) | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func firstLoadNeedsSchemaColumns(for tab: QueryTab, hint: DefaultSortHint) -> Bool { | ||
| wantsDefaultSort(for: tab, hint: hint) || !tab.columnLayout.hiddenColumns.isEmpty | ||
| } | ||
|
|
||
| func wantsDefaultSort(for tab: QueryTab, hint: DefaultSortHint) -> Bool { | ||
| guard tab.tabType == .table, | ||
| !tab.sortState.isSorting, | ||
| let tableName = tab.tableContext.tableName, !tableName.isEmpty else { | ||
| return false | ||
| } | ||
|
|
||
| switch hint { | ||
| case .suppress: | ||
| return false | ||
| case .forceColumns: | ||
| return true | ||
| case .useAppDefault: | ||
| return AppSettingsManager.shared.dataGrid.defaultSortBehavior != .none | ||
| } | ||
| } | ||
|
|
||
| private func applyResolvedDefaultSort(at index: Int, hint: DefaultSortHint) -> Bool { | ||
| let tab = tabManager.tabs[index] | ||
| guard wantsDefaultSort(for: tab, hint: hint) else { return false } | ||
|
|
||
| let resolved = DefaultSortResolver.resolveSortState( | ||
| behavior: AppSettingsManager.shared.dataGrid.defaultSortBehavior, | ||
| pluginHint: hint, | ||
| primaryKeyColumns: resolvedPrimaryKeyColumns(for: tab), | ||
| allColumns: effectiveResultColumns(for: tab) | ||
| ) | ||
| guard resolved.isSorting else { return false } | ||
|
|
||
| tabManager.mutate(at: index) { | ||
| $0.sortState = resolved | ||
| $0.pagination.reset() | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| private func resolvedPrimaryKeyColumns(for tab: QueryTab) -> [String] { | ||
| if let pks = cachedSchemaColumns(for: tab)?.primaryKeys, !pks.isEmpty { | ||
| return pks | ||
| } | ||
| if let defaultPK = PluginManager.shared.defaultPrimaryKeyColumn(for: connection.type) { | ||
| return [defaultPK] | ||
| } | ||
| return [] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
refreshTables()or teardown callsschemaColumns.removeAll(), this store cancels in-flight loads and clearsentries, but any metadata fetch that does not promptly observe cancellation can still resume and write its old result here. That means a schema refresh can be followed by the previousfetchColumnsresult reappearing under the same key, so later default-sort or hidden-column logic can use stale column/PK metadata until another invalidation; the generation check only controlsloadscleanup and does not guard the cache write.Useful? React with 👍 / 👎.