fix(grid): append infinite scroll result segments

This commit is contained in:
t8y2 committed 2026-07-21 18:45:02 +08:00
1 parent 678033c82e
commit c153e93d54
9 files changed
+303 -30

No files matched your search

@@ -247,13 +247,16 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
async function onPaginate(offset: number, limit: number, whereInput?: string, orderBy?: string) {
const tab = activeTab.value;
if (!tab) return;
const appendResult = settingsStore.editorSettings.infiniteScroll && offset > 0 && offset === tab.result?.rows.length;
const appendOptions = appendResult ? { appendResult: { maxRows: settingsStore.editorSettings.infiniteScrollMaxRows } } : {};
if (tab.mode !== "data") {
const sortColumns = visibleQuerySortColumns(tab.result?.columns ?? [], tab.result?.hidden_column_indexes, tab.resultSortColumnIndex ?? -1);
const hasDatabaseSort = !!tab.result?.hidden_column_indexes?.length && tab.resultSortMode === "database" && !!tab.resultSortDirection && !!tab.resultSortColumn && !!sortColumns;
const baseSql = hasDatabaseSort ? queryResultBaseSql(tab) : queryResultExecutionSql(tab);
if (!baseSql.trim()) return;
const expectedNextOffset = (tab.resultPageOffset ?? 0) + (tab.resultPageLimit ?? limit);
const sessionId = tab.result?.has_more && tab.result?.session_id && offset === expectedNextOffset && limit === tab.resultPageLimit ? tab.result.session_id : undefined;
const expectedNextOffset = appendResult ? tab.result?.rows.length : (tab.resultPageOffset ?? 0) + (tab.resultPageLimit ?? limit);
const continuesResultSession = appendResult ? offset === expectedNextOffset : offset === expectedNextOffset && limit === tab.resultPageLimit;
const sessionId = tab.result?.has_more && tab.result?.session_id && continuesResultSession ? tab.result.session_id : undefined;
const resultBaseSql = queryResultBaseSql(tab);
await queryStore.executeTabSql(tab.id, baseSql, {
resultBaseSql,
@@ -269,6 +272,7 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
}
: {}),
pagination: { offset, limit, sessionId },
...appendOptions,
preserveResultDuringExecution: true,
preserveTotalRowCountDuringExecution: true,
replaceActiveResultInGroup: true,
@@ -282,6 +286,7 @@ export function useDataGridActions(activeTab: ComputedRef<QueryTab | undefined>)
queryStore.updateSql(tab.id, sql);
await queryStore.executeTabSql(tab.id, sql, {
pagination: { offset, limit },
...appendOptions,
preserveResultDuringExecution: true,
preserveTotalRowCountDuringExecution: true,
});
@@ -1,4 +1,4 @@
import { ref, computed, nextTick, watch, getCurrentInstance, onActivated, onBeforeUnmount, onDeactivated, onMounted, type ComputedRef, type Ref } from "vue";
import { ref, computed, nextTick, watch, getCurrentInstance, onActivated, onBeforeUnmount, onDeactivated, onMounted, toRaw, type ComputedRef, type Ref } from "vue";
import * as api from "@/lib/backend/api";
import type { CellValue } from "@/lib/dataGrid/cellValue";
import { coerceDataGridCellValue, dataGridCellEditorText } from "@/lib/dataGrid/dataGridCellCoercion";
@@ -139,10 +139,15 @@ const closingPendingSnapshotTabs = new Set<string>();
const BEFORE_TAB_SWITCH_EVENT = "dbx:before-tab-switch";
const MAX_PENDING_CHANGES_HISTORY = 100;
function dataGridRowsIdentityChanged(previousRows: CellValue[][] | undefined, nextRows: CellValue[][]): boolean {
function dataGridRowsIdentityChanged(previousRows: CellValue[][] | undefined, nextRows: CellValue[][], appendedFromRowCount?: number): boolean {
if (!previousRows) return true;
if (previousRows.length !== nextRows.length) return true;
return previousRows.some((row, index) => row !== nextRows[index]);
if (appendedFromRowCount !== previousRows.length || previousRows.length > nextRows.length) {
if (previousRows.length !== nextRows.length) return true;
return previousRows.some((row, index) => toRaw(row) !== toRaw(nextRows[index]));
}
// Infinite scrolling appends rows without changing existing source indexes.
// Preserve pending edits only when every previously loaded row is the same object.
return previousRows.some((row, index) => toRaw(row) !== toRaw(nextRows[index]));
}
function cacheKeyBelongsToTab(cacheKey: string, tabId: string) {
@@ -1334,13 +1339,13 @@ export function useDataGridEditor(options: UseDataGridEditorOptions) {
exitTransaction();
}
// Pending changes reference rows by sourceIndex. When the result set changes
// (e.g. different WHERE clause, pagination), stale indices point to wrong rows.
// Pending changes reference rows by sourceIndex. Replacements (different WHERE,
// sort, normal pagination, refresh) invalidate them; prefix-only appends do not.
let previousResultRows = result.value.rows;
watch(
() => result.value.rows,
(rows) => {
if (!dataGridRowsIdentityChanged(previousResultRows, rows)) {
() => [result.value.rows, (result.value as { appended_from_row_count?: number }).appended_from_row_count] as const,
([rows, appendedFromRowCount]) => {
if (!dataGridRowsIdentityChanged(previousResultRows, rows, appendedFromRowCount)) {
previousResultRows = rows;
return;
}