| ... | @@ -1,9 +1,20 @@ | ... | @@ -1,9 +1,20 @@ |
| 1 | import { WorkerPoolContextProvider, useWorkerPool } from "@pierre/diffs/react"; | 1 | import { WorkerPoolContextProvider, useWorkerPool } from "@pierre/diffs/react"; |
| 2 | import DiffsWorker from "@pierre/diffs/worker/worker.js?worker"; | 2 | import DiffsWorker from "@pierre/diffs/worker/worker.js?worker"; |
| | 3 | import * as Schema from "effect/Schema"; |
| 3 | import { useEffect, useMemo, type ReactNode } from "react"; | 4 | import { useEffect, useMemo, type ReactNode } from "react"; |
| 4 | import { useTheme } from "../hooks/useTheme"; | 5 | import { useTheme } from "../hooks/useTheme"; |
| 5 | import { resolveDiffThemeName, type DiffThemeName } from "../lib/diffRendering"; | 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 | function DiffWorkerThemeSync({ themeName }: { themeName: DiffThemeName }) { | 18 | function DiffWorkerThemeSync({ themeName }: { themeName: DiffThemeName }) { |
| 8 | const workerPool = useWorkerPool(); | 19 | const workerPool = useWorkerPool(); |
| 9 | | 20 | |
| ... | @@ -12,17 +23,23 @@ function DiffWorkerThemeSync({ themeName }: { themeName: DiffThemeName }) { | ... | @@ -12,17 +23,23 @@ function DiffWorkerThemeSync({ themeName }: { themeName: DiffThemeName }) { |
| 12 | return; | 23 | return; |
| 13 | } | 24 | } |
| 14 | | 25 | |
| 15 | const current = workerPool.getDiffRenderOptions(); | 26 | let operation: DiffWorkerError["operation"] = "get-render-options"; |
| 16 | if (current.theme === themeName) { | 27 | void (async () => { |
| 17 | return; | 28 | try { |
| 18 | } | 29 | const current = workerPool.getDiffRenderOptions(); |
| | 30 | if (current.theme === themeName) { |
| | 31 | return; |
| | 32 | } |
| 19 | | 33 | |
| 20 | void workerPool | 34 | operation = "set-render-options"; |
| 21 | .setRenderOptions({ | 35 | await workerPool.setRenderOptions({ |
| 22 | ...current, | 36 | ...current, |
| 23 | theme: themeName, | 37 | theme: themeName, |
| 24 | }) | 38 | }); |
| 25 | .catch(() => undefined); | 39 | } catch (cause) { |
| | 40 | console.error(new DiffWorkerError({ operation, themeName, cause })); |
| | 41 | } |
| | 42 | })(); |
| 26 | }, [themeName, workerPool]); | 43 | }, [themeName, workerPool]); |
| 27 | | 44 | |
| 28 | return null; | 45 | return null; |
| ... | @@ -40,7 +57,17 @@ export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) { | ... | @@ -40,7 +57,17 @@ export function DiffWorkerPoolProvider({ children }: { children?: ReactNode }) { |
| 40 | return ( | 57 | return ( |
| 41 | <WorkerPoolContextProvider | 58 | <WorkerPoolContextProvider |
| 42 | poolOptions={{ | 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 | poolSize: workerPoolSize, | 71 | poolSize: workerPoolSize, |
| 45 | totalASTLRUCacheSize: 240, | 72 | totalASTLRUCacheSize: 240, |
| 46 | }} | 73 | }} |