From 8883a6b3b7078fe8348b5f809b099f1ec01e1d5f Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Sun, 5 Jul 2026 02:45:46 +0800 Subject: [PATCH] fix(desktop): format loading elapsed time --- apps/desktop/src/components/grid/DataGrid.vue | 20 ++++++++++++------- .../src/components/layout/ContentArea.vue | 19 +++++++++++++----- .../lib/__tests__/common/elapsedTime.spec.ts | 15 ++++++++++++++ apps/desktop/src/lib/common/elapsedTime.ts | 3 +++ 4 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 apps/desktop/src/lib/__tests__/common/elapsedTime.spec.ts create mode 100644 apps/desktop/src/lib/common/elapsedTime.ts diff --git a/apps/desktop/src/components/grid/DataGrid.vue b/apps/desktop/src/components/grid/DataGrid.vue index 6901ac5e9d..26604f593c 100644 --- a/apps/desktop/src/components/grid/DataGrid.vue +++ b/apps/desktop/src/components/grid/DataGrid.vue @@ -87,6 +87,7 @@ import TemporalCellEditor from "@/components/grid/TemporalCellEditor.vue"; import EnumCellEditor from "@/components/grid/EnumCellEditor.vue"; import type { QueryResult, ColumnInfo, DatabaseType, ForeignKeyInfo, IndexInfo, TriggerInfo, TableInfoTab } from "@/types/database"; import * as api from "@/lib/backend/api"; +import { formatElapsedSeconds } from "@/lib/common/elapsedTime"; import { dataGridCellDisplayText, dataGridCellEditorText } from "@/lib/dataGrid/dataGridCellCoercion"; import { createColumnDrafts } from "@/lib/table/tableStructureEditorState"; import type { BuildSingleColumnAlterSqlOptions } from "@/lib/table/tableStructureEditorSql"; @@ -7652,12 +7653,14 @@ function onDetailResizeEnd() { } const loadingElapsed = ref(0); -let _loadingTimer: ReturnType | undefined; +let _loadingFrame: number | undefined; let _loadingStart = 0; function stopLoadingElapsedTimer() { - clearInterval(_loadingTimer); - _loadingTimer = undefined; + if (_loadingFrame !== undefined) { + window.cancelAnimationFrame(_loadingFrame); + _loadingFrame = undefined; + } } function startLoadingElapsedTimer() { @@ -7665,9 +7668,12 @@ function startLoadingElapsedTimer() { if (!dataGridIsActive || !props.loading) return; _loadingStart = Date.now(); loadingElapsed.value = 0; - _loadingTimer = setInterval(() => { + const updateOnNextFrame = () => { + if (!dataGridIsActive || !props.loading) return; loadingElapsed.value = Date.now() - _loadingStart; - }, 100); + _loadingFrame = window.requestAnimationFrame(updateOnNextFrame); + }; + _loadingFrame = window.requestAnimationFrame(updateOnNextFrame); } watch( @@ -7716,7 +7722,7 @@ onUnmounted(() => { if (serverFilterSearchTimer !== undefined) { window.clearTimeout(serverFilterSearchTimer); } - clearInterval(_loadingTimer); + stopLoadingElapsedTimer(); }); const filteredColumns = computed(() => { @@ -9496,7 +9502,7 @@ const gridContextMenuItems = computed(() => {
- {{ (loadingElapsed / 1000).toFixed(1) }}s + {{ formatElapsedSeconds(loadingElapsed) }}s
diff --git a/apps/desktop/src/components/layout/ContentArea.vue b/apps/desktop/src/components/layout/ContentArea.vue index 56c0455cab..4e82e0ceac 100644 --- a/apps/desktop/src/components/layout/ContentArea.vue +++ b/apps/desktop/src/components/layout/ContentArea.vue @@ -67,6 +67,7 @@ import { buildMongoUpdateDocument, formatMongoShellLiteral, type MongoInputValue import type { SqlExecutionOverride } from "@/lib/sql/sqlExecutionTarget"; import type { DataGridSortMode } from "@/lib/dataGrid/dataGridSort"; import { useTabScroll } from "@/composables/useTabScroll"; +import { formatElapsedSeconds } from "@/lib/common/elapsedTime"; import type { CustomSaveHandler } from "@/composables/useDataGridEditor"; import type { QueryTab, ConnectionConfig, TableInfoTab, TreeNode, VectorCollectionMeta } from "@/types/database"; import { sqlFormatDialectForDbType, type SqlFormatDialect } from "@/lib/sql/sqlFormatter"; @@ -376,11 +377,13 @@ function onResultsResized(payload: { panes: { size: number }[] }) { safeLocalStorageSet("dbx-results-pane-size", String(resultsPane.size)); } } -let queryRunningElapsedTimer: ReturnType | undefined; +let queryRunningElapsedFrame: number | undefined; function stopQueryRunningElapsedTimer() { - clearInterval(queryRunningElapsedTimer); - queryRunningElapsedTimer = undefined; + if (queryRunningElapsedFrame !== undefined) { + window.cancelAnimationFrame(queryRunningElapsedFrame); + queryRunningElapsedFrame = undefined; + } } function updateQueryRunningElapsed() { @@ -392,10 +395,16 @@ function startQueryRunningElapsedTimer() { stopQueryRunningElapsedTimer(); updateQueryRunningElapsed(); if (!props.activeTab.isExecuting || !props.activeTab.queryExecutionStartedAt) return; - queryRunningElapsedTimer = setInterval(updateQueryRunningElapsed, 100); + const updateOnNextFrame = () => { + updateQueryRunningElapsed(); + if (props.activeTab.isExecuting && props.activeTab.queryExecutionStartedAt) { + queryRunningElapsedFrame = window.requestAnimationFrame(updateOnNextFrame); + } + }; + queryRunningElapsedFrame = window.requestAnimationFrame(updateOnNextFrame); } -const queryRunningElapsedSeconds = computed(() => (queryRunningElapsed.value / 1000).toFixed(1)); +const queryRunningElapsedSeconds = computed(() => formatElapsedSeconds(queryRunningElapsed.value)); watch(() => [props.activeTab.id, props.activeTab.isExecuting, props.activeTab.queryExecutionStartedAt] as const, startQueryRunningElapsedTimer, { immediate: true }); diff --git a/apps/desktop/src/lib/__tests__/common/elapsedTime.spec.ts b/apps/desktop/src/lib/__tests__/common/elapsedTime.spec.ts new file mode 100644 index 0000000000..e6e648829c --- /dev/null +++ b/apps/desktop/src/lib/__tests__/common/elapsedTime.spec.ts @@ -0,0 +1,15 @@ +import { describe, expect, it } from "vitest"; +import { formatElapsedSeconds } from "@/lib/common/elapsedTime"; + +describe("formatElapsedSeconds", () => { + it("formats elapsed milliseconds with two fractional second digits", () => { + expect(formatElapsedSeconds(1234)).toBe("1.23"); + expect(formatElapsedSeconds(5)).toBe("0.01"); + expect(formatElapsedSeconds(0)).toBe("0.00"); + }); + + it("clamps invalid elapsed values to zero", () => { + expect(formatElapsedSeconds(-100)).toBe("0.00"); + expect(formatElapsedSeconds(Number.NaN)).toBe("0.00"); + }); +}); diff --git a/apps/desktop/src/lib/common/elapsedTime.ts b/apps/desktop/src/lib/common/elapsedTime.ts new file mode 100644 index 0000000000..e814a7bba8 --- /dev/null +++ b/apps/desktop/src/lib/common/elapsedTime.ts @@ -0,0 +1,3 @@ +export function formatElapsedSeconds(ms: number): string { + return (Math.max(0, Number.isFinite(ms) ? ms : 0) / 1000).toFixed(2); +}