refactor(frontend): seed per-workspace handles via UserDraft.use defaultValue

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.
This commit is contained in:
Diego Imbert
2026-05-13 16:00:37 +02:00
parent 3301d81ed6
commit 42e97bb05a
2 changed files with 12 additions and 11 deletions
@@ -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<ResourceState> {
/** 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<ResourceState> {
if (states[ws]) return states[ws]
const h = UserDraft.use<ResourceState>('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
}
@@ -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<VariableState> {
/** 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<VariableState> {
if (states[ws]) return states[ws]
const h = UserDraft.use<VariableState>('variable', editPath ?? '', {
workspace: ws,
defaultValue,
manualRelease: true
})
if (h.draft === undefined) h.draft = baseline
states[ws] = h
return h
}