forge
Explore
Log in

Don't you dare go Hollow

📝 local-signal pattern for live save-on-click toggles

Browse files at this commit

494ba281c97ee468655cc75c7c0de18221ade7b5Ahmet Kaan GümüşParent: c7f0472
  • docs
    • 📝 local-signal pattern for live save-on-click toggles
Show full message
docs: 📝 local-signal pattern for live save-on-click toggles

Companion to the resource.latest rule (#6): the mutation side. An inline
switch that PATCHes one field must not refetch() the shared resource
(re-suspends → page spinner) nor mutate({...res, field}) it — mutating a
shared resource changes its identity, so every other createResource whose
source reads it re-runs and refetches, cascading the same spinner. Use a
local signal synced via createEffect, flipped optimistically, reverted on
failure. One PATCH, zero GETs, no success toast. Documented after the exam
finished-review toggle hit this.
AGENTS.mdmodified+26 -0
@@ -84,6 +84,32 @@ Solid components run **once**, there is no re-render:
84 `<Suspense>` use `resource()`; always-rendered/shell/badge reads of a84 `<Suspense>` use `resource()`; always-rendered/shell/badge reads of a
85 periodically-refetched resource use `resource.latest` (last value, no85 periodically-refetched resource use `resource.latest` (last value, no
86 suspend). Poll-driven revalidation belongs behind `.latest`.86 suspend). Poll-driven revalidation belongs behind `.latest`.
87+7. **Live save-on-click toggles use a local signal, not `mutate`/`refetch`.**
88+ For an inline switch that PATCHes one field (e.g. the exam-review toggle in
89+ `exam-detail-page.tsx`), do NOT `refetch()` the shared resource (re-suspends
90+ → page spinner) and do NOT `mutate({...res, field})` either — mutating a
91+ shared resource changes its object identity, so every *other* `createResource`
92+ whose source reads it (directly or via a memo like `hasCourseManagementRights()`)
93+ re-runs and refetches, cascading the same spinner. Instead keep a local
94+ signal, sync it from the resource in a `createEffect`, flip it optimistically
95+ on toggle, and revert on failure:
96+ ```ts
97+ const [on, setOn] = createSignal(false);
98+ createEffect(() => { const e = res(); if (e) setOn(e.field); });
99+ const toggle = async (next: boolean) => {
100+ if (saving()) return;
101+ setOn(next); setSaving(true);
102+ try { await patchX(id(), { field: next }); }
103+ catch (err) { setOn(!next); setError(fmt(err)); }
104+ finally { setSaving(false); }
105+ };
106+ ```
107+ A correct toggle fires exactly one PATCH and zero GETs. No success toast — the
108+ switch position is the feedback. Rule #6's `resource.latest` is the read-side
109+ companion (survive a refetch without blanking); this is the write-side (don't
110+ trigger the refetch at all). `mutate()` (`settings-page.tsx`) is fine for a
111+ form that owns its whole resource, but still cascades for a shared one — the
112+ local signal is the safe default.
87 113
88 ## Performance114 ## Performance
89 115