fix(deep-link): preserve cold-start connection links

Closes #10090
This commit is contained in:
t8y2 committed 2026-09-25 19:20:11 +08:00
1 parent 02886a4682
commit 135e5a91a7
5 files changed
+163 -92

No files matched your search

+7 -6
View File
@@ -4071,14 +4071,15 @@ onMounted(async () => {
appVersion.value = v;
})
.catch(() => {});
setupTauriListeners();
void setupTauriListeners().then(() => {
void openPendingSqlFiles();
void openPendingDbFiles();
void openPendingConnectionLinks();
void openPendingAiConfigLinks();
void openPendingPluginInstallLinks();
});
setupCloseActionPromptListener();
void setupDetachedWindowEvents();
void openPendingSqlFiles();
void openPendingDbFiles();
void openPendingConnectionLinks();
void openPendingAiConfigLinks();
void openPendingPluginInstallLinks();
console.log(`[STARTUP] onMounted sync done: ${(performance.now() - mountStart).toFixed(0)}ms`);
});
@@ -2,12 +2,15 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
const listeners = new Map<string, (event: { payload: unknown }) => void>();
const unlisten = vi.fn();
let listenerRegistrationBarrier: Promise<void> | null = null;
const listen = vi.fn(async (event: string, handler: (event: { payload: unknown }) => void) => {
await listenerRegistrationBarrier;
listeners.set(event, handler);
return unlisten;
});
vi.mock("@tauri-apps/api/event", () => ({
listen: vi.fn(async (event: string, handler: (event: { payload: unknown }) => void) => {
listeners.set(event, handler);
return unlisten;
}),
listen,
}));
vi.mock("@/stores/connectionStore", () => ({
@@ -23,7 +26,40 @@ import { useTauriEvents } from "@/composables/useTauriEvents";
describe("useTauriEvents", () => {
beforeEach(() => {
listeners.clear();
listen.mockClear();
unlisten.mockClear();
listenerRegistrationBarrier = null;
});
it("resolves setup only after every native listener is registered", async () => {
let releaseRegistration!: () => void;
listenerRegistrationBarrier = new Promise<void>((resolve) => {
releaseRegistration = resolve;
});
const events = useTauriEvents({
openTableTarget: vi.fn(),
openSqlFilePath: vi.fn(),
openDbFilePath: vi.fn(),
openConnectionDeepLink: vi.fn(),
openAiConfigDeepLink: vi.fn(),
openPluginInstallDeepLink: vi.fn(),
closeActiveSurface: vi.fn(),
refreshPluginWorkbenches: vi.fn(),
});
let setupResolved = false;
const setup = events.setupTauriListeners().then(() => {
setupResolved = true;
});
await vi.waitFor(() => expect(listen).toHaveBeenCalled());
expect(setupResolved).toBe(false);
expect(listeners.size).toBe(0);
releaseRegistration();
await setup;
expect(setupResolved).toBe(true);
expect(listeners.has("dbx-open-connection-links")).toBe(true);
});
it("routes the native macOS close-tab menu event to the active surface", async () => {
@@ -37,8 +73,7 @@ describe("useTauriEvents", () => {
refreshPluginWorkbenches: vi.fn(),
});
events.setupTauriListeners();
await vi.waitFor(() => expect(listeners.has("dbx-close-active-tab")).toBe(true));
await events.setupTauriListeners();
listeners.get("dbx-close-active-tab")!({ payload: undefined });
expect(closeActiveSurface).toHaveBeenCalledOnce();
@@ -57,8 +92,7 @@ describe("useTauriEvents", () => {
refreshPluginWorkbenches,
});
events.setupTauriListeners();
await vi.waitFor(() => expect(listeners.has("plugin-runtime-replaced")).toBe(true));
await events.setupTauriListeners();
listeners.get("plugin-runtime-replaced")!({ payload: { pluginId: "io.example.plugin", version: "0.2.0" } });
expect(refreshPluginWorkbenches).toHaveBeenCalledExactlyOnceWith("io.example.plugin");
+37 -26
View File
@@ -25,10 +25,19 @@ export function useTauriEvents(deps: {
);
}
function setupTauriListeners() {
import("@tauri-apps/api/event")
function setupTauriListeners(): Promise<void> {
return import("@tauri-apps/api/event")
.then(({ listen }) => {
listen<{ connection_id: string; database: string; schema?: string; table: string }>("mcp-open-table", async (event) => {
const registrations: Promise<void>[] = [];
const track = (registration: Promise<() => void>) => {
registrations.push(
registration.then((unlisten) => {
unlistenHandles.push(unlisten);
}),
);
};
track(listen<{ connection_id: string; database: string; schema?: string; table: string }>("mcp-open-table", async (event) => {
try {
const { connection_id, database, schema, table } = event.payload;
if (!connectionStore.connections.length) await connectionStore.initFromDisk();
@@ -47,9 +56,9 @@ export function useTauriEvents(deps: {
} catch (e) {
console.error("[DBX] mcp-open-table error:", e);
}
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
listen<{ connection_id: string }>("mcp-open-connection-workbench", async (event) => {
track(listen<{ connection_id: string }>("mcp-open-connection-workbench", async (event) => {
try {
const { connection_id } = event.payload;
if (!connectionStore.connections.length) await connectionStore.initFromDisk();
@@ -65,12 +74,12 @@ export function useTauriEvents(deps: {
} catch (e) {
console.error("[DBX] mcp-open-connection-workbench error:", e);
}
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
// Plugins report live transport state through plugin events; when an
// SSH session dies, flip the sidebar entry offline so the tree stops
// showing a connection that no longer exists.
listen<{ method: string; params: { state?: string; connectionId?: string } }>("dbx-plugin-event", async (event) => {
track(listen<{ method: string; params: { state?: string; connectionId?: string } }>("dbx-plugin-event", async (event) => {
try {
const payload = event.payload ?? ({} as typeof event.payload);
if (payload.method !== "ssh/session/state" || payload.params?.state !== "disconnected") return;
@@ -82,17 +91,17 @@ export function useTauriEvents(deps: {
} catch (e) {
console.error("[DBX] dbx-plugin-event (ssh/session/state) error:", e);
}
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
listen("mcp-reload-connections", async () => {
track(listen("mcp-reload-connections", async () => {
try {
await connectionStore.initFromDisk();
} catch (e) {
console.error("[DBX] mcp-reload-connections error:", e);
}
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
listen<{
track(listen<{
connection_id: string;
database: string;
sql: string;
@@ -109,9 +118,9 @@ export function useTauriEvents(deps: {
} catch (e) {
console.error("[DBX] mcp-execute-query error:", e);
}
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
listen<string[]>("dbx-open-sql-files", async (event) => {
track(listen<string[]>("dbx-open-sql-files", async (event) => {
try {
for (const path of event.payload) {
await deps.openSqlFilePath(path);
@@ -120,9 +129,9 @@ export function useTauriEvents(deps: {
} catch (e) {
console.error("[DBX] dbx-open-sql-files error:", e);
}
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
listen<string[]>("dbx-open-db-files", async (event) => {
track(listen<string[]>("dbx-open-db-files", async (event) => {
try {
for (const path of event.payload) {
await deps.openDbFilePath(path);
@@ -131,9 +140,9 @@ export function useTauriEvents(deps: {
} catch (e) {
console.error("[DBX] dbx-open-db-files error:", e);
}
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
listen<string[]>("dbx-open-connection-links", async (event) => {
track(listen<string[]>("dbx-open-connection-links", async (event) => {
try {
for (const url of event.payload) {
await deps.openConnectionDeepLink(url);
@@ -142,9 +151,9 @@ export function useTauriEvents(deps: {
} catch (e) {
console.error("[DBX] dbx-open-connection-links error:", e);
}
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
listen<string[]>("dbx-open-ai-config-links", async (event) => {
track(listen<string[]>("dbx-open-ai-config-links", async (event) => {
try {
for (const url of event.payload) {
await deps.openAiConfigDeepLink(url);
@@ -153,9 +162,9 @@ export function useTauriEvents(deps: {
} catch (e) {
console.error("[DBX] dbx-open-ai-config-links error:", e);
}
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
listen<string[]>("dbx-open-plugin-install-links", async (event) => {
track(listen<string[]>("dbx-open-plugin-install-links", async (event) => {
try {
for (const url of event.payload) {
await deps.openPluginInstallDeepLink(url);
@@ -164,18 +173,20 @@ export function useTauriEvents(deps: {
} catch (e) {
console.error("[DBX] dbx-open-plugin-install-links error:", e);
}
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
listen("dbx-close-active-tab", () => {
track(listen("dbx-close-active-tab", () => {
deps.closeActiveSurface();
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
// A plugin was installed/rolled back from its already-replaced runtime:
// open workbench tabs still render the previous UI bundle until they
// reload, so hand them the new identity to pick up.
listen<{ pluginId: string; version: string }>("plugin-runtime-replaced", (event) => {
track(listen<{ pluginId: string; version: string }>("plugin-runtime-replaced", (event) => {
if (event.payload?.pluginId) deps.refreshPluginWorkbenches(event.payload.pluginId);
}).then((unlisten) => unlistenHandles.push(unlisten));
}));
return Promise.allSettled(registrations).then(() => {});
})
.catch(() => {});
}