diff --git a/frontend/src/lib/components/pierre/PierreFileEditor.svelte b/frontend/src/lib/components/pierre/PierreFileEditor.svelte index 510dd1f0ab..dbfc0754b6 100644 --- a/frontend/src/lib/components/pierre/PierreFileEditor.svelte +++ b/frontend/src/lib/components/pierre/PierreFileEditor.svelte @@ -24,6 +24,8 @@ editorOptions?: Omit selectionAction?: Snippet<[SelectionActionContext]> markers?: Marker[] + /** A later editor with the same key resumes this one's draft and undo history. */ + editStateKey?: string onChange?: (code: string) => void onEditor?: (editor: Editor<'file', undefined, undefined> | undefined) => void class?: string @@ -38,6 +40,7 @@ editorOptions, selectionAction, markers, + editStateKey, onChange, onEditor, class: className = '' @@ -100,7 +103,8 @@ renderSelectionAction: selectionAction ? (ctx) => elements.render(selectionAction, ctx) : undefined - })) + })), + untrack(() => editStateKey) ) dispose = e.edit(f) editor = e diff --git a/frontend/src/lib/components/pierre/pierre.ts b/frontend/src/lib/components/pierre/pierre.ts index 733f7d5361..67d2ca12fa 100644 --- a/frontend/src/lib/components/pierre/pierre.ts +++ b/frontend/src/lib/components/pierre/pierre.ts @@ -39,7 +39,11 @@ export async function switchMainThreadHighlighter(type: HighlighterTypes): Promi const pools = new Map() -/** One pool per highlighter backend: a pool's backend is fixed when its workers start. */ +/** + * One pool per highlighter backend: a pool's backend is fixed when its workers start. Pools live + * as long as the page, shared by every component that asks; a caller that wants its workers back + * must `terminate()` the pool and accept that later components restart it. + */ export function pierreWorkerPool(preferredHighlighter: HighlighterTypes): WorkerPoolManager { let pool = pools.get(preferredHighlighter) if (!pool) { diff --git a/frontend/src/lib/components/pierre/snippetElements.ts b/frontend/src/lib/components/pierre/snippetElements.ts index d5922d381b..beb13e3bc5 100644 --- a/frontend/src/lib/components/pierre/snippetElements.ts +++ b/frontend/src/lib/components/pierre/snippetElements.ts @@ -1,21 +1,22 @@ import { mount, unmount, type Snippet } from 'svelte' import SnippetHost from './SnippetHost.svelte' -type Entry = { target: HTMLElement; component?: Record; seen: boolean; frames: number } +type Entry = { target: HTMLElement; component?: Record; seen: boolean; sweeps: number } // Pierre may build an element and drop it in the same pass without inserting it. -const MAX_UNSEEN_FRAMES = 10 +const MAX_UNSEEN_SWEEPS = 10 +const SWEEP_MS = 500 /** * Pierre's render callbacks return detached DOM nodes and later remove them without telling - * the caller. This mounts a Svelte snippet into each one and unmounts it once its node has - * left the document, checked on the frame after a render so a node Pierre detaches and - * reinserts within one render is kept. + * the caller (a re-render with fewer annotations, a selection popover closing). This mounts a + * Svelte snippet into each one and unmounts it once its node has left the document. Nodes are + * checked on a timer rather than at render, so one Pierre detaches and reinserts within a + * render is kept, and one removed with no render following is still released. */ export function snippetElements() { const entries = new Set() - let destroyed = false - let sweepScheduled = false + let timer: ReturnType | undefined function drop(entry: Entry) { if (entry.component) unmount(entry.component) @@ -23,42 +24,33 @@ export function snippetElements() { } function sweep() { - sweepScheduled = false - if (destroyed) return - let pending = false for (const entry of entries) { - if (entry.target.isConnected) { - entry.seen = true - } else if (entry.seen || ++entry.frames > MAX_UNSEEN_FRAMES) { - drop(entry) - } else { - pending = true - } + if (entry.target.isConnected) entry.seen = true + else if (entry.seen || ++entry.sweeps > MAX_UNSEEN_SWEEPS) drop(entry) } - if (pending) scheduleSweep() + if (entries.size === 0) stop() } - function scheduleSweep() { - if (sweepScheduled) return - sweepScheduled = true - requestAnimationFrame(sweep) + function stop() { + clearInterval(timer) + timer = undefined } return { render(snippet: Snippet<[T]>, arg: T, tag: 'div' | 'span' = 'div'): HTMLElement { - const entry: Entry = { target: document.createElement(tag), seen: false, frames: 0 } + const entry: Entry = { target: document.createElement(tag), seen: false, sweeps: 0 } entries.add(entry) + timer ??= setInterval(sweep, SWEEP_MS) // Pierre re-renders synchronously when an edit session ends, which can be inside an // effect teardown, where Svelte refuses to create the mounted component's effects. queueMicrotask(() => { - if (destroyed || !entries.has(entry)) return + if (!entries.has(entry)) return entry.component = mount(SnippetHost, { target: entry.target, props: { snippet, arg } }) }) - scheduleSweep() return entry.target }, destroy() { - destroyed = true + stop() for (const entry of entries) drop(entry) } } diff --git a/frontend/src/routes/(styled)/test_dev/diff-comparison/Benchmark.svelte b/frontend/src/routes/(styled)/test_dev/diff-comparison/Benchmark.svelte index eadbe9fbdf..3a17b80744 100644 --- a/frontend/src/routes/(styled)/test_dev/diff-comparison/Benchmark.svelte +++ b/frontend/src/routes/(styled)/test_dev/diff-comparison/Benchmark.svelte @@ -1,5 +1,5 @@
@@ -104,7 +104,7 @@ class="h-full overflow-auto" items={[{ type: 'diff', id: activeCase.id, fileDiff: caseFileDiff(activeCase) }]} {options} - workerPool={pool} + workerPool={pool.current} /> {:else} {/if} {/snippet} diff --git a/frontend/src/routes/(styled)/test_dev/diff-comparison/EditorTab.svelte b/frontend/src/routes/(styled)/test_dev/diff-comparison/EditorTab.svelte index 89841dc914..e9f728b86a 100644 --- a/frontend/src/routes/(styled)/test_dev/diff-comparison/EditorTab.svelte +++ b/frontend/src/routes/(styled)/test_dev/diff-comparison/EditorTab.svelte @@ -10,10 +10,14 @@ import { loadCopilot } from '$lib/components/copilot/loadCopilot' import { copilotInfo } from '$lib/aiStore' import { workspaceStore } from '$lib/stores' + import { randomUUID } from '$lib/utils/uuid' import { Redo2, Undo2 } from 'lucide-svelte' import { lint } from './lint' import { REVIEW_BEFORE } from './reviewSample' + // Changing the prediction mode recreates the editor; the key carries the draft and undo + // history over. Per tab instance, so reopening the tab starts from the sample again. + const editStateKey = randomUUID() let pierreCode = $state(REVIEW_BEFORE) let monacoCode = $state(REVIEW_BEFORE) let markers: Marker[] = $state.raw(lint(REVIEW_BEFORE)) @@ -117,6 +121,7 @@ {#key predictionMode} settings) async function show(side: Side) { stats[side] = undefined @@ -114,7 +114,7 @@ class="h-full overflow-auto" {items} options={diffOptions(settings)} - workerPool={pool} + workerPool={pool.current} /> {/await} {/key} diff --git a/frontend/src/routes/(styled)/test_dev/diff-comparison/PierreSettingsBar.svelte b/frontend/src/routes/(styled)/test_dev/diff-comparison/PierreSettingsBar.svelte index 4de28a22cb..313e403508 100644 --- a/frontend/src/routes/(styled)/test_dev/diff-comparison/PierreSettingsBar.svelte +++ b/frontend/src/routes/(styled)/test_dev/diff-comparison/PierreSettingsBar.svelte @@ -2,7 +2,7 @@ import Select from '$lib/components/select/Select.svelte' import Toggle from '$lib/components/Toggle.svelte' import { canUseWASM } from '@pierre/diffs' - import { THEMES, type PierreSettings } from './pierreSettings' + import { THEMES, type PierreSettings } from './pierreSettings.svelte' let { settings = $bindable() }: { settings: PierreSettings } = $props() diff --git a/frontend/src/routes/(styled)/test_dev/diff-comparison/pierreSettings.ts b/frontend/src/routes/(styled)/test_dev/diff-comparison/pierreSettings.svelte.ts similarity index 61% rename from frontend/src/routes/(styled)/test_dev/diff-comparison/pierreSettings.ts rename to frontend/src/routes/(styled)/test_dev/diff-comparison/pierreSettings.svelte.ts index 5eea0a4ba3..ea0afd81f8 100644 --- a/frontend/src/routes/(styled)/test_dev/diff-comparison/pierreSettings.ts +++ b/frontend/src/routes/(styled)/test_dev/diff-comparison/pierreSettings.svelte.ts @@ -56,10 +56,33 @@ export function diffOptions( } } -/** Theme and inline-diff granularity belong to the pool when one is used, not to the component. */ -export function settingsPool(s: PierreSettings): WorkerPoolManager | undefined { - if (!s.useWorkers) return undefined - const pool = pierreWorkerPool(s.highlighter) - void pool.setRenderOptions({ theme: THEMES[s.theme], lineDiffType: s.lineDiffType }) - return pool +// Pools are shared across components, so the options last applied are tracked per pool. +const appliedOptions = new WeakMap() + +/** + * The pool for these settings, with its render options kept in sync: theme and inline-diff + * granularity belong to the pool when one is used, not to the component. Call during component + * init; the getter is safe in `$derived` and templates. + */ +export function useSettingsPool(settings: () => PierreSettings): { + readonly current: WorkerPoolManager | undefined +} { + const pool = $derived( + settings().useWorkers ? pierreWorkerPool(settings().highlighter) : undefined + ) + $effect(() => { + const s = settings() + if (!pool) return + const key = `${s.theme}:${s.lineDiffType}` + // Setting options re-renders every mounted component and clears the pool's cache. + // `pierreWorkerPool` starts a pool on the pierre theme and Pierre's default `word-alt`. + if ((appliedOptions.get(pool) ?? 'pierre:word-alt') === key) return + appliedOptions.set(pool, key) + void pool.setRenderOptions({ theme: THEMES[s.theme], lineDiffType: s.lineDiffType }) + }) + return { + get current() { + return pool + } + } }