fix(plugins): refresh frontend contributions on plugin changes
This commit is contained in:
1 parent
a2cd9fb5da
commit
55a4b9f450
4 files changed
+335
-12
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, ref } from "vue";
|
import { computed, onScopeDispose, ref } from "vue";
|
||||||
import { GitBranch, Gauge, Loader2, PlugZap, Upload } from "@lucide/vue";
|
import { GitBranch, Gauge, Loader2, PlugZap, Upload } from "@lucide/vue";
|
||||||
import { useI18n } from "vue-i18n";
|
import { useI18n } from "vue-i18n";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
@@ -36,12 +36,21 @@ const installedPlugins = ref<InstalledPlugin[]>([]);
|
|||||||
const resultViews = computed<PluginContributionEntry<PluginResultViewContribution>[]>(() => createFrontendPluginRegistry(installedPlugins.value, appLocale.value).listResultViews());
|
const resultViews = computed<PluginContributionEntry<PluginResultViewContribution>[]>(() => createFrontendPluginRegistry(installedPlugins.value, appLocale.value).listResultViews());
|
||||||
const visibleResultViews = computed(() => (props.hasResult ? resultViews.value.slice(0, 4) : []));
|
const visibleResultViews = computed(() => (props.hasResult ? resultViews.value.slice(0, 4) : []));
|
||||||
|
|
||||||
void api.listPlugins().then(
|
async function refreshInstalledPlugins() {
|
||||||
(plugins) => {
|
try {
|
||||||
|
const plugins = await api.listPlugins();
|
||||||
installedPlugins.value = plugins.filter((plugin) => plugin.compatibility.compatible);
|
installedPlugins.value = plugins.filter((plugin) => plugin.compatibility.compatible);
|
||||||
},
|
} catch {
|
||||||
() => {},
|
// Keep the previous list: an empty registry would drop live plugin result-view entries.
|
||||||
);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Plugin install/update/uninstall from the Plugin Center broadcasts this event;
|
||||||
|
// without it the toolbar keeps the mount-time snapshot until DBX restarts.
|
||||||
|
const onPluginsChanged = () => void refreshInstalledPlugins();
|
||||||
|
window.addEventListener("dbx:plugins-changed", onPluginsChanged);
|
||||||
|
onScopeDispose(() => window.removeEventListener("dbx:plugins-changed", onPluginsChanged));
|
||||||
|
void refreshInstalledPlugins();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
+126
@@ -0,0 +1,126 @@
|
|||||||
|
// @vitest-environment happy-dom
|
||||||
|
import { createApp, nextTick, type App } from "vue";
|
||||||
|
import { createI18n } from "vue-i18n";
|
||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import type { InstalledPlugin } from "@/types/database";
|
||||||
|
|
||||||
|
const { listPlugins } = vi.hoisted(() => ({ listPlugins: vi.fn() }));
|
||||||
|
|
||||||
|
vi.mock("@/lib/backend/api", () => ({ listPlugins }));
|
||||||
|
|
||||||
|
import QueryResultToolbarActions from "../QueryResultToolbarActions.vue";
|
||||||
|
|
||||||
|
function installedPlugin(id: string, contributions: InstalledPlugin["manifest"]["contributions"]): InstalledPlugin {
|
||||||
|
return {
|
||||||
|
compatibility: { compatible: true },
|
||||||
|
manifest: { id, name: id, version: "1.0.0", drivers: [], contributions },
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function resultViewPlugin(id: string, contributionId: string, label: string): InstalledPlugin {
|
||||||
|
return installedPlugin(id, [{ type: "result-view", id: contributionId, label }]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const mountedApps: Array<{ app: App<Element>; host: HTMLDivElement }> = [];
|
||||||
|
|
||||||
|
async function flush(): Promise<void> {
|
||||||
|
await nextTick();
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||||
|
await nextTick();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mountToolbar() {
|
||||||
|
const host = document.createElement("div");
|
||||||
|
document.body.appendChild(host);
|
||||||
|
const app = createApp(QueryResultToolbarActions, {
|
||||||
|
activeView: "result",
|
||||||
|
canShowExplain: false,
|
||||||
|
canShowProfile: false,
|
||||||
|
canExportArchive: false,
|
||||||
|
archiveExporting: false,
|
||||||
|
hasResult: true,
|
||||||
|
});
|
||||||
|
app.use(createI18n({ legacy: false, locale: "en", messages: { en: {} }, missingWarn: false, fallbackWarn: false }));
|
||||||
|
app.mount(host);
|
||||||
|
mountedApps.push({ app, host });
|
||||||
|
await flush();
|
||||||
|
return { host, app };
|
||||||
|
}
|
||||||
|
|
||||||
|
function visibleLabels(host: HTMLDivElement): string[] {
|
||||||
|
return [...host.querySelectorAll("button span")].map((node) => node.textContent?.trim() ?? "").filter(Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("QueryResultToolbarActions plugin contribution refresh", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
listPlugins.mockReset();
|
||||||
|
listPlugins.mockResolvedValue([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
for (const { app, host } of mountedApps.splice(0)) {
|
||||||
|
app.unmount();
|
||||||
|
host.remove();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("renders a result-view contribution installed after mount once dbx:plugins-changed fires", async () => {
|
||||||
|
const { host } = await mountToolbar();
|
||||||
|
expect(visibleLabels(host)).not.toContain("Open graph");
|
||||||
|
|
||||||
|
listPlugins.mockResolvedValue([resultViewPlugin("com.example.chart", "result.graph", "Open graph")]);
|
||||||
|
window.dispatchEvent(new CustomEvent("dbx:plugins-changed"));
|
||||||
|
await flush();
|
||||||
|
|
||||||
|
expect(visibleLabels(host)).toContain("Open graph");
|
||||||
|
expect(listPlugins).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("drops the contribution when the plugin set becomes empty again", async () => {
|
||||||
|
listPlugins.mockResolvedValue([resultViewPlugin("com.example.chart", "result.graph", "Open graph")]);
|
||||||
|
const { host } = await mountToolbar();
|
||||||
|
expect(visibleLabels(host)).toContain("Open graph");
|
||||||
|
|
||||||
|
listPlugins.mockResolvedValue([]);
|
||||||
|
window.dispatchEvent(new CustomEvent("dbx:plugins-changed"));
|
||||||
|
await flush();
|
||||||
|
|
||||||
|
expect(visibleLabels(host)).not.toContain("Open graph");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ignores plugins that are not compatible with the current host", async () => {
|
||||||
|
const incompatible = { ...resultViewPlugin("com.example.legacy", "result.legacy", "Legacy view"), compatibility: { compatible: false } };
|
||||||
|
listPlugins.mockResolvedValue([incompatible]);
|
||||||
|
const { host } = await mountToolbar();
|
||||||
|
|
||||||
|
expect(visibleLabels(host)).not.toContain("Legacy view");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("stops refreshing after the toolbar is unmounted", async () => {
|
||||||
|
const { host, app } = await mountToolbar();
|
||||||
|
expect(listPlugins).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
app.unmount();
|
||||||
|
host.remove();
|
||||||
|
window.dispatchEvent(new CustomEvent("dbx:plugins-changed"));
|
||||||
|
await flush();
|
||||||
|
|
||||||
|
expect(listPlugins).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("removes every listener on unmount instead of stacking them across mounts", async () => {
|
||||||
|
const first = await mountToolbar();
|
||||||
|
first.app.unmount();
|
||||||
|
first.host.remove();
|
||||||
|
|
||||||
|
const second = await mountToolbar();
|
||||||
|
expect(listPlugins).toHaveBeenCalledTimes(2);
|
||||||
|
|
||||||
|
window.dispatchEvent(new CustomEvent("dbx:plugins-changed"));
|
||||||
|
await flush();
|
||||||
|
|
||||||
|
expect(listPlugins).toHaveBeenCalledTimes(3);
|
||||||
|
second.app.unmount();
|
||||||
|
second.host.remove();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, nextTick, watch, onBeforeUnmount, inject, reactive, ref, shallowRef } from "vue";
|
import { computed, nextTick, watch, onBeforeUnmount, onScopeDispose, inject, reactive, ref, shallowRef } from "vue";
|
||||||
import { createRoutedSidebarDialogController, routedCanSetCreateDatabaseCharset } from "./sidebarDialogControllerRouting";
|
import { createRoutedSidebarDialogController, routedCanSetCreateDatabaseCharset } from "./sidebarDialogControllerRouting";
|
||||||
import { useSqlHighlighter } from "@/composables/useSqlHighlighter";
|
import { useSqlHighlighter } from "@/composables/useSqlHighlighter";
|
||||||
import { useSidebarDataOpenRuntime } from "@/composables/useSidebarDataOpenRuntime";
|
import { useSidebarDataOpenRuntime } from "@/composables/useSidebarDataOpenRuntime";
|
||||||
@@ -377,12 +377,22 @@ const { toast } = useToast();
|
|||||||
const installedPlugins = ref<InstalledPlugin[]>([]);
|
const installedPlugins = ref<InstalledPlugin[]>([]);
|
||||||
const sidebarPluginRegistry = computed(() => createFrontendPluginRegistry(installedPlugins.value, appLocale.value));
|
const sidebarPluginRegistry = computed(() => createFrontendPluginRegistry(installedPlugins.value, appLocale.value));
|
||||||
|
|
||||||
void api.listPlugins().then(
|
async function refreshInstalledPlugins() {
|
||||||
(plugins) => {
|
try {
|
||||||
|
const plugins = await api.listPlugins();
|
||||||
installedPlugins.value = plugins.filter((plugin) => plugin.compatibility.compatible);
|
installedPlugins.value = plugins.filter((plugin) => plugin.compatibility.compatible);
|
||||||
},
|
} catch {
|
||||||
() => {},
|
// Keep the previous list: an empty registry would drop live plugin context-menu entries.
|
||||||
);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Plugin install/update/uninstall from the Plugin Center broadcasts this event;
|
||||||
|
// without it the sidebar registry keeps the mount-time snapshot and the Table /
|
||||||
|
// Connection context menus stay empty until DBX restarts.
|
||||||
|
const onPluginsChanged = () => void refreshInstalledPlugins();
|
||||||
|
window.addEventListener("dbx:plugins-changed", onPluginsChanged);
|
||||||
|
onScopeDispose(() => window.removeEventListener("dbx:plugins-changed", onPluginsChanged));
|
||||||
|
void refreshInstalledPlugins();
|
||||||
|
|
||||||
const { highlight } = useSqlHighlighter();
|
const { highlight } = useSqlHighlighter();
|
||||||
|
|
||||||
|
|||||||
+178
@@ -0,0 +1,178 @@
|
|||||||
|
// @vitest-environment happy-dom
|
||||||
|
import { createApp, defineComponent, h, nextTick, ref, type App } from "vue";
|
||||||
|
import { createPinia, setActivePinia } from "pinia";
|
||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import i18n from "@/i18n";
|
||||||
|
import type { ContextMenuItem } from "@/components/ui/CustomContextMenu.vue";
|
||||||
|
import type { InstalledPlugin, TreeNode } from "@/types/database";
|
||||||
|
|
||||||
|
const { listPlugins } = vi.hoisted(() => ({ listPlugins: vi.fn() }));
|
||||||
|
|
||||||
|
vi.mock("@/lib/backend/api", async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import("@/lib/backend/api")>();
|
||||||
|
return { ...actual, listPlugins };
|
||||||
|
});
|
||||||
|
|
||||||
|
import SidebarTreeRuntimeHost from "@/components/sidebar/SidebarTreeRuntimeHost.vue";
|
||||||
|
import { useConnectionStore } from "@/stores/connectionStore";
|
||||||
|
|
||||||
|
const connection = {
|
||||||
|
id: "conn-1",
|
||||||
|
name: "Test MySQL",
|
||||||
|
db_type: "mysql",
|
||||||
|
driver_profile: "mysql",
|
||||||
|
host: "localhost",
|
||||||
|
port: 3306,
|
||||||
|
username: "",
|
||||||
|
password: "",
|
||||||
|
};
|
||||||
|
|
||||||
|
function contextMenuPlugin(id: string, menu: "connection" | "table", label: string): InstalledPlugin {
|
||||||
|
return {
|
||||||
|
compatibility: { compatible: true },
|
||||||
|
manifest: {
|
||||||
|
id,
|
||||||
|
name: id,
|
||||||
|
version: "1.0.0",
|
||||||
|
drivers: [],
|
||||||
|
contributions: [{ type: "context-menu", id: "example.generate", label, menu, action: { type: "open-workbench", workbench: `${id}.workbench` } }],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function connectionNode(): TreeNode {
|
||||||
|
return { id: "conn-1", label: connection.name, type: "connection", connectionId: connection.id };
|
||||||
|
}
|
||||||
|
|
||||||
|
function tableNode(): TreeNode {
|
||||||
|
return { id: "conn-1:app:users", label: "users", type: "table", connectionId: connection.id, database: "app", tableName: "users" };
|
||||||
|
}
|
||||||
|
|
||||||
|
const mountedApps: App<Element>[] = [];
|
||||||
|
|
||||||
|
async function flush(): Promise<void> {
|
||||||
|
await nextTick();
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||||
|
await nextTick();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function mountHost() {
|
||||||
|
const pinia = createPinia();
|
||||||
|
setActivePinia(pinia);
|
||||||
|
const connectionStore = useConnectionStore();
|
||||||
|
connectionStore.connections = [connection];
|
||||||
|
|
||||||
|
const host = ref<InstanceType<typeof SidebarTreeRuntimeHost> | null>(null);
|
||||||
|
const root = defineComponent({ setup: () => () => h(SidebarTreeRuntimeHost, { ref: host, node: connectionNode(), depth: 0 }) });
|
||||||
|
const container = document.createElement("div");
|
||||||
|
document.body.appendChild(container);
|
||||||
|
const app = createApp(root);
|
||||||
|
app.use(pinia);
|
||||||
|
app.use(i18n);
|
||||||
|
app.mount(container);
|
||||||
|
mountedApps.push(app);
|
||||||
|
await flush();
|
||||||
|
expect(host.value).not.toBeNull();
|
||||||
|
return { host: host as unknown as { value: { buildContextMenu(node: TreeNode): ContextMenuItem[] } }, container };
|
||||||
|
}
|
||||||
|
|
||||||
|
function menuLabels(items: ContextMenuItem[]): string[] {
|
||||||
|
return items.flatMap((item) => [...(item.label ? [item.label] : []), ...(item.children ? menuLabels(item.children) : [])]);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("SidebarTreeRuntimeHost plugin context-menu refresh", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
listPlugins.mockReset();
|
||||||
|
listPlugins.mockResolvedValue([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
for (const app of mountedApps.splice(0)) app.unmount();
|
||||||
|
document.body.innerHTML = "";
|
||||||
|
});
|
||||||
|
|
||||||
|
it("adds a table context-menu contribution installed after mount without remounting the host", async () => {
|
||||||
|
const { host } = await mountHost();
|
||||||
|
expect(menuLabels(host.value.buildContextMenu(tableNode()))).not.toContain("Generate test data");
|
||||||
|
|
||||||
|
listPlugins.mockResolvedValue([contextMenuPlugin("com.example.schema-seed", "table", "Generate test data")]);
|
||||||
|
window.dispatchEvent(new CustomEvent("dbx:plugins-changed"));
|
||||||
|
await flush();
|
||||||
|
|
||||||
|
expect(menuLabels(host.value.buildContextMenu(tableNode()))).toContain("Generate test data");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("adds a connection context-menu contribution installed after mount without remounting the host", async () => {
|
||||||
|
const { host } = await mountHost();
|
||||||
|
expect(menuLabels(host.value.buildContextMenu(connectionNode()))).not.toContain("Inspect connection");
|
||||||
|
|
||||||
|
listPlugins.mockResolvedValue([contextMenuPlugin("com.example.inspector", "connection", "Inspect connection")]);
|
||||||
|
window.dispatchEvent(new CustomEvent("dbx:plugins-changed"));
|
||||||
|
await flush();
|
||||||
|
|
||||||
|
expect(menuLabels(host.value.buildContextMenu(connectionNode()))).toContain("Inspect connection");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps each contribution on its own menu surface", async () => {
|
||||||
|
listPlugins.mockResolvedValue([contextMenuPlugin("com.example.schema-seed", "table", "Generate test data")]);
|
||||||
|
const { host } = await mountHost();
|
||||||
|
|
||||||
|
expect(menuLabels(host.value.buildContextMenu(tableNode()))).toContain("Generate test data");
|
||||||
|
expect(menuLabels(host.value.buildContextMenu(connectionNode()))).not.toContain("Generate test data");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("drops the contribution after the plugin set becomes empty again", async () => {
|
||||||
|
listPlugins.mockResolvedValue([contextMenuPlugin("com.example.schema-seed", "table", "Generate test data")]);
|
||||||
|
const { host } = await mountHost();
|
||||||
|
expect(menuLabels(host.value.buildContextMenu(tableNode()))).toContain("Generate test data");
|
||||||
|
|
||||||
|
listPlugins.mockResolvedValue([]);
|
||||||
|
window.dispatchEvent(new CustomEvent("dbx:plugins-changed"));
|
||||||
|
await flush();
|
||||||
|
|
||||||
|
expect(menuLabels(host.value.buildContextMenu(tableNode()))).not.toContain("Generate test data");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ignores plugins that are not compatible with the current host", async () => {
|
||||||
|
const incompatible: InstalledPlugin = { ...contextMenuPlugin("com.example.legacy", "table", "Legacy action"), compatibility: { compatible: false } };
|
||||||
|
listPlugins.mockResolvedValue([incompatible]);
|
||||||
|
const { host } = await mountHost();
|
||||||
|
|
||||||
|
expect(menuLabels(host.value.buildContextMenu(tableNode()))).not.toContain("Legacy action");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps the previous contributions when a refresh fails", async () => {
|
||||||
|
listPlugins.mockResolvedValue([contextMenuPlugin("com.example.schema-seed", "table", "Generate test data")]);
|
||||||
|
const { host } = await mountHost();
|
||||||
|
|
||||||
|
listPlugins.mockRejectedValue(new Error("backend unavailable"));
|
||||||
|
window.dispatchEvent(new CustomEvent("dbx:plugins-changed"));
|
||||||
|
await flush();
|
||||||
|
|
||||||
|
expect(menuLabels(host.value.buildContextMenu(tableNode()))).toContain("Generate test data");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("stops refreshing after the host is unmounted", async () => {
|
||||||
|
await mountHost();
|
||||||
|
expect(listPlugins).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
mountedApps.splice(0).forEach((app) => app.unmount());
|
||||||
|
window.dispatchEvent(new CustomEvent("dbx:plugins-changed"));
|
||||||
|
await flush();
|
||||||
|
|
||||||
|
expect(listPlugins).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not stack listeners when the host remounts", async () => {
|
||||||
|
await mountHost();
|
||||||
|
mountedApps.splice(0).forEach((app) => app.unmount());
|
||||||
|
|
||||||
|
await mountHost();
|
||||||
|
const callsAfterMount = listPlugins.mock.calls.length;
|
||||||
|
|
||||||
|
window.dispatchEvent(new CustomEvent("dbx:plugins-changed"));
|
||||||
|
await flush();
|
||||||
|
|
||||||
|
expect(listPlugins).toHaveBeenCalledTimes(callsAfterMount + 1);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user