fix(plugins): retire plugin batch failure banner on state change

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-09-29 13:38:44 +08:00
1 parent e96d8890bc
commit 092a562ebf
4 files changed
+484 -43

No files matched your search

+36 -2
View File
@@ -10,6 +10,7 @@ describe("runBatch", () => {
async (item) => {
order.push(item.id);
},
(item) => item.id,
);
expect(order).toEqual(["a", "b", "c"]);
expect(outcome.succeeded).toEqual(["a", "b", "c"]);
@@ -25,15 +26,48 @@ describe("runBatch", () => {
ran.push(item.id);
if (item.id === "b") throw new Error("boom");
},
(item) => item.id,
);
expect(ran).toEqual(["a", "b", "c"]);
expect(outcome.succeeded).toEqual(["a", "c"]);
expect(outcome.failed).toEqual([{ name: "b", error: "boom" }]);
expect(outcome.failed).toEqual([{ id: "b", name: "b", error: "boom" }]);
});
it("records the item identity for a non-Error rejection too", async () => {
const outcome = await runBatch(
[{ id: "a" }, { id: "b" }],
(item) => item.id,
async (item) => {
if (item.id === "b") throw "denied";
},
(item) => item.id,
);
expect(outcome.failed).toEqual([{ id: "b", name: "b", error: "denied" }]);
});
it("reads identity and label before the action can destroy them", async () => {
// A failed action may leave the item unusable (see the plugin store leaving a half-replaced
// version dir behind), so the failure has to carry the identity captured up front.
const outcome = await runBatch(
[{ id: "a" }],
(item) => item.id,
async (item) => {
item.id = "";
throw new Error("boom");
},
(item) => item.id,
);
expect(outcome.failed).toEqual([{ id: "a", name: "a", error: "boom" }]);
});
it("returns an empty outcome for empty input", async () => {
const action = vi.fn();
const outcome = await runBatch([], (item: { id: string }) => item.id, action);
const outcome = await runBatch(
[],
(item: { id: string }) => item.id,
action,
(item: { id: string }) => item.id,
);
expect(action).not.toHaveBeenCalled();
expect(outcome.succeeded).toEqual([]);
expect(outcome.failed).toEqual([]);
+14 -7
View File
@@ -3,6 +3,9 @@
// and result aggregation so partial failures can be reported without aborting the batch.
export interface BatchItemOutcome {
// Item identity, kept alongside the label so a caller can map a failure back to the item it came
// from (retire a stale per-item banner, for instance) instead of string-matching the name.
id: string;
name: string;
ok: boolean;
error?: string;
@@ -11,25 +14,29 @@ export interface BatchItemOutcome {
export interface BatchOutcome {
results: BatchItemOutcome[];
succeeded: string[];
failed: { name: string; error: string }[];
failed: { id: string; name: string; error: string }[];
}
/**
* Run `action` over `items` sequentially, recording per-item success/failure. A failing item is
* captured and the batch continues; nothing is rolled back. `nameOf` supplies a human label used
* in the summary. `onProgress` fires after every item (success or failure) so callers can render
* live "(current/total)" progress. Empty input yields an empty outcome.
* in the summary and `idOf` the machine identity of the item. `onProgress` fires after every item
* (success or failure) so callers can render live "(current/total)" progress. Empty input yields
* an empty outcome.
*/
export async function runBatch<T>(items: readonly T[], nameOf: (item: T) => string, action: (item: T) => Promise<void>, onProgress?: (completed: number, total: number) => void): Promise<BatchOutcome> {
export async function runBatch<T>(items: readonly T[], nameOf: (item: T) => string, action: (item: T) => Promise<void>, idOf: (item: T) => string, onProgress?: (completed: number, total: number) => void): Promise<BatchOutcome> {
const results: BatchItemOutcome[] = [];
let completed = 0;
for (const item of items) {
// Identity (and label) are read before the action runs: a failed action may leave the item in a
// state it can no longer be identified from, and the failure still has to name its own item.
const id = idOf(item);
const name = nameOf(item);
try {
await action(item);
results.push({ name, ok: true });
results.push({ id, name, ok: true });
} catch (cause) {
results.push({ name, ok: false, error: cause instanceof Error ? cause.message : String(cause) });
results.push({ id, name, ok: false, error: cause instanceof Error ? cause.message : String(cause) });
}
completed += 1;
onProgress?.(completed, items.length);
@@ -37,7 +44,7 @@ export async function runBatch<T>(items: readonly T[], nameOf: (item: T) => stri
return {
results,
succeeded: results.filter((result) => result.ok).map((result) => result.name),
failed: results.filter((result) => !result.ok).map((result) => ({ name: result.name, error: result.error ?? "" })),
failed: results.filter((result) => !result.ok).map((result) => ({ id: result.id, name: result.name, error: result.error ?? "" })),
};
}