From fe0c014e607d6c86ca6580e3347a771c395575af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=B7=9D=E7=99=BD=E6=B5=AA?= <1679659@qq.com> Date: Thu, 3 Dec 2026 02:07:43 -0700 Subject: [PATCH] fix: preserve plugin dialogs and isolate event delivery --- frontend/scripts/test-plugins.mjs | 6 ++++-- frontend/src/lib/pluginViewEvents.ts | 8 +++++++- frontend/src/lib/pluginViewRouter.ts | 7 ++++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/frontend/scripts/test-plugins.mjs b/frontend/scripts/test-plugins.mjs index 36e4cbb..ba534f7 100644 --- a/frontend/scripts/test-plugins.mjs +++ b/frontend/scripts/test-plugins.mjs @@ -330,10 +330,12 @@ test('closing a page retries cleanup of a late task after rate limiting', async assert.equal(attempts, 2) }) -test('pruning removes a view and dialogs when their conditions stop matching', () => { +test('pruning removes an unavailable page but keeps open dialogs until their VM disappears', () => { const page = { pluginId: 'demo', viewId: 'vm', location: 'vm.tab', vm: 'vm-1' } const dialog = { ...page, viewId: 'dialog', location: 'dialog' } let state = openPluginView(emptyPluginViewState, page) state = openPluginView(state, dialog) - assert.deepEqual(prunePluginViews(state, new Set(['demo/vm', 'demo/dialog']), () => true, () => false), emptyPluginViewState) + const views = new Set(['demo/vm', 'demo/dialog']) + assert.deepEqual(prunePluginViews(state, views, () => true, () => false), { ...emptyPluginViewState, dialogs: [dialog] }) + assert.deepEqual(prunePluginViews(state, views, () => false), emptyPluginViewState) }) diff --git a/frontend/src/lib/pluginViewEvents.ts b/frontend/src/lib/pluginViewEvents.ts index 81791de..ddc63b2 100644 --- a/frontend/src/lib/pluginViewEvents.ts +++ b/frontend/src/lib/pluginViewEvents.ts @@ -4,7 +4,13 @@ export type PluginHostEventListener = (event: PluginHostEvent, data: unknown) => const listeners = new Set() export function publishPluginHostEvent(event: PluginHostEvent, data: unknown): void { - for (const listener of [...listeners]) listener(event, data) + for (const listener of [...listeners]) { + try { + listener(event, data) + } catch (error) { + console.warn('[plugin] could not deliver host event:', event, error) + } + } } export function subscribePluginHostEvents(listener: PluginHostEventListener): () => void { diff --git a/frontend/src/lib/pluginViewRouter.ts b/frontend/src/lib/pluginViewRouter.ts index f1ee9f1..b58e806 100644 --- a/frontend/src/lib/pluginViewRouter.ts +++ b/frontend/src/lib/pluginViewRouter.ts @@ -67,11 +67,12 @@ export function prunePluginViews( validVM: (vm: string | undefined) => boolean, available: (route: PluginViewRoute) => boolean = () => true, ): PluginViewState { - const valid = (route: PluginViewRoute) => validViews.has(pluginViewKey(route)) && (route.location !== 'vm.tab' || validVM(route.vm)) && available(route) - const route = state.route && valid(state.route) ? state.route : null + const valid = (route: PluginViewRoute) => validViews.has(pluginViewKey(route)) && (!route.vm || validVM(route.vm)) + const usable = (route: PluginViewRoute) => valid(route) && available(route) + const route = state.route && usable(state.route) ? state.route : null return { route, dialogs: state.dialogs.filter(valid), - returnStack: route ? state.returnStack.filter(valid) : [], + returnStack: route ? state.returnStack.filter(usable) : [], } }