2025-12-17 16:16:28 +02:00
|
|
|
import React, { useMemo, useEffect, useRef } from 'react';
|
2025-12-14 02:29:46 +02:00
|
|
|
import { WorkerPoolContextProvider, useWorkerPool } from '@pierre/diffs/react';
|
2025-12-17 16:16:28 +02:00
|
|
|
import { parseDiffFromFile, type FileContents } from '@pierre/diffs';
|
2025-12-14 02:29:46 +02:00
|
|
|
import type { SupportedLanguages } from '@pierre/diffs';
|
|
|
|
|
|
|
|
|
|
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';
|
2025-12-17 16:16:28 +02:00
|
|
|
import { useGitStore } from '@/stores/useGitStore';
|
|
|
|
|
import { getLanguageFromExtension } from '@/lib/toolHelpers';
|
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[] = [
|
|
|
|
|
'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
|
|
|
'html',
|
|
|
|
|
'css',
|
|
|
|
|
'markdown',
|
|
|
|
|
'yaml',
|
|
|
|
|
'python',
|
|
|
|
|
'rust',
|
|
|
|
|
'go',
|
|
|
|
|
'bash',
|
2025-12-14 02:29:46 +02:00
|
|
|
];
|
|
|
|
|
|
2026-01-28 12:29:01 +02:00
|
|
|
// Limit warmup to prevent memory bloat with many modified files
|
|
|
|
|
const WARMUP_MAX_FILES = 10;
|
|
|
|
|
|
2025-12-17 16:16:28 +02:00
|
|
|
// Matches cache key logic in `packages/ui/src/components/views/PierreDiffViewer.tsx`
|
2026-02-01 18:29:34 +02:00
|
|
|
function getPierreCacheKey(fileName: string, original: string, modified: string, themeKey: string): string {
|
2025-12-17 16:16:28 +02:00
|
|
|
const sampleOriginal = original.length > 100
|
|
|
|
|
? `${original.slice(0, 50)}${original.slice(-50)}`
|
|
|
|
|
: original;
|
|
|
|
|
const sampleModified = modified.length > 100
|
|
|
|
|
? `${modified.slice(0, 50)}${modified.slice(-50)}`
|
|
|
|
|
: modified;
|
2026-02-01 18:29:34 +02:00
|
|
|
return `${themeKey}::${fileName}:${original.length}:${modified.length}:${sampleOriginal.length}:${sampleModified.length}`;
|
2025-12-17 16:16:28 +02:00
|
|
|
}
|
|
|
|
|
|
2025-12-14 02:29:46 +02:00
|
|
|
interface DiffWorkerProviderProps {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-17 16:16:28 +02:00
|
|
|
type IdleDeadlineLike = { timeRemaining(): number; didTimeout: boolean };
|
|
|
|
|
|
|
|
|
|
function scheduleWarmupWork(cb: (deadline?: IdleDeadlineLike) => void): () => void {
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
|
return () => {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const id = window.setTimeout(() => cb(undefined), 0);
|
|
|
|
|
return () => window.clearTimeout(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Component that warms up the worker pool and precomputes diff ASTs
|
2026-02-01 18:29:34 +02:00
|
|
|
const WorkerPoolWarmup: React.FC<{
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
themeKey: string;
|
|
|
|
|
renderTheme: { light: string; dark: string };
|
|
|
|
|
}> = ({ children, themeKey, renderTheme }) => {
|
2025-12-17 16:16:28 +02:00
|
|
|
const workerPool = useWorkerPool();
|
|
|
|
|
const activeDirectory = useGitStore((state) => state.activeDirectory);
|
|
|
|
|
const lastStatusChange = useGitStore((state) => {
|
|
|
|
|
if (!activeDirectory) return 0;
|
|
|
|
|
return state.directories.get(activeDirectory)?.lastStatusChange ?? 0;
|
|
|
|
|
});
|
|
|
|
|
const diffCacheSize = useGitStore((state) => {
|
|
|
|
|
if (!activeDirectory) return 0;
|
|
|
|
|
return state.directories.get(activeDirectory)?.diffCache.size ?? 0;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const didDummyWarmupRef = useRef(false);
|
|
|
|
|
const warmedStatusRef = useRef(new Map<string, number>());
|
|
|
|
|
|
2026-02-01 18:29:34 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
warmedStatusRef.current.clear();
|
|
|
|
|
}, [themeKey]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!workerPool) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Important: WorkerPoolContextProvider uses a singleton and does not react to
|
|
|
|
|
// prop changes. Update the worker pool render options explicitly.
|
|
|
|
|
void workerPool.setRenderOptions({ theme: renderTheme });
|
|
|
|
|
}, [renderTheme, workerPool]);
|
|
|
|
|
|
2025-12-17 16:16:28 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!workerPool || didDummyWarmupRef.current) return;
|
|
|
|
|
|
|
|
|
|
didDummyWarmupRef.current = true;
|
|
|
|
|
|
|
|
|
|
const dummyFile: FileContents = {
|
|
|
|
|
name: 'warmup.ts',
|
|
|
|
|
contents: 'const x = 1;',
|
|
|
|
|
lang: 'typescript',
|
|
|
|
|
cacheKey: 'warmup-file',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const dummyDiff = parseDiffFromFile(dummyFile, { ...dummyFile, contents: 'const x = 2;', cacheKey: 'warmup-file-2' });
|
|
|
|
|
|
|
|
|
|
const dummyInstance = {
|
|
|
|
|
onHighlightSuccess: () => {},
|
|
|
|
|
onHighlightError: () => {},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
workerPool.highlightDiffAST(dummyInstance, dummyDiff);
|
|
|
|
|
}, [workerPool]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!workerPool || !activeDirectory) return;
|
|
|
|
|
if (!lastStatusChange || diffCacheSize === 0) return;
|
|
|
|
|
|
|
|
|
|
const dirState = useGitStore.getState().directories.get(activeDirectory);
|
|
|
|
|
if (!dirState || dirState.diffCache.size === 0) return;
|
|
|
|
|
|
|
|
|
|
const alreadyWarmedAt = warmedStatusRef.current.get(activeDirectory) ?? 0;
|
|
|
|
|
if (alreadyWarmedAt >= dirState.lastStatusChange) return;
|
|
|
|
|
|
|
|
|
|
// Only warm once per status-change tick
|
|
|
|
|
warmedStatusRef.current.set(activeDirectory, dirState.lastStatusChange);
|
|
|
|
|
|
|
|
|
|
let cancelled = false;
|
|
|
|
|
let cancelScheduled: (() => void) | null = null;
|
|
|
|
|
|
2026-01-28 12:29:01 +02:00
|
|
|
// Limit entries to prevent memory bloat with many modified files
|
|
|
|
|
const allEntries = Array.from(dirState.diffCache.entries());
|
|
|
|
|
const entries = allEntries.slice(0, WARMUP_MAX_FILES);
|
2025-12-17 16:16:28 +02:00
|
|
|
|
|
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
|
|
const processChunk = (deadline?: IdleDeadlineLike) => {
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
|
|
|
|
|
const chunkStart = Date.now();
|
|
|
|
|
while (index < entries.length) {
|
|
|
|
|
if (deadline && deadline.timeRemaining() < 8) break;
|
|
|
|
|
if (!deadline && Date.now() - chunkStart > 8) break;
|
|
|
|
|
|
|
|
|
|
const [filePath, diff] = entries[index];
|
|
|
|
|
index += 1;
|
|
|
|
|
|
|
|
|
|
const language = getLanguageFromExtension(filePath) || 'text';
|
2026-02-01 18:29:34 +02:00
|
|
|
const cacheKey = getPierreCacheKey(filePath, diff.original, diff.modified, themeKey);
|
2025-12-17 16:16:28 +02:00
|
|
|
|
|
|
|
|
const oldFile: FileContents = {
|
|
|
|
|
name: filePath,
|
|
|
|
|
contents: diff.original,
|
|
|
|
|
lang: language as FileContents['lang'],
|
|
|
|
|
cacheKey: `old-${cacheKey}`,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const newFile: FileContents = {
|
|
|
|
|
name: filePath,
|
|
|
|
|
contents: diff.modified,
|
|
|
|
|
lang: language as FileContents['lang'],
|
|
|
|
|
cacheKey: `new-${cacheKey}`,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fileDiff = parseDiffFromFile(oldFile, newFile);
|
|
|
|
|
|
|
|
|
|
// Use a unique instance per request so Pierre doesn't ignore earlier results.
|
|
|
|
|
const instance = {
|
|
|
|
|
onHighlightSuccess: () => {},
|
|
|
|
|
onHighlightError: () => {},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
workerPool.highlightDiffAST(instance, fileDiff);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (index < entries.length) {
|
|
|
|
|
cancelScheduled = scheduleWarmupWork(processChunk);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Kick off immediately, then continue in chunks.
|
|
|
|
|
processChunk(undefined);
|
|
|
|
|
|
|
|
|
|
if (index < entries.length) {
|
|
|
|
|
cancelScheduled = scheduleWarmupWork(processChunk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
cancelScheduled?.();
|
|
|
|
|
};
|
2026-02-01 18:29:34 +02:00
|
|
|
}, [workerPool, activeDirectory, lastStatusChange, diffCacheSize, themeKey]);
|
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();
|
|
|
|
|
const isDark = themeSystem?.currentTheme?.metadata?.variant === 'dark';
|
|
|
|
|
|
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
|
|
|
|
2025-12-14 02:29:46 +02:00
|
|
|
const highlighterOptions = useMemo(() => ({
|
|
|
|
|
theme: {
|
2026-02-01 18:29:34 +02:00
|
|
|
dark: darkTheme.metadata.id,
|
|
|
|
|
light: lightTheme.metadata.id,
|
2025-12-14 02:29:46 +02:00
|
|
|
},
|
|
|
|
|
themeType: isDark ? ('dark' as const) : ('light' as const),
|
|
|
|
|
langs: PRELOAD_LANGS,
|
2026-02-01 18:29:34 +02:00
|
|
|
}), [darkTheme.metadata.id, isDark, lightTheme.metadata.id]);
|
|
|
|
|
|
|
|
|
|
const workerThemeKey = `${lightTheme.metadata.id}:${darkTheme.metadata.id}:${isDark ? 'dark' : 'light'}`;
|
|
|
|
|
|
|
|
|
|
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 (
|
|
|
|
|
<WorkerPoolContextProvider
|
|
|
|
|
poolOptions={{
|
|
|
|
|
workerFactory,
|
2026-01-28 12:29:01 +02:00
|
|
|
poolSize: 2,
|
|
|
|
|
totalASTLRUCacheSize: 50,
|
2025-12-14 02:29:46 +02:00
|
|
|
}}
|
|
|
|
|
highlighterOptions={highlighterOptions}
|
|
|
|
|
>
|
2026-02-01 18:29:34 +02:00
|
|
|
<WorkerPoolWarmup
|
|
|
|
|
themeKey={workerThemeKey}
|
|
|
|
|
renderTheme={renderTheme}
|
|
|
|
|
>
|
2025-12-17 16:16:28 +02:00
|
|
|
{children}
|
|
|
|
|
</WorkerPoolWarmup>
|
2025-12-14 02:29:46 +02:00
|
|
|
</WorkerPoolContextProvider>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-14 02:34:15 +02:00
|
|
|
// eslint-disable-next-line react-refresh/only-export-components
|
2025-12-14 02:29:46 +02:00
|
|
|
export { useWorkerPool };
|