From 8733a53b8b204d3413448b73121aa057fb6ea7db Mon Sep 17 00:00:00 2001 From: niko_hxx <40194421+NikoHXX@users.noreply.github.com> Date: Sun, 6 Dec 2026 23:01:47 +0800 Subject: [PATCH] fix(plugins): flush project-tree-expanded immediately (#8347) Plugins received project-tree-expanded only with the 500 ms workbench event batch, so notes they add to a folder's files appeared about half a second after the folder opened. The event, and anything already queued before it, is now posted at once. Co-authored-by: NikoHXX <40194421+NikoHXX@users.noreply.github.com> --- .../__tests__/runtimeUi.events.test.ts | 25 ++++++++++++++++++- src/features/plugins/runtimeUi.tsx | 11 ++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/features/plugins/__tests__/runtimeUi.events.test.ts b/src/features/plugins/__tests__/runtimeUi.events.test.ts index 488042dde5..3d7ce247c4 100644 --- a/src/features/plugins/__tests__/runtimeUi.events.test.ts +++ b/src/features/plugins/__tests__/runtimeUi.events.test.ts @@ -7,7 +7,8 @@ import { commands } from "@/platform/ipc"; import { useProject } from "@/platform/project"; import { createMemoryPluginsBackend, setPluginsBackend, tauriPluginsBackend } from "../backend"; import { providePluginEditorContext } from "../editorContext"; -import { startRuntimeUi } from "../runtimeUi"; +import { refreshEventKinds, startRuntimeUi } from "../runtimeUi"; +import { notifyProjectViewExpanded } from "@/workbench/contributions"; const project: ProjectInfo = { root: "/work", name: "work", trusted: true, isGit: false, settings: {} }; let received: WorkbenchEvent[]; @@ -68,6 +69,28 @@ describe("runtime UI caret event lifetime", () => { }); }); +describe("project tree expansion events", () => { + it("are posted with the folder at once, together with the events already queued", async () => { + setPluginsBackend({ + ...createMemoryPluginsBackend(), + eventKinds: async () => ["file-saved", "project-tree-expanded"], + postEvents: async (events) => { + received.push(...events); + }, + }); + refreshEventKinds(); + await vi.advanceTimersByTimeAsync(0); + _emitEditorEvent({ type: "docSaved", doc: 1, path: "/work/a.ts" }); + notifyProjectViewExpanded(["/work/src"]); + expect(received).toEqual([ + { kind: "fileSaved", value: "/work/a.ts" }, + { kind: "projectTreeExpanded", value: ["/work/src"] }, + ]); + await vi.advanceTimersByTimeAsync(1000); + expect(received).toHaveLength(2); + }); +}); + const transitions = [ { name: "closing the active document", diff --git a/src/features/plugins/runtimeUi.tsx b/src/features/plugins/runtimeUi.tsx index 2d04d0bc9b..c1438e5b28 100644 --- a/src/features/plugins/runtimeUi.tsx +++ b/src/features/plugins/runtimeUi.tsx @@ -472,6 +472,17 @@ function post(event: WorkbenchEvent) { const queued = queue.findIndex((queued) => queued.kind === "caretMoved" && queued.value.path === event.value.path); if (queued >= 0) queue.splice(queued, 1); } + // Project-tree expand notes must appear with the folder, not after the 500ms + // workbench batch — flush this event (and anything already queued) immediately. + if (event.kind === "projectTreeExpanded") { + queue.push(event); + if (postTimer) { + clearTimeout(postTimer); + postTimer = null; + } + flush(); + return; + } queue.push(event); const delay = event.kind === "caretMoved" ? CARET_FLUSH_MS : POST_INTERVAL_MS; const due = Date.now() + delay;