Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions apps/web/src/components/DiffWorkerPoolProvider.tsx
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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;
Expand All @@ -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,
}}
Expand Down
Loading