| ... | ... | @@ -1,9 +1,20 @@ |
| 1 | 1 | import { WorkerPoolContextProvider, useWorkerPool } from "@pierre/diffs/react"; |
| 2 | 2 | import DiffsWorker from "@pierre/diffs/worker/worker.js?worker"; |
| 3 | import * as Schema from "effect/Schema"; |
| 3 | 4 | import { useEffect, useMemo, type ReactNode } from "react"; |
| 4 | 5 | import { useTheme } from "../hooks/useTheme"; |
| 5 | 6 | import { resolveDiffThemeName, type DiffThemeName } from "../lib/diffRendering"; |
| 6 | 7 | |
| 8 | export class DiffWorkerError extends Schema.TaggedErrorClass<DiffWorkerError>()("DiffWorkerError", { |
| 9 | operation: Schema.Literals(["create-worker", "get-render-options", "set-render-options"]), |
| 10 | themeName: Schema.Literals(["pierre-light", "pierre-dark"]), |
| 11 | cause: Schema.Defect(), |
| 12 | }) { |
| 13 | override get message(): string { |
| 14 | return `Diff worker operation ${this.operation} failed for theme ${this.themeName}.`; |
| 15 | } |
| 16 | } |
| 17 | |
| 7 | 18 | function DiffWorkerThemeSync({ themeName }: { themeName: DiffThemeName }) { |
| 8 | 19 | const workerPool = useWorkerPool(); |
| 9 | 20 | |
| ... | ... | @@ -12,17 +23,23 @@ function DiffWorkerThemeSync({ themeName }: { themeName: DiffThemeName }) { |
| 12 | 23 | return; |
| 13 | 24 | } |
| 14 | 25 | |
| 15 | | const current = workerPool.getDiffRenderOptions(); |
| 16 | | if (current.theme === themeName) { |
| 17 | | return; |
| 18 | | } |
| 26 | let operation: DiffWorkerError["operation"] = "get-render-options"; |
| 27 | void (async () => { |
| 28 | try { |
| 29 | const current = workerPool.getDiffRenderOptions(); |
| 30 | if (current.theme === themeName) { |
| 31 | return; |
| 32 | } |
| 19 | 33 | |
| 20 | | void workerPool |
| 21 | | .setRenderOptions({ |
| 22 | | ...current, |
| 23 | | theme: themeName, |
| 24 | | }) |
| 25 | | .catch(() => undefined); |
| 34 | operation = "set-render-options"; |
| 35 | await workerPool.setRenderOptions({ |
| 36 | ...current, |
| 37 | theme: themeName, |
| 38 | }); |
| 39 | } catch (cause) { |
| 40 | console.error(new DiffWorkerError({ operation, themeName, cause })); |
| 41 | } |
| 42 | })(); |
| 26 | 43 | }, [themeName, workerPool]); |
| 27 | 44 | |
| 28 | 45 | return null; |
| ... | ... | @@ -40,7 +57,17 @@ export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) { |
| 40 | 57 | return ( |
| 41 | 58 | <WorkerPoolContextProvider |
| 42 | 59 | poolOptions={{ |
| 43 | | workerFactory: () => new DiffsWorker(), |
| 60 | workerFactory: () => { |
| 61 | try { |
| 62 | return new DiffsWorker(); |
| 63 | } catch (cause) { |
| 64 | throw new DiffWorkerError({ |
| 65 | operation: "create-worker", |
| 66 | themeName: diffThemeName, |
| 67 | cause, |
| 68 | }); |
| 69 | } |
| 70 | }, |
| 44 | 71 | poolSize: workerPoolSize, |
| 45 | 72 | totalASTLRUCacheSize: 240, |
| 46 | 73 | }} |