(
files: { path: string, content: string }[],
onProgressTracker?: (progressMsg: string, percentage: number, diagnosticLog?: string) => void,
options: {
indexVariables?: boolean;
maxNodes?: number;
maxEdges?: number;
} = { indexVariables: true }
)
| 5 | * Ensures the main thread stays highly responsive even for 1000+ files. |
| 6 | */ |
| 7 | export function parseFilesIntoGraph( |
| 8 | files: { path: string, content: string }[], |
| 9 | onProgressTracker?: (progressMsg: string, percentage: number, diagnosticLog?: string) => void, |
| 10 | options: { |
| 11 | indexVariables?: boolean; |
| 12 | maxNodes?: number; |
| 13 | maxEdges?: number; |
| 14 | } = { indexVariables: true } |
| 15 | ): Promise<{ nodes: any[], links: any[], files: string[] }> { |
| 16 | return new Promise((resolve, reject) => { |
| 17 | // Vite built-in worker support |
| 18 | const worker = new Worker(new URL('./parser.worker.ts', import.meta.url), { type: 'module' }); |
| 19 | |
| 20 | worker.onmessage = (e) => { |
| 21 | const { type, payload } = e.data; |
| 22 | if (type === 'PROGRESS') { |
| 23 | if (onProgressTracker) onProgressTracker(payload.msg, payload.percent); |
| 24 | } else if (type === 'DIAGNOSTIC_LOG') { |
| 25 | if (onProgressTracker) onProgressTracker("", -1, payload); |
| 26 | } else if (type === 'DONE') { |
| 27 | resolve(payload); |
| 28 | worker.terminate(); |
| 29 | } else if (type === 'ERROR') { |
| 30 | reject(new Error(payload)); |
| 31 | worker.terminate(); |
| 32 | } |
| 33 | }; |
| 34 | |
| 35 | worker.onerror = (e) => { |
| 36 | reject(e); |
| 37 | worker.terminate(); |
| 38 | }; |
| 39 | |
| 40 | // The main thread sends groups of messages to the worker |
| 41 | const CHUNK_SIZE = 50; |
| 42 | for (let i = 0; i < files.length; i += CHUNK_SIZE) { |
| 43 | const chunk = files.slice(i, i + CHUNK_SIZE); |
| 44 | worker.postMessage({ type: 'ADD_FILES', files: chunk }); |
| 45 | } |
| 46 | |
| 47 | worker.postMessage({ type: 'START', options }); |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | // UPLOADER UTILITIES |
no test coverage detected