From 42e97bb05a6370daf215a06e249911db75d4e319 Mon Sep 17 00:00:00 2001 From: Diego Imbert Date: Wed, 13 May 2026 16:00:37 +0200 Subject: [PATCH] refactor(frontend): seed per-workspace handles via UserDraft.use defaultValue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ensureHandle was doing a post-hoc `if (h.draft === undefined) h.draft = baseline`, which relies on the saveInitialValue=false skip to swallow that seeding write. Hand the baseline to `UserDraft.use({ defaultValue })` instead — useLocalStorageValue uses it as the initial $state value when localStorage is empty, so lastSerialized is correct out of the gate and no setter call is needed. --- frontend/src/lib/components/ResourceEditor.svelte | 13 ++++++------- frontend/src/lib/components/VariableEditor.svelte | 10 ++++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/frontend/src/lib/components/ResourceEditor.svelte b/frontend/src/lib/components/ResourceEditor.svelte index 39bf946f21..19e55138c2 100644 --- a/frontend/src/lib/components/ResourceEditor.svelte +++ b/frontend/src/lib/components/ResourceEditor.svelte @@ -62,18 +62,17 @@ for (const h of Object.values(states)) h.release() }) - /** Create (or reuse) a per-workspace handle, seeding it with `baseline` if - * no local autosave is already present. */ - function ensureHandle(ws: string, baseline: ResourceState): UserDraftHandle { + /** Create (or reuse) a per-workspace handle. `defaultValue` is what the + * handle reports when no autosave is persisted; an existing autosave + * always wins. The default itself never round-trips to localStorage — only + * the user's first real edit triggers a write. */ + function ensureHandle(ws: string, defaultValue: ResourceState): UserDraftHandle { if (states[ws]) return states[ws] const h = UserDraft.use('resource', initialPath ?? '', { workspace: ws, + defaultValue, manualRelease: true }) - // Existing autosave wins; only seed when there's nothing persisted yet. - // The seed itself doesn't persist (saveInitialValue=false) — only the - // user's first real edit triggers a write. - if (h.draft === undefined) h.draft = baseline states[ws] = h return h } diff --git a/frontend/src/lib/components/VariableEditor.svelte b/frontend/src/lib/components/VariableEditor.svelte index 6737752514..5d3a9b3f06 100644 --- a/frontend/src/lib/components/VariableEditor.svelte +++ b/frontend/src/lib/components/VariableEditor.svelte @@ -43,15 +43,17 @@ for (const h of Object.values(states)) h.release() }) - /** Create (or reuse) a per-workspace handle, seeding with `baseline` when - * no autosave is already persisted. */ - function ensureHandle(ws: string, baseline: VariableState): UserDraftHandle { + /** Create (or reuse) a per-workspace handle. `defaultValue` is what the + * handle reports when no autosave is persisted; an existing autosave + * always wins. The default itself never round-trips to localStorage — only + * the user's first real edit triggers a write. */ + function ensureHandle(ws: string, defaultValue: VariableState): UserDraftHandle { if (states[ws]) return states[ws] const h = UserDraft.use('variable', editPath ?? '', { workspace: ws, + defaultValue, manualRelease: true }) - if (h.draft === undefined) h.draft = baseline states[ws] = h return h }