[codex] Structure web diff worker failures (#3356)

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex authored and GitHub committed 2026-06-20 11:02:07 -07:00
1 parent 40d14647db
commit 08650a7424
1 file changed
+38 -11
@@ -1,9 +1,20 @@
import { WorkerPoolContextProvider, useWorkerPool } from "@pierre/diffs/react";
import DiffsWorker from "@pierre/diffs/worker/worker.js?worker";
import * as Schema from "effect/Schema";
import { useEffect, useMemo, type ReactNode } from "react";
import { useTheme } from "../hooks/useTheme";
import { resolveDiffThemeName, type DiffThemeName } from "../lib/diffRendering";
export class DiffWorkerError extends Schema.TaggedErrorClass<DiffWorkerError>()("DiffWorkerError", {
operation: Schema.Literals(["create-worker", "get-render-options", "set-render-options"]),
themeName: Schema.Literals(["pierre-light", "pierre-dark"]),
cause: Schema.Defect(),
}) {
override get message(): string {
return `Diff worker operation ${this.operation} failed for theme ${this.themeName}.`;
}
}
function DiffWorkerThemeSync({ themeName }: { themeName: DiffThemeName }) {
const workerPool = useWorkerPool();
@@ -12,17 +23,23 @@ function DiffWorkerThemeSync({ themeName }: { themeName: DiffThemeName }) {
return;
}
const current = workerPool.getDiffRenderOptions();
if (current.theme === themeName) {
return;
}
let operation: DiffWorkerError["operation"] = "get-render-options";
void (async () => {
try {
const current = workerPool.getDiffRenderOptions();
if (current.theme === themeName) {
return;
}
void workerPool
.setRenderOptions({
...current,
theme: themeName,
})
.catch(() => undefined);
operation = "set-render-options";
await workerPool.setRenderOptions({
...current,
theme: themeName,
});
} catch (cause) {
console.error(new DiffWorkerError({ operation, themeName, cause }));
}
})();
}, [themeName, workerPool]);
return null;
@@ -40,7 +57,17 @@ export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) {
return (
<WorkerPoolContextProvider
poolOptions={{
workerFactory: () => new DiffsWorker(),
workerFactory: () => {
try {
return new DiffsWorker();
} catch (cause) {
throw new DiffWorkerError({
operation: "create-worker",
themeName: diffThemeName,
cause,
});
}
},
poolSize: workerPoolSize,
totalASTLRUCacheSize: 240,
}}