Skip to content

Commit 3616d2a

Browse files
committed
fix(gui): defer client-resource store eviction across resubscribe churn
Keep cached data when React tears down and reattaches a single subscriber for pollMs or enabled changes; only delete the store on the next tick if it stays empty.
1 parent afc99ec commit 3616d2a

2 files changed

Lines changed: 124 additions & 6 deletions

File tree

‎gui/src/client-resource.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ function abortInflightOwnedBy<T>(store: Store<T>, owner: () => void): boolean {
147147
return true;
148148
}
149149

150+
/**
151+
* Drop the module cache only after a macrotask so React's subscribe teardown +
152+
* resubscribe (pollMs / enabled / key churn) can reattach in the same turn
153+
* without wiping cached data.
154+
*/
155+
function scheduleStoreEviction(key: string, store: Store<unknown>) {
156+
clearPollTimer(store);
157+
setTimeout(() => {
158+
if (store.subscriberCount !== 0) return;
159+
if (stores.get(key) !== store) return;
160+
store.inflight?.abort();
161+
store.inflight = null;
162+
store.inflightOwner = null;
163+
stores.delete(key);
164+
}, 0);
165+
}
166+
150167
function subscribeResource<T>(
151168
key: string,
152169
fetcher: (signal: AbortSignal) => Promise<T>,
@@ -159,7 +176,8 @@ function subscribeResource<T>(
159176
store.fetcherByListener.set(onStoreChange, fetcher);
160177
store.subscriberCount++;
161178

162-
if (store.subscriberCount === 1) {
179+
// Cold start only — keep cached data across transient 0→1 resubscribe gaps.
180+
if (store.subscriberCount === 1 && store.snapshot.data === undefined) {
163181
void runFetch(store, fetcher, { replaceInflight: true, owner: onStoreChange });
164182
}
165183
recomputePoll(store);
@@ -172,11 +190,7 @@ function subscribeResource<T>(
172190
// Drop this subscriber's in-flight work so a late resolve cannot stomp shared data.
173191
const abortedOwned = abortInflightOwnedBy(store, onStoreChange);
174192
if (store.subscriberCount === 0) {
175-
store.inflight?.abort();
176-
store.inflight = null;
177-
store.inflightOwner = null;
178-
clearPollTimer(store);
179-
stores.delete(key);
193+
scheduleStoreEviction(key, store);
180194
return;
181195
}
182196
// Replace aborted work immediately so the shared snapshot cannot stay stuck loading.

‎gui/tests/client-resource-poll.test.tsx‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,107 @@ test("keyed deps changes force loading while retaining previous data until refet
316316
});
317317
container.remove();
318318
});
319+
320+
test("changing pollMs keeps cached data without a cold refetch", async () => {
321+
const { createRoot } = await import("react-dom/client");
322+
const container = document.createElement("div");
323+
document.body.append(container);
324+
325+
const KEY = `pollms-churn-${Date.now()}`;
326+
let fetches = 0;
327+
type Snap = { data: string | undefined; loading: boolean };
328+
let snap: Snap = { data: undefined, loading: false };
329+
let setPollMs!: (value: number) => void;
330+
331+
function Page() {
332+
const [pollMs, setPollMsState] = useState(40);
333+
setPollMs = setPollMsState;
334+
const resource = useClientResource(
335+
KEY,
336+
async () => {
337+
fetches += 1;
338+
return "cached";
339+
},
340+
{ pollMs },
341+
);
342+
useEffect(() => {
343+
snap = { data: resource.data, loading: resource.loading };
344+
}, [resource.data, resource.loading]);
345+
return <span />;
346+
}
347+
348+
let root!: Root;
349+
await act(async () => {
350+
root = createRoot(container);
351+
root.render(<Page />);
352+
});
353+
354+
await waitFor(() => snap.data === "cached" && snap.loading === false);
355+
expect(fetches).toBe(1);
356+
357+
await act(async () => {
358+
setPollMs(80);
359+
});
360+
361+
// Subscribe identity churn must not wipe the store or force a cold fetch.
362+
await act(async () => {
363+
await new Promise<void>((resolve) => testWindow.setTimeout(resolve, 20));
364+
});
365+
expect(snap).toEqual({ data: "cached", loading: false });
366+
expect(fetches).toBe(1);
367+
368+
await act(async () => {
369+
root.unmount();
370+
});
371+
container.remove();
372+
});
373+
374+
test("store is evicted after the last subscriber leaves", async () => {
375+
const { createRoot } = await import("react-dom/client");
376+
const container = document.createElement("div");
377+
document.body.append(container);
378+
379+
const KEY = `store-evict-${Date.now()}`;
380+
let fetches = 0;
381+
type Snap = { data: string | undefined };
382+
let snap: Snap = { data: undefined };
383+
384+
function Page() {
385+
const resource = useClientResource(KEY, async () => {
386+
fetches += 1;
387+
return `v${fetches}`;
388+
});
389+
useEffect(() => {
390+
snap = { data: resource.data };
391+
}, [resource.data]);
392+
return <span />;
393+
}
394+
395+
let root!: Root;
396+
await act(async () => {
397+
root = createRoot(container);
398+
root.render(<Page />);
399+
});
400+
await waitFor(() => snap.data === "v1");
401+
expect(fetches).toBe(1);
402+
403+
await act(async () => {
404+
root.unmount();
405+
});
406+
// Flush deferred eviction.
407+
await act(async () => {
408+
await new Promise<void>((resolve) => testWindow.setTimeout(resolve, 0));
409+
});
410+
411+
await act(async () => {
412+
root = createRoot(container);
413+
root.render(<Page />);
414+
});
415+
await waitFor(() => snap.data === "v2");
416+
expect(fetches).toBe(2);
417+
418+
await act(async () => {
419+
root.unmount();
420+
});
421+
container.remove();
422+
});

0 commit comments

Comments
 (0)