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 && (
-
);
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. */