diff --git a/src/features/plugins/__tests__/plugins.test.tsx b/src/features/plugins/__tests__/plugins.test.tsx index 6e5aa2cdb1..41b0b5352e 100644 --- a/src/features/plugins/__tests__/plugins.test.tsx +++ b/src/features/plugins/__tests__/plugins.test.tsx @@ -22,6 +22,7 @@ import { applyThemeAttributes, previewTheme, useThemeOverride } from "@/workbenc import { createMemoryPluginsBackend, sampleCatalog, setPluginsBackend, type PluginsBackend } from "../backend"; import { applyPluginTheme, + expireInstalledUpdates, followThemeSetting, PLUGIN_THEME_STYLE_ID, handlePluginEvent, @@ -53,6 +54,7 @@ import { setAllInstalledEnabled, setConsentPrompt, setPluginEnabled, + syncPluginsAcrossWindows, usePlugins, type ConsentRequest, } from "../store"; @@ -1771,6 +1773,26 @@ describe("marketplace", () => { await waitFor(() => expect(consents.map(([id]) => id)).toEqual(["b"])); }); + it("hides the update notice once another window installed the updates (#7742)", async () => { + const expire = vi.fn(); + provideNotificationService({ notify: (n) => void notes.push(n), expire }); + const base = createMemoryPluginsBackend([plugin({ id: "a", version: "1.0.0" })]); + setPluginsBackend(base); + const stops = [expireInstalledUpdates(), syncPluginsAcrossWindows()]; + await refreshPlugins(); + handlePluginEvent({ type: "updatesAvailable", updates: [{ id: "a", name: "A", version: "1.1.0" }] }); + expect(notes.at(-1)).toMatchObject({ key: "plugins.updates" }); + // The other window updated A and announced it; this one reads the plugins again. + const list = vi.spyOn(base, "list").mockResolvedValue({ + ...(await base.list()), + plugins: [plugin({ id: "a", version: "1.1.0" })], + }); + window.dispatchEvent(new StorageEvent("storage", { key: "openidea.plugins.changed.v1" })); + await waitFor(() => expect(expire).toHaveBeenCalledWith("plugins.updates")); + expect(list).toHaveBeenCalled(); + stops.forEach((stop) => stop()); + }); + it("announces updates with actions to install them or show the plugins", async () => { updatesBackend({ a: preview("a", "1.1.0"), b: preview("b", "2.0.0") }); handlePluginEvent({ diff --git a/src/features/plugins/bridge.ts b/src/features/plugins/bridge.ts index 53e842a217..cc812a52ef 100644 --- a/src/features/plugins/bridge.ts +++ b/src/features/plugins/bridge.ts @@ -19,10 +19,12 @@ import { trackIconTheme } from "./iconThemes"; import { defineBuiltinUiPoints } from "./points"; import { handleRuntimeEvent, refreshEventKinds, startRuntimeUi } from "./runtimeUi"; import { + compareVersions, grantPlugin, runPluginCommand, refreshPlugins, setActivePluginTheme, + syncPluginsAcrossWindows, updateAllPlugins, usePlugins, } from "./store"; @@ -60,9 +62,15 @@ function trackActiveFile(): () => void { // --- events ----------------------------------------------------------------- +const UPDATES_NOTICE = "plugins.updates"; +/** The updates the last notice offered, until they are installed (here or in another window). */ +let offered: readonly PluginUpdate[] = []; + function announceUpdates(updates: readonly PluginUpdate[]) { + offered = updates; const names = updates.map((u) => `${u.name} ${u.version}`).join(", "); notifications().notify({ + key: UPDATES_NOTICE, group: "plugins", level: "info", title: updates.length === 1 ? "Plugin update available" : `${updates.length} plugin updates available`, @@ -74,6 +82,20 @@ function announceUpdates(updates: readonly PluginUpdate[]) { }); } +/** Hides the update notice once every update it offered is installed. */ +export function expireInstalledUpdates(): () => void { + return usePlugins.subscribe((s, prev) => { + if (s.plugins === prev.plugins || !offered.length) return; + const installed = (u: PluginUpdate) => { + const plugin = s.plugins.find((p) => p.id === u.id); + return !!plugin && compareVersions(plugin.version, u.version) >= 0; + }; + if (!offered.every(installed)) return; + offered = []; + notifications().expire?.(UPDATES_NOTICE); + }); +} + export function handlePluginEvent(e: PluginEvent) { switch (e.type) { case "commandsChanged": @@ -167,6 +189,8 @@ export function startPluginBridge(): () => void { trackActiveFile(), trackActivationEvents(), startRuntimeUi(), + expireInstalledUpdates(), + syncPluginsAcrossWindows(), ]; return () => { if (refreshTimer) clearTimeout(refreshTimer); diff --git a/src/features/plugins/store.ts b/src/features/plugins/store.ts index f18f9bb93e..cd468ca4ca 100644 --- a/src/features/plugins/store.ts +++ b/src/features/plugins/store.ts @@ -137,6 +137,28 @@ export async function refreshPlugins(): Promise { } } +/** Written after a change to the installed plugins, so the other windows read them again. */ +const CHANGED_KEY = "openidea.plugins.changed.v1"; + +function announcePluginsChanged() { + try { + localStorage.setItem(CHANGED_KEY, String(Date.now())); + } catch { + // Storage unavailable: other windows catch up on their next refresh. + } +} + +/** Takes in plugins another window installed, updated, rolled back, removed or switched. */ +export function syncPluginsAcrossWindows(): () => void { + const onStorage = (e: StorageEvent) => { + if (e.key !== CHANGED_KEY) return; + void refreshPlugins(); + if (usePlugins.getState().market) void refreshMarket(false); + }; + window.addEventListener("storage", onStorage); + return () => window.removeEventListener("storage", onStorage); +} + async function withBusy(id: string, title: string, op: () => Promise): Promise { usePlugins.setState((s) => ({ busy: { ...s.busy, [id]: true } })); try { @@ -152,6 +174,7 @@ async function withBusy(id: string, title: string, op: () => Promise): return { busy }; }); await refreshPlugins(); + announcePluginsChanged(); } } @@ -309,6 +332,7 @@ export async function rollBackPlugin(id: string): Promise { notifications().notify({ group: "plugins", level: "error", title: "Could not roll back", body: errorText(e) }); } finally { await refreshPlugins(); + announcePluginsChanged(); } } @@ -410,6 +434,7 @@ export async function installFromDisk( } finally { usePlugins.setState({ installing: false }); await refreshPlugins(); + if (installed) announcePluginsChanged(); if (installed) await adoptIconTheme(installed); } } diff --git a/src/features/tools/__tests__/tools.test.tsx b/src/features/tools/__tests__/tools.test.tsx index 97aaaf489a..f81e03d102 100644 --- a/src/features/tools/__tests__/tools.test.tsx +++ b/src/features/tools/__tests__/tools.test.tsx @@ -15,6 +15,7 @@ import { loadToolUpdates, publishToolUpdates, refreshTools, + syncToolsAcrossWindows, uninstallTool, UPDATES_EVERY_MS, useTools, @@ -84,6 +85,37 @@ describe("tool updates (#7365)", () => { }); }); +describe("tools across windows (#7742)", () => { + afterEach(() => { + localStorage.removeItem("openidea.tools.updates.v1"); + localStorage.removeItem("openidea.tools.changed.v1"); + }); + + it("reads the tools and updates again after another window installed one", async () => { + const stop = syncToolsAcrossWindows(); + useTools.setState({ updates: { gopls: { name: "gopls", current: "1.0", latest: "1.1" } } }); + const list = vi.spyOn(backend, "list"); + // The other window stored its updates without gopls and announced the install. + localStorage.setItem("openidea.tools.updates.v1", JSON.stringify({ at: 1, updates: [] })); + window.dispatchEvent(new StorageEvent("storage", { key: "openidea.tools.updates.v1" })); + window.dispatchEvent(new StorageEvent("storage", { key: "openidea.tools.changed.v1" })); + expect(useTools.getState().updates).toEqual({}); + await waitFor(() => expect(list).toHaveBeenCalled()); + stop(); + }); + + it("tells the other windows when a tool is installed or uninstalled", async () => { + await refreshTools(); + const set = vi.spyOn(Storage.prototype, "setItem"); + await installTool("gopls"); + expect(set).toHaveBeenCalledWith("openidea.tools.changed.v1", expect.any(String)); + set.mockClear(); + await uninstallTool("gopls"); + expect(set).toHaveBeenCalledWith("openidea.tools.changed.v1", expect.any(String)); + set.mockRestore(); + }); +}); + describe("tools store", () => { it("installs, streams progress, restarts servers and notifies", async () => { await refreshTools(); diff --git a/src/features/tools/index.ts b/src/features/tools/index.ts index 984a076367..3008b8f54d 100644 --- a/src/features/tools/index.ts +++ b/src/features/tools/index.ts @@ -24,6 +24,7 @@ import { loadToolUpdates, publishToolUpdates, refreshTools, + syncToolsAcrossWindows, SETTINGS_PAGE_ID, toolByName, useTools, @@ -97,6 +98,7 @@ export function activate(): () => void { }, ]), publishToolUpdates(), + syncToolsAcrossWindows(), lookForToolUpdatesLater(), installMissingServerPrompts(), installServerReadyExpiry(), diff --git a/src/features/tools/store.ts b/src/features/tools/store.ts index 811661a69f..fe4b300d2c 100644 --- a/src/features/tools/store.ts +++ b/src/features/tools/store.ts @@ -121,6 +121,29 @@ export function publishToolUpdates(): () => void { }); } +/** Written after an install or uninstall, so the other windows read the tools again. */ +const CHANGED_KEY = "openidea.tools.changed.v1"; + +function announceToolsChanged() { + try { + localStorage.setItem(CHANGED_KEY, String(Date.now())); + } catch { + // Storage unavailable: other windows catch up on their next refresh. + } +} + +/** Takes in what another window installed, uninstalled or found: its tools and updates. */ +export function syncToolsAcrossWindows(): () => void { + const onStorage = (e: StorageEvent) => { + if (e.key === CHANGED_KEY) void refreshTools(); + if (e.key !== UPDATES_KEY) return; + const updates = Object.fromEntries((cachedUpdates()?.updates ?? []).map((u) => [u.name, u])); + if (JSON.stringify(updates) !== JSON.stringify(useTools.getState().updates)) useTools.setState({ updates }); + }; + window.addEventListener("storage", onStorage); + return () => window.removeEventListener("storage", onStorage); +} + /** Looks for newer versions of the tools the IDE installed; offline, none are shown. */ export function checkToolUpdates(): Promise { checkingUpdates ??= (async () => { @@ -221,6 +244,7 @@ export function installTool(name: string, opts: { notificationKey?: string } = { }); // Shared packages (e.g. vscode-langservers-extracted) change sibling rows too. void refreshTools(); + announceToolsChanged(); return info; } catch (e) { if (isCancelled(e)) { @@ -259,6 +283,7 @@ export async function uninstallTool(name: string): Promise { try { upsertTool(await toolsBackend().uninstall(name)); void refreshTools(); + announceToolsChanged(); return true; } catch (e) { notifications().notify({