1- import { type FileContents , FileDiff , type DiffLineAnnotation , FileDiffOptions } from "@pierre/precision-diffs"
2- import { PreloadMultiFileDiffResult } from "@pierre/precision-diffs/ssr"
3- import { ComponentProps , createEffect , onCleanup , onMount , Show , splitProps } from "solid-js"
4- import { isServer } from "solid-js/web"
5- import { createDefaultOptions , styleVariables } from "./pierre"
6-
7- export type DiffProps < T = { } > = FileDiffOptions < T > & {
8- preloadedDiff ?: PreloadMultiFileDiffResult < T >
9- before : FileContents
10- after : FileContents
11- annotations ?: DiffLineAnnotation < T > [ ]
12- class ?: string
13- classList ?: ComponentProps < "div" > [ "classList" ]
14- }
1+ import { FileDiff } from "@pierre/precision-diffs"
2+ import { getOrCreateWorkerPoolSingleton } from "@pierre/precision-diffs/worker"
3+ import { createEffect , onCleanup , splitProps } from "solid-js"
4+ import { createDefaultOptions , type DiffProps , styleVariables } from "../pierre"
5+ import { workerFactory } from "../pierre/worker"
6+
7+ const workerPool = getOrCreateWorkerPoolSingleton ( {
8+ poolOptions : {
9+ workerFactory,
10+ // poolSize defaults to 8. More workers = more parallelism but
11+ // also more memory. Too many can actually slow things down.
12+ // poolSize: 8,
13+ } ,
14+ highlighterOptions : {
15+ theme : "OpenCode" ,
16+ // Optionally preload languages to avoid lazy-loading delays
17+ // langs: ["typescript", "javascript", "css", "html"],
18+ } ,
19+ } )
1520
1621// interface ThreadMetadata {
1722// threadId: string
@@ -21,21 +26,21 @@ export type DiffProps<T = {}> = FileDiffOptions<T> & {
2126
2227export function Diff < T > ( props : DiffProps < T > ) {
2328 let container ! : HTMLDivElement
24- let fileDiffRef ! : HTMLElement
2529 const [ local , others ] = splitProps ( props , [ "before" , "after" , "class" , "classList" , "annotations" ] )
2630
2731 let fileDiffInstance : FileDiff < T > | undefined
2832 const cleanupFunctions : Array < ( ) => void > = [ ]
2933
3034 createEffect ( ( ) => {
31- if ( props . preloadedDiff ) return
3235 container . innerHTML = ""
3336 if ( ! fileDiffInstance ) {
34- fileDiffInstance = new FileDiff < T > ( {
35- ...createDefaultOptions ( props . diffStyle ) ,
36- ...others ,
37- ...( props . preloadedDiff ?? { } ) ,
38- } )
37+ fileDiffInstance = new FileDiff < T > (
38+ {
39+ ...createDefaultOptions ( props . diffStyle ) ,
40+ ...others ,
41+ } ,
42+ workerPool ,
43+ )
3944 }
4045 fileDiffInstance . render ( {
4146 oldFile : local . before ,
@@ -45,60 +50,11 @@ export function Diff<T>(props: DiffProps<T>) {
4550 } )
4651 } )
4752
48- onMount ( ( ) => {
49- if ( isServer || ! props . preloadedDiff ) return
50- fileDiffInstance = new FileDiff < T > ( {
51- ...createDefaultOptions ( props . diffStyle ) ,
52- ...others ,
53- ...( props . preloadedDiff ?? { } ) ,
54- } )
55- // @ts -expect-error - fileContainer is private but needed for SSR hydration
56- fileDiffInstance . fileContainer = fileDiffRef
57- fileDiffInstance . hydrate ( {
58- oldFile : local . before ,
59- newFile : local . after ,
60- lineAnnotations : local . annotations ,
61- fileContainer : fileDiffRef ,
62- containerWrapper : container ,
63- } )
64-
65- // Hydrate annotation slots with interactive SolidJS components
66- // if (props.annotations.length > 0 && props.renderAnnotation != null) {
67- // for (const annotation of props.annotations) {
68- // const slotName = `annotation-${annotation.side}-${annotation.lineNumber}`;
69- // const slotElement = fileDiffRef.querySelector(
70- // `[slot="${slotName}"]`
71- // ) as HTMLElement;
72- //
73- // if (slotElement != null) {
74- // // Clear the static server-rendered content from the slot
75- // slotElement.innerHTML = '';
76- //
77- // // Mount a fresh SolidJS component into this slot using render().
78- // // This enables full SolidJS reactivity (signals, effects, etc.)
79- // const dispose = render(
80- // () => props.renderAnnotation!(annotation),
81- // slotElement
82- // );
83- // cleanupFunctions.push(dispose);
84- // }
85- // }
86- // }
87- } )
88-
8953 onCleanup ( ( ) => {
9054 // Clean up FileDiff event handlers and dispose SolidJS components
9155 fileDiffInstance ?. cleanUp ( )
9256 cleanupFunctions . forEach ( ( dispose ) => dispose ( ) )
9357 } )
9458
95- return (
96- < div data-component = "diff" style = { styleVariables } ref = { container } >
97- < file-diff ref = { fileDiffRef } id = "ssr-diff" >
98- < Show when = { isServer && props . preloadedDiff } >
99- { ( preloadedDiff ) => < template shadowrootmode = "open" innerHTML = { preloadedDiff ( ) . prerenderedHTML } /> }
100- </ Show >
101- </ file-diff >
102- </ div >
103- )
59+ return < div data-component = "diff" style = { styleVariables } ref = { container } />
10460}
0 commit comments