From 34739e181d1cc091f4c09e64c5fc376fda5e34e2 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Fri, 4 Dec 2026 14:03:58 +0800 Subject: [PATCH] feat(plugins): download several marketplace plugins at once (#7950) Installing a marketplace plugin disabled every other Install and Update button until it finished. Each marketplace plugin now tracks its own download: others can be installed meanwhile, and the install pages of finished downloads follow one another. A second click on a plugin already on its way does nothing. Refs #7911 --- src/features/plugins/PluginsPage.tsx | 13 +++-- .../plugins/__tests__/parallelInstall.test.ts | 52 +++++++++++++++++ src/features/plugins/store.ts | 56 +++++++++++++------ 3 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 src/features/plugins/__tests__/parallelInstall.test.ts diff --git a/src/features/plugins/PluginsPage.tsx b/src/features/plugins/PluginsPage.tsx index 7a7cba6d07..0f97f91ff2 100644 --- a/src/features/plugins/PluginsPage.tsx +++ b/src/features/plugins/PluginsPage.tsx @@ -793,8 +793,8 @@ function InstalledDetails({ plugin }: { plugin: PluginInfo }) { const locale = useI18n((s) => s.locale); const t = usePluginT(plugin.id); const busy = usePlugins((s) => !!s.busy[plugin.id]); - const installing = usePlugins((s) => s.installing); const update = useMarketUpdate(plugin); + const updating = usePlugins((s) => !!update && !!s.downloading[update.id]); const homepage = useHomepage(plugin); const status = STATUS[plugin.status]; const [confirmUninstall, setConfirmUninstall] = useState(false); @@ -970,7 +970,7 @@ function InstalledDetails({ plugin }: { plugin: PluginInfo }) { {(update || plugin.previousVersion || busy) && (
{update && ( - )} @@ -1229,7 +1229,7 @@ function MarketAction({ selected?: boolean; onInstalled?: (id: string) => void; }) { - const installing = usePlugins((s) => s.installing); + const downloading = usePlugins((s) => !!s.downloading[entry.id]); const update = isUpdate(entry); if (!entry.compatible) { return compact ? {tx("Incompatible")} : null; @@ -1243,7 +1243,7 @@ function MarketAction({ size={compact ? "small" : "normal"} variant={update || !compact || selected ? "primary" : "secondary"} className={selected && !update ? styles.selectedInstall : undefined} - disabled={installing} + disabled={downloading} aria-label={compact ? `${update ? tx("Update") : tx("Install")} ${entry.name}` : undefined} onClick={(e) => { e.stopPropagation(); @@ -1479,6 +1479,7 @@ function useSuggestions(market: readonly MarketPlugin[] | null): Map Object.keys(s.downloading).length > 0); const [tab, setTab] = useState(initialTab); const [collapsed, toggleGroup] = useCollapsed(); const [query, setQuery] = useState(""); @@ -1626,7 +1627,9 @@ export function PluginsPage({ initialTab = "marketplace" }: { initialTab?: Tab } }, ]} /> - {(loading || installing) && } + {(loading || installing || downloading) && ( + + )}
); diff --git a/src/features/plugins/__tests__/parallelInstall.test.ts b/src/features/plugins/__tests__/parallelInstall.test.ts new file mode 100644 index 0000000000..d71b85a930 --- /dev/null +++ b/src/features/plugins/__tests__/parallelInstall.test.ts @@ -0,0 +1,52 @@ +import { afterEach, expect, it } from "vitest"; +import { createMemoryPluginsBackend, setPluginsBackend, type PluginsBackend } from "../backend"; +import { installFromMarket, usePlugins } from "../store"; + +afterEach(() => setPluginsBackend(null)); + +function deferred() { + let resolve!: (dir: string) => void; + const promise = new Promise((r) => (resolve = r)); + return { promise, resolve }; +} + +it("downloads several marketplace plugins at once and installs them one after another", async () => { + const downloads = new Map([ + ["acme.a", deferred()], + ["acme.b", deferred()], + ]); + const log: string[] = []; + let installs = 0; + const backend: PluginsBackend = { + ...createMemoryPluginsBackend(), + marketDownload: (id) => { + log.push(`download ${id}`); + return downloads.get(id)!.promise; + }, + previewInstall: async (dir) => { + log.push(`preview ${dir}`); + expect(++installs).toBe(1); + return { id: dir, name: dir, version: "1.0.0", needsConsent: false, capabilities: [] } as never; + }, + installDir: async (dir) => { + await new Promise((r) => setTimeout(r, 5)); + installs--; + log.push(`install ${dir}`); + return { id: dir, name: dir, version: "1.0.0", status: "running" } as never; + }, + }; + setPluginsBackend(backend); + + const a = installFromMarket("acme.a", { quiet: true }); + const b = installFromMarket("acme.b", { quiet: true }); + // A second click on a plugin already on its way does nothing. + expect(await installFromMarket("acme.a")).toBeNull(); + expect(log).toEqual(["download acme.a", "download acme.b"]); + expect(usePlugins.getState().downloading).toEqual({ "acme.a": true, "acme.b": true }); + + downloads.get("acme.b")!.resolve("/dl/b"); + downloads.get("acme.a")!.resolve("/dl/a"); + expect(await Promise.all([a, b])).toEqual(["/dl/a", "/dl/b"]); + expect(log.slice(2)).toEqual(["preview /dl/b", "install /dl/b", "preview /dl/a", "install /dl/a"]); + expect(usePlugins.getState().downloading).toEqual({}); +}); diff --git a/src/features/plugins/store.ts b/src/features/plugins/store.ts index 9b3024039a..510e384598 100644 --- a/src/features/plugins/store.ts +++ b/src/features/plugins/store.ts @@ -33,6 +33,8 @@ interface PluginsState { /** Plugin ids with an operation in flight. */ busy: Readonly>; installing: boolean; + /** Marketplace plugin ids being downloaded or installed: several download at once. */ + downloading: Readonly>; /** Locally available packages (Marketplace tab); `null` until loaded. */ catalog: CatalogPlugin[] | null; catalogError: string | null; @@ -52,6 +54,7 @@ export const usePlugins = create(() => ({ error: null, busy: {}, installing: false, + downloading: {}, catalog: null, catalogError: null, marketUrl: null, @@ -282,25 +285,46 @@ export function isUpdate(entry: MarketPlugin): boolean { return entry.compatible && !!entry.installed && compareVersions(entry.version, entry.installed) > 0; } -/** Downloads (and checks) a marketplace plugin, then the install page as from disk. */ +const setDownloading = (id: string, on: boolean) => + usePlugins.setState((s) => { + const { [id]: _, ...rest } = s.downloading; + return { downloading: on ? { ...rest, [id]: true } : rest }; + }); + +/** Install pages of finished downloads, one after another. */ +let installQueue: Promise = Promise.resolve(); +function nextInstall(install: () => Promise): Promise { + const run = installQueue.then(install, install); + installQueue = run.catch(() => undefined); + return run; +} + +/** + * Downloads (and checks) a marketplace plugin, then the install page as from disk. Several + * plugins download at once; their install pages follow one another. + */ export async function installFromMarket(id: string, options: InstallOptions = {}): Promise { - usePlugins.setState({ installing: true }); - let dir: string; + if (usePlugins.getState().downloading[id]) return null; + setDownloading(id, true); try { - dir = await pluginsBackend().marketDownload(id); - } catch (e) { - usePlugins.setState({ installing: false }); - notifications().notify({ - group: "plugins", - level: "error", - title: "Could not download plugin", - body: errorText(e), - }); - return null; + let dir: string; + try { + dir = await pluginsBackend().marketDownload(id); + } catch (e) { + notifications().notify({ + group: "plugins", + level: "error", + title: "Could not download plugin", + body: errorText(e), + }); + return null; + } + const installed = await nextInstall(() => installFromDisk(dir, false, options)); + void refreshMarket(false); + return installed; + } finally { + setDownloading(id, false); } - const installed = await installFromDisk(dir, false, options); - void refreshMarket(false); - return installed; } /** `plugins.installFromMarket`: the id or ids in `ids`, one install page after another. */