Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(mqtt): adjust default console polling interval to 1s and support interval selection (#12829) (#12851)

lxk955e8bee1ca76ba
diff --git a/apps/desktop/src/components/mqtt/MqttAdminConsole.vue b/apps/desktop/src/components/mqtt/MqttAdminConsole.vue
index 2fa0f25144..3970afd485 100644
--- a/apps/desktop/src/components/mqtt/MqttAdminConsole.vue
+++ b/apps/desktop/src/components/mqtt/MqttAdminConsole.vue
@@ -35,6 +35,7 @@ interface MqttTabUiState {
formEnabled?: boolean;
editingTopic?: string | null;
publishPanelCollapsed?: boolean;
+ pollingIntervalMs?: number;
}
interface MqttMessagePreview extends MqttMessage {
@@ -44,6 +45,14 @@ interface MqttMessagePreview extends MqttMessage {
type MqttMessageDirectionFilter = "all" | MqttMessageDirection;
+const DEFAULT_MQTT_POLLING_INTERVAL_MS = 1000;
+const MQTT_POLLING_INTERVAL_OPTIONS = [
+ { value: 1000, label: "1s" },
+ { value: 2000, label: "2s" },
+ { value: 3000, label: "3s" },
+ { value: 5000, label: "5s" },
+] as const;
+
const { initialState: restoredUiState, track: trackUiState } = useTabUiState<MqttTabUiState>({}, "MqttAdminConsole");
const brokerInfo = ref<MqttBrokerInfo | null>(null);
@@ -55,6 +64,7 @@ const noLocalSubscribe = ref(false);
const selectedTopic = ref<string>(restoredUiState.selectedTopic ?? props.initialTopic ?? "");
const loading = ref(true);
const error = ref<string | null>(null);
+const pollingIntervalMs = ref<number>(MQTT_POLLING_INTERVAL_OPTIONS.some((opt) => opt.value === restoredUiState.pollingIntervalMs) ? restoredUiState.pollingIntervalMs! : DEFAULT_MQTT_POLLING_INTERVAL_MS);
const pollingTimer = ref<ReturnType<typeof setInterval> | null>(null);
const messageListRef = ref<HTMLElement | null>(null);
const contextMenuMessage = ref<MqttMessagePreview | null>(null);
@@ -105,6 +115,7 @@ trackUiState(() => ({
formEnabled: formEnabled.value,
editingTopic: editingTopic.value,
publishPanelCollapsed: publishPanelCollapsed.value,
+ pollingIntervalMs: pollingIntervalMs.value,
}));
const connected = computed(() => brokerInfo.value?.connected ?? false);
@@ -431,7 +442,11 @@ function startPolling() {
} catch {
/* ignore polling errors */
}
- }, 3000);
+ }, pollingIntervalMs.value);
+}
+
+function handlePollingIntervalChange() {
+ startPolling();
}
function stopPolling() {
@@ -614,6 +629,18 @@ onUnmounted(stopPolling);
<select v-model="displayEncoding" class="h-6 rounded border bg-transparent px-1.5 text-[11px] text-muted-foreground outline-none">
<option v-for="enc in PAYLOAD_ENCODINGS" :key="enc" :value="enc">{{ PAYLOAD_ENCODING_LABELS[enc] }}</option>
</select>
+ <select
+ v-model.number="pollingIntervalMs"
+ data-testid="mqtt-refresh-interval"
+ class="h-6 rounded border bg-transparent px-1.5 text-[11px] text-muted-foreground outline-none"
+ :aria-label="t('connection.mqttRefreshInterval')"
+ :title="t('connection.mqttRefreshInterval')"
+ @change="handlePollingIntervalChange"
+ >
+ <option v-for="option in MQTT_POLLING_INTERVAL_OPTIONS" :key="option.value" :value="option.value">
+ {{ option.label }}
+ </option>
+ </select>
<Button size="sm" variant="ghost" class="h-6 gap-1 px-2 text-[11px] font-normal normal-case" :title="messagesPaused ? t('connection.mqttResumeMessages') : t('connection.mqttPauseMessages')" @click="toggleMessagesPaused">
<Play v-if="messagesPaused" class="h-3.5 w-3.5" />
<Pause v-else class="h-3.5 w-3.5" />
diff --git a/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsolePause.spec.ts b/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsolePause.spec.ts
index 8431836969..2ac5ce3c6e 100644
--- a/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsolePause.spec.ts
+++ b/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsolePause.spec.ts
@@ -84,7 +84,7 @@ describe("MQTT 控制台消息暂停自动滚动 (issue #5615)", () => {
await nextTick();
expect(container.textContent).toContain("payload-0");
- vi.advanceTimersByTime(3000);
+ vi.advanceTimersByTime(1000);
await Promise.resolve();
expect(mqttGetMessagesMock).toHaveBeenCalledTimes(2);
diff --git a/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsolePayloadSearch.spec.ts b/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsolePayloadSearch.spec.ts
index 015bc8182c..4451d7f9ad 100644
--- a/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsolePayloadSearch.spec.ts
+++ b/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsolePayloadSearch.spec.ts
@@ -177,7 +177,7 @@ describe("MQTT 控制台 payload 搜索 (issue #8183)", () => {
await settleConsole();
await setPayloadSearch(container, "error");
- await vi.advanceTimersByTimeAsync(3000);
+ await vi.advanceTimersByTimeAsync(1000);
await nextTick();
expect(container.textContent).toContain("next-error");
diff --git a/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsoleRefreshInterval.spec.ts b/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsoleRefreshInterval.spec.ts
new file mode 100644
index 0000000000..7420c2b0fd
--- /dev/null
+++ b/apps/desktop/src/components/mqtt/__tests__/MqttAdminConsoleRefreshInterval.spec.ts
@@ -0,0 +1,159 @@
+// @vitest-environment happy-dom
+
+import { createApp, nextTick, type App } from "vue";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import i18n, { loadLocaleMessages } from "@/i18n";
+import MqttAdminConsole from "@/components/mqtt/MqttAdminConsole.vue";
+import type { MqttBrokerInfo, MqttMessage } from "@/types/mqtt";
+
+const { mqttGetMessagesMock, tabUiStateMock } = vi.hoisted(() => ({
+ mqttGetMessagesMock: vi.fn(),
+ tabUiStateMock: {
+ initialState: {} as Record<string, unknown>,
+ track: vi.fn(),
+ },
+}));
+
+vi.mock("@/lib/tabs/tabUiState", () => ({
+ useTabUiState: () => tabUiStateMock,
+}));
+
+vi.mock("@/lib/backend/api", () => ({
+ mqttGetBrokerInfo: vi.fn(async (): Promise<MqttBrokerInfo> => ({
+ brokerUrl: "mqtt://localhost:1883",
+ clientId: "dbx-test",
+ connected: true,
+ protocolVersion: "5.0",
+ subscriptionCount: 1,
+ })),
+ mqttListTopics: vi.fn(async () => [["sensors/temperature", "atmostonce"]]),
+ mqttListSavedTopicConfigs: vi.fn(async () => [{ topic: "sensors/temperature", qos: "atmostonce", enabled: true, noLocal: false }]),
+ mqttGetMessages: mqttGetMessagesMock,
+ mqttSubscribe: vi.fn(),
+ mqttUnsubscribe: vi.fn(),
+ mqttSaveTopicConfig: vi.fn(),
+ mqttDeleteTopicConfig: vi.fn(),
+ mqttClearMessages: vi.fn(),
+}));
+
+const mountedApps: App[] = [];
+
+function messageAt(index: number, topic = "sensors/temperature"): MqttMessage {
+ return {
+ topic,
+ payloadBase64: btoa(`payload-${index}`),
+ payloadText: `payload-${index}`,
+ qos: 0,
+ retain: false,
+ receivedAtMs: Date.now(),
+ direction: "received",
+ };
+}
+
+async function mountConsole(initialState: Record<string, unknown> = {}) {
+ tabUiStateMock.initialState = initialState;
+ const container = document.createElement("div");
+ document.body.append(container);
+ const app = createApp(MqttAdminConsole, {
+ connectionId: "mqtt-connection-refresh",
+ initialTopic: "sensors/temperature",
+ });
+ mountedApps.push(app);
+ app.use(i18n);
+ app.mount(container);
+ await nextTick();
+ await Promise.resolve();
+ await Promise.resolve();
+ await nextTick();
+ return container;
+}
+
+beforeEach(async () => {
+ mqttGetMessagesMock.mockReset().mockResolvedValue([messageAt(0)]);
+ tabUiStateMock.track.mockClear();
+ tabUiStateMock.initialState = {};
+ await loadLocaleMessages("zh-CN");
+ i18n.global.locale.value = "zh-CN";
+ vi.useFakeTimers();
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+ for (const app of mountedApps.splice(0)) app.unmount();
+ document.body.innerHTML = "";
+});
+
+describe("MQTT 控制台前台刷新时间 (issue #12829)", () => {
+ it("默认以 1 秒(1000ms)间隔轮询最新消息并提供刷新间隔下拉选项", async () => {
+ const container = await mountConsole();
+ expect(mqttGetMessagesMock).toHaveBeenCalledTimes(1);
+
+ const intervalSelect = container.querySelector('[data-testid="mqtt-refresh-interval"]') as HTMLSelectElement | null;
+ expect(intervalSelect).toBeTruthy();
+ expect(intervalSelect!.value).toBe("1000");
+
+ const optionValues = Array.from(intervalSelect!.options).map((opt) => opt.value);
+ expect(optionValues).toEqual(["1000", "2000", "3000", "5000"]);
+
+ const optionLabels = Array.from(intervalSelect!.options).map((opt) => opt.textContent?.trim());
+ expect(optionLabels).toEqual(["1s", "2s", "3s", "5s"]);
+
+ // Advancing 1000ms triggers the next poll
+ await vi.advanceTimersByTimeAsync(1000);
+ expect(mqttGetMessagesMock).toHaveBeenCalledTimes(2);
+
+ await vi.advanceTimersByTimeAsync(1000);
+ expect(mqttGetMessagesMock).toHaveBeenCalledTimes(3);
+ });
+
+ it("切换刷新间隔至 2 秒后按新间隔进行消息轮询", async () => {
+ const container = await mountConsole();
+ expect(mqttGetMessagesMock).toHaveBeenCalledTimes(1);
+
+ const intervalSelect = container.querySelector('[data-testid="mqtt-refresh-interval"]') as HTMLSelectElement | null;
+ expect(intervalSelect).toBeTruthy();
+
+ intervalSelect!.value = "2000";
+ intervalSelect!.dispatchEvent(new Event("change"));
+ await nextTick();
+
+ mqttGetMessagesMock.mockClear();
+
+ // 1000ms should NOT trigger poll with 2000ms interval
+ await vi.advanceTimersByTimeAsync(1000);
+ expect(mqttGetMessagesMock).not.toHaveBeenCalled();
+
+ // Reaching 2000ms triggers the poll
+ await vi.advanceTimersByTimeAsync(1000);
+ expect(mqttGetMessagesMock).toHaveBeenCalledTimes(1);
+
+ // Another 2000ms triggers again
+ await vi.advanceTimersByTimeAsync(2000);
+ expect(mqttGetMessagesMock).toHaveBeenCalledTimes(2);
+ });
+
+ it("恢复保存的 Tab UI State 中的刷新间隔配置", async () => {
+ const container = await mountConsole({ pollingIntervalMs: 3000 });
+ const intervalSelect = container.querySelector('[data-testid="mqtt-refresh-interval"]') as HTMLSelectElement | null;
+ expect(intervalSelect).toBeTruthy();
+ expect(intervalSelect!.value).toBe("3000");
+
+ mqttGetMessagesMock.mockClear();
+ await vi.advanceTimersByTimeAsync(2000);
+ expect(mqttGetMessagesMock).not.toHaveBeenCalled();
+
+ await vi.advanceTimersByTimeAsync(1000);
+ expect(mqttGetMessagesMock).toHaveBeenCalledTimes(1);
+ });
+
+ it("若 Tab UI State 中的刷新间隔无效则回退至默认 1 秒", async () => {
+ const container = await mountConsole({ pollingIntervalMs: 9999 });
+ const intervalSelect = container.querySelector('[data-testid="mqtt-refresh-interval"]') as HTMLSelectElement | null;
+ expect(intervalSelect).toBeTruthy();
+ expect(intervalSelect!.value).toBe("1000");
+
+ mqttGetMessagesMock.mockClear();
+ await vi.advanceTimersByTimeAsync(1000);
+ expect(mqttGetMessagesMock).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/apps/desktop/src/i18n/locales/az.ts b/apps/desktop/src/i18n/locales/az.ts
index 0d1bb8ac90..329de63fd4 100644
--- a/apps/desktop/src/i18n/locales/az.ts
+++ b/apps/desktop/src/i18n/locales/az.ts
@@ -1021,6 +1021,7 @@ export default withEnglishFallback({
mqttConnected: "Əlaqə qurulub",
mqttDisconnected: "Əlaqə kəsilib",
mqttRefresh: "Yenilə",
+ mqttRefreshInterval: "Yeniləmə intervalı",
mqttClearMessages: "Mesajları təmizlə",
mqttCopyPayload: "Payload-i kopyala",
mqttCopyTopic: "Topic-i kopyala",
diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts
index 9218c57315..f4548bbc89 100644
--- a/apps/desktop/src/i18n/locales/en.ts
+++ b/apps/desktop/src/i18n/locales/en.ts
@@ -1672,6 +1672,7 @@ export default {
mqttConnected: "Connected",
mqttDisconnected: "Disconnected",
mqttRefresh: "Refresh",
+ mqttRefreshInterval: "Refresh Interval",
mqttClearMessages: "Clear messages",
mqttCopyPayload: "Copy Payload",
mqttCopyTopic: "Copy Topic",
diff --git a/apps/desktop/src/i18n/locales/es.ts b/apps/desktop/src/i18n/locales/es.ts
index 376c735b67..5e30aeeffa 100644
--- a/apps/desktop/src/i18n/locales/es.ts
+++ b/apps/desktop/src/i18n/locales/es.ts
@@ -1815,6 +1815,7 @@ export default withEnglishFallback({
mqttConnected: "Conectado",
mqttDisconnected: "Desconectado",
mqttRefresh: "Actualizar",
+ mqttRefreshInterval: "Intervalo de actualización",
mqttClearMessages: "Borrar mensajes",
mqttCopyPayload: "Copiar Payload",
mqttCopyTopic: "Copiar Topic",
diff --git a/apps/desktop/src/i18n/locales/id.ts b/apps/desktop/src/i18n/locales/id.ts
index bacbc48499..8ba9ad81de 100644
--- a/apps/desktop/src/i18n/locales/id.ts
+++ b/apps/desktop/src/i18n/locales/id.ts
@@ -1277,6 +1277,7 @@ export default withEnglishFallback({
mqttConnected: "Terhubung",
mqttDisconnected: "Terputus",
mqttRefresh: "Segarkan",
+ mqttRefreshInterval: "Interval Penyegaran",
mqttClearMessages: "Bersihkan pesan",
mqttCopyPayload: "Salin Payload",
mqttCopyTopic: "Salin Topic",
diff --git a/apps/desktop/src/i18n/locales/it.ts b/apps/desktop/src/i18n/locales/it.ts
index 677336022a..2a437889a6 100644
--- a/apps/desktop/src/i18n/locales/it.ts
+++ b/apps/desktop/src/i18n/locales/it.ts
@@ -1724,6 +1724,7 @@ export default withEnglishFallback({
mqttConnected: "Connesso",
mqttDisconnected: "Disconnesso",
mqttRefresh: "Aggiorna",
+ mqttRefreshInterval: "Intervallo di aggiornamento",
mqttClearMessages: "Cancella messaggi",
mqttCopyPayload: "Copia Payload",
mqttCopyTopic: "Copia Topic",
diff --git a/apps/desktop/src/i18n/locales/ja.ts b/apps/desktop/src/i18n/locales/ja.ts
index 2e8dc0b4ff..4c41bdc1d4 100644
--- a/apps/desktop/src/i18n/locales/ja.ts
+++ b/apps/desktop/src/i18n/locales/ja.ts
@@ -1724,6 +1724,7 @@ export default withEnglishFallback({
mqttConnected: "接続済み",
mqttDisconnected: "切断済み",
mqttRefresh: "更新",
+ mqttRefreshInterval: "更新間隔",
mqttClearMessages: "メッセージを消去",
mqttCopyPayload: "Payload をコピー",
mqttCopyTopic: "Topic をコピー",
diff --git a/apps/desktop/src/i18n/locales/ko.ts b/apps/desktop/src/i18n/locales/ko.ts
index a8ebb23bb6..7efad87941 100644
--- a/apps/desktop/src/i18n/locales/ko.ts
+++ b/apps/desktop/src/i18n/locales/ko.ts
@@ -1742,6 +1742,7 @@ export default withEnglishFallback({
objectCacheRefreshFailed: "개체 캐시 새로 고침 실패: {message}",
mqttMaxPacketSize: "최대 패킷 크기(바이트)",
mqttMessageDirectionFilterLabel: "메시지 방향으로 필터링",
+ mqttRefreshInterval: "새로고침 간격",
mqttDirectionFilterAll: "전체",
mqttDirectionFilterReceived: "수신됨",
mqttDirectionFilterSent: "발신됨",
diff --git a/apps/desktop/src/i18n/locales/pl.ts b/apps/desktop/src/i18n/locales/pl.ts
index 1a916889fc..05353814aa 100644
--- a/apps/desktop/src/i18n/locales/pl.ts
+++ b/apps/desktop/src/i18n/locales/pl.ts
@@ -1341,6 +1341,7 @@ export default withEnglishFallback({
mqttConnected: "Połączono",
mqttDisconnected: "Rozłączono",
mqttRefresh: "Odśwież",
+ mqttRefreshInterval: "Interwał odświeżania",
mqttClearMessages: "Wyczyść wiadomości",
mqttCollapsePayload: "Zwiń zawartość",
mqttExpandPayload: "Rozwiń zawartość",
diff --git a/apps/desktop/src/i18n/locales/pt-BR.ts b/apps/desktop/src/i18n/locales/pt-BR.ts
index b5e3a0d8e7..b5d1c5576d 100644
--- a/apps/desktop/src/i18n/locales/pt-BR.ts
+++ b/apps/desktop/src/i18n/locales/pt-BR.ts
@@ -1725,6 +1725,7 @@ export default withEnglishFallback({
mqttConnected: "Conectado",
mqttDisconnected: "Desconectado",
mqttRefresh: "Atualizar",
+ mqttRefreshInterval: "Intervalo de atualização",
mqttClearMessages: "Limpar mensagens",
mqttCopyPayload: "Copiar Payload",
mqttCopyTopic: "Copiar Topic",
diff --git a/apps/desktop/src/i18n/locales/ru.ts b/apps/desktop/src/i18n/locales/ru.ts
index f860d64bb5..9cc1ef2ff2 100644
--- a/apps/desktop/src/i18n/locales/ru.ts
+++ b/apps/desktop/src/i18n/locales/ru.ts
@@ -1427,6 +1427,7 @@ export default withEnglishFallback({
mqttConnected: "Подключено",
mqttDisconnected: "Отключено",
mqttRefresh: "Обновить",
+ mqttRefreshInterval: "Интервал обновления",
mqttClearMessages: "Очистить сообщения",
mqttCopyPayload: "Скопировать Payload",
mqttCopyTopic: "Скопировать Topic",
diff --git a/apps/desktop/src/i18n/locales/th.ts b/apps/desktop/src/i18n/locales/th.ts
index 79f34e178a..798459d826 100644
--- a/apps/desktop/src/i18n/locales/th.ts
+++ b/apps/desktop/src/i18n/locales/th.ts
@@ -1626,6 +1626,7 @@ export default withEnglishFallback({
mqttConnected: "เชื่อมต่อแล้ว",
mqttDisconnected: "ตัดการเชื่อมต่อแล้ว",
mqttRefresh: "รีเฟรช",
+ mqttRefreshInterval: "ช่วงเวลาการรีเฟรช",
mqttClearMessages: "ล้างข้อความ",
mqttCollapsePayload: "ยุบ Payload",
mqttExpandPayload: "ขยาย Payload",
diff --git a/apps/desktop/src/i18n/locales/tr.ts b/apps/desktop/src/i18n/locales/tr.ts
index 856d1e92ac..aea4b8ed0a 100644
--- a/apps/desktop/src/i18n/locales/tr.ts
+++ b/apps/desktop/src/i18n/locales/tr.ts
@@ -1021,6 +1021,7 @@ export default withEnglishFallback({
mqttConnected: "Bağlandı",
mqttDisconnected: "Bağlantı kesildi",
mqttRefresh: "Yenile",
+ mqttRefreshInterval: "Yenileme Aralığı",
mqttClearMessages: "Mesajları temizle",
mqttCopyPayload: "Payload'i kopyala",
mqttCopyTopic: "Topic'i kopyala",
diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts
index cb1ee3e5ab..ce3274f834 100644
--- a/apps/desktop/src/i18n/locales/zh-CN.ts
+++ b/apps/desktop/src/i18n/locales/zh-CN.ts
@@ -1607,6 +1607,7 @@ export default withEnglishFallback({
mqttConnected: "已连接",
mqttDisconnected: "已断开",
mqttRefresh: "刷新",
+ mqttRefreshInterval: "刷新间隔",
mqttClearMessages: "清空消息",
mqttCopyPayload: "复制 Payload",
mqttCopyTopic: "复制 Topic",
diff --git a/apps/desktop/src/i18n/locales/zh-TW.ts b/apps/desktop/src/i18n/locales/zh-TW.ts
index a467438049..d06ea198fe 100644
--- a/apps/desktop/src/i18n/locales/zh-TW.ts
+++ b/apps/desktop/src/i18n/locales/zh-TW.ts
@@ -1735,6 +1735,7 @@ export default withEnglishFallback({
mqttConnected: "已連線",
mqttDisconnected: "已斷線",
mqttRefresh: "重新整理",
+ mqttRefreshInterval: "重新整理間隔",
mqttClearMessages: "清除訊息",
mqttCopyPayload: "複製 Payload",
mqttCopyTopic: "複製 Topic",