2026-02-01 20:53:00 +02:00
|
|
|
import React, { useMemo, useEffect } from 'react';
|
2025-12-14 02:29:46 +02:00
|
|
|
import type { SupportedLanguages } from '@pierre/diffs';
|
2026-02-16 14:15:19 +02:00
|
|
|
import { WorkerPoolManager } from '@pierre/diffs/worker';
|
2025-12-14 02:29:46 +02:00
|
|
|
|
|
|
|
|
import { useOptionalThemeSystem } from './useThemeSystem';
|
|
|
|
|
import { workerFactory } from '@/lib/diff/workerFactory';
|
2026-02-01 18:29:34 +02:00
|
|
|
import { ensurePierreThemeRegistered } from '@/lib/shiki/appThemeRegistry';
|
|
|
|
|
import { getDefaultTheme } from '@/lib/theme/themes';
|
2026-02-01 20:53:00 +02:00
|
|
|
// NOTE: keep provider lightweight; avoid main-thread diff parsing here.
|
2025-12-14 02:29:46 +02:00
|
|
|
|
2025-12-17 16:16:28 +02:00
|
|
|
// Preload common languages for faster initial diff rendering
|
2025-12-14 02:29:46 +02:00
|
|
|
const PRELOAD_LANGS: SupportedLanguages[] = [
|
2026-02-01 20:53:00 +02:00
|
|
|
// Keep small; workers load others on-demand.
|
2025-12-14 02:29:46 +02:00
|
|
|
'typescript',
|
|
|
|
|
'javascript',
|
|
|
|
|
'tsx',
|
2025-12-17 16:16:28 +02:00
|
|
|
'jsx',
|
2025-12-14 02:29:46 +02:00
|
|
|
'json',
|
2025-12-17 16:16:28 +02:00
|
|
|
'markdown',
|
2025-12-14 02:29:46 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
interface DiffWorkerProviderProps {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-16 14:15:19 +02:00
|
|
|
type WorkerPoolStyle = 'unified' | 'split';
|
|
|
|
|
|
|
|
|
|
const WORKER_POOL_CONFIG: Record<WorkerPoolStyle, { poolSize: number; totalASTLRUCacheSize: number; lineDiffType: 'none' | 'word-alt' }> = {
|
|
|
|
|
unified: {
|
|
|
|
|
poolSize: 1,
|
|
|
|
|
totalASTLRUCacheSize: 24,
|
|
|
|
|
lineDiffType: 'none',
|
|
|
|
|
},
|
|
|
|
|
split: {
|
|
|
|
|
poolSize: 2,
|
|
|
|
|
totalASTLRUCacheSize: 56,
|
|
|
|
|
lineDiffType: 'word-alt',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let unifiedWorkerPool: WorkerPoolManager | undefined;
|
|
|
|
|
let splitWorkerPool: WorkerPoolManager | undefined;
|
|
|
|
|
|
|
|
|
|
const createWorkerPool = (style: WorkerPoolStyle) => {
|
|
|
|
|
const config = WORKER_POOL_CONFIG[style];
|
|
|
|
|
const pool = new WorkerPoolManager(
|
|
|
|
|
{
|
|
|
|
|
workerFactory,
|
|
|
|
|
poolSize: config.poolSize,
|
|
|
|
|
totalASTLRUCacheSize: config.totalASTLRUCacheSize,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
theme: {
|
|
|
|
|
light: 'pierre-light',
|
|
|
|
|
dark: 'pierre-dark',
|
|
|
|
|
},
|
|
|
|
|
langs: PRELOAD_LANGS,
|
|
|
|
|
lineDiffType: config.lineDiffType,
|
|
|
|
|
preferredHighlighter: 'shiki-wasm',
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
void pool.initialize();
|
|
|
|
|
return pool;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getWorkerPool = (style: WorkerPoolStyle): WorkerPoolManager | undefined => {
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (style === 'split') {
|
|
|
|
|
splitWorkerPool ??= createWorkerPool('split');
|
|
|
|
|
return splitWorkerPool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unifiedWorkerPool ??= createWorkerPool('unified');
|
|
|
|
|
return unifiedWorkerPool;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-01 18:29:34 +02:00
|
|
|
const WorkerPoolWarmup: React.FC<{
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
renderTheme: { light: string; dark: string };
|
2026-02-01 20:53:00 +02:00
|
|
|
}> = ({ children, renderTheme }) => {
|
2026-02-16 14:15:19 +02:00
|
|
|
const unifiedPool = useWorkerPool('unified');
|
|
|
|
|
const splitPool = useWorkerPool('split');
|
2026-02-01 18:29:34 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-02-16 14:15:19 +02:00
|
|
|
if (unifiedPool) {
|
|
|
|
|
void unifiedPool.setRenderOptions({
|
|
|
|
|
theme: renderTheme,
|
|
|
|
|
lineDiffType: WORKER_POOL_CONFIG.unified.lineDiffType,
|
|
|
|
|
});
|
2026-02-01 18:29:34 +02:00
|
|
|
}
|
2026-02-16 14:15:19 +02:00
|
|
|
if (splitPool) {
|
|
|
|
|
void splitPool.setRenderOptions({
|
|
|
|
|
theme: renderTheme,
|
|
|
|
|
lineDiffType: WORKER_POOL_CONFIG.split.lineDiffType,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [renderTheme, splitPool, unifiedPool]);
|
2026-02-01 18:29:34 +02:00
|
|
|
|
2025-12-17 16:16:28 +02:00
|
|
|
return <>{children}</>;
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-14 02:29:46 +02:00
|
|
|
export const DiffWorkerProvider: React.FC<DiffWorkerProviderProps> = ({ children }) => {
|
|
|
|
|
const themeSystem = useOptionalThemeSystem();
|
|
|
|
|
|
2026-02-01 18:29:34 +02:00
|
|
|
const fallbackLight = getDefaultTheme(false);
|
|
|
|
|
const fallbackDark = getDefaultTheme(true);
|
|
|
|
|
|
|
|
|
|
const lightThemeId = themeSystem?.lightThemeId ?? fallbackLight.metadata.id;
|
|
|
|
|
const darkThemeId = themeSystem?.darkThemeId ?? fallbackDark.metadata.id;
|
|
|
|
|
|
|
|
|
|
const lightTheme =
|
|
|
|
|
themeSystem?.availableThemes.find((theme) => theme.metadata.id === lightThemeId) ??
|
|
|
|
|
fallbackLight;
|
|
|
|
|
const darkTheme =
|
|
|
|
|
themeSystem?.availableThemes.find((theme) => theme.metadata.id === darkThemeId) ??
|
|
|
|
|
fallbackDark;
|
|
|
|
|
|
|
|
|
|
ensurePierreThemeRegistered(lightTheme);
|
|
|
|
|
ensurePierreThemeRegistered(darkTheme);
|
2025-12-15 00:20:26 +02:00
|
|
|
|
2026-02-01 18:29:34 +02:00
|
|
|
const renderTheme = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
light: lightTheme.metadata.id,
|
|
|
|
|
dark: darkTheme.metadata.id,
|
|
|
|
|
}),
|
|
|
|
|
[darkTheme.metadata.id, lightTheme.metadata.id],
|
|
|
|
|
);
|
2025-12-14 02:29:46 +02:00
|
|
|
|
|
|
|
|
return (
|
2026-02-16 14:15:19 +02:00
|
|
|
<WorkerPoolWarmup renderTheme={renderTheme}>
|
|
|
|
|
{children}
|
|
|
|
|
</WorkerPoolWarmup>
|
2025-12-14 02:29:46 +02:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-14 02:34:15 +02:00
|
|
|
// eslint-disable-next-line react-refresh/only-export-components
|
2026-02-16 14:15:19 +02:00
|
|
|
export const useWorkerPool = (style: WorkerPoolStyle = 'unified'): WorkerPoolManager | undefined => {
|
|
|
|
|
return useMemo(() => getWorkerPool(style), [style]);
|
|
|
|
|
};
|