Skip to content

Commit ccd1605

Browse files
brenelzclaude
andcommitted
fix(signals): exempt the thenable probe from the store strict-read check
Resolving a promise with a store proxy — refresh(store)'s waiter delivers the store, Promise.resolve(store), `return store` from an async function — makes the engine read `store.then` synchronously in the caller's scope. When that scope carries a strict-read label (an effect callback, a component body) the read produced a spurious STRICT_READ_UNTRACKED warning, and against a refetching derived store it could escalate to the PENDING_ASYNC_UNTRACKED_READ throw, rejecting the promise being resolved. `await refresh(list)` inside an action logged the warning on every call. The `then` probe is not a read the user wrote; it is now exempt from both. Regressions live in the store strict-read suite. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 439a2fe commit ccd1605

3 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
Stop the store proxy's dev strict-read check from firing on the engine's
6+
thenable probe. Resolving a promise with a store proxy — `refresh(store)`'s
7+
waiter delivers the store, `Promise.resolve(store)`, `return store` from an
8+
async function — makes the engine read `store.then` synchronously in the
9+
caller's scope. When that scope carries a strict-read label (an effect
10+
callback, a component body) the read produced a spurious
11+
`STRICT_READ_UNTRACKED` warning, and against a refetching derived store it
12+
could escalate to the `PENDING_ASYNC_UNTRACKED_READ` throw, rejecting the
13+
promise being resolved. `await refresh(list)` inside an action logged the
14+
warning on every call. The `then` probe is not a read the user wrote and is
15+
now exempt from both.

‎packages/signals/src/store/next/store.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,11 +1536,18 @@ const traps: ProxyHandler<StoreNextTarget> = {
15361536
}
15371537
// Dev strictRead: untracked store reads in labeled scopes (component
15381538
// bodies, effect callbacks) warn — the value can never update the reader.
1539+
// `then` is exempt: resolving a promise with a store proxy (refresh()'s
1540+
// waiter delivers the store, `Promise.resolve(store)`, `return store`
1541+
// from an async function) makes the engine probe `.then` for
1542+
// thenable-ness synchronously in the caller's scope. That is not a read
1543+
// the user wrote, and it must neither warn nor escalate to the pending
1544+
// throw — a throw out of promise resolution rejects the promise.
15391545
if (
15401546
__DEV__ &&
15411547
strictRead &&
15421548
!inDraft(target) &&
15431549
typeof key === "string" &&
1550+
key !== "then" &&
15441551
getObserver() === null
15451552
) {
15461553
// Safeguard parity with core read() (#2897): a component-body read of

‎packages/signals/tests/strict-read-pending-store.test.ts‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
createOptimisticStore,
55
createRoot,
66
createStore,
7+
DEV,
78
flush,
89
isPending,
910
refresh,
@@ -245,3 +246,115 @@ describe("uninitialized derived stores never leak the seed (#2897)", () => {
245246
warn.mockRestore();
246247
});
247248
});
249+
250+
/**
251+
* Resolving a promise with a store proxy makes the engine read `.then` to
252+
* decide whether the value is a thenable. The read goes through the store's
253+
* get trap synchronously, in whatever scope the resolution happens — so
254+
* refresh(store)'s waiter callback (an "effect callback" strict-read scope),
255+
* `Promise.resolve(store)` in a component body, or `return store` from an
256+
* async function all probe `.then` under a strict-read label. The probe is
257+
* not a read the user wrote: it must neither warn (STRICT_READ_UNTRACKED)
258+
* nor escalate to the pending throw (PENDING_ASYNC_UNTRACKED_READ), which
259+
* would reject the promise being resolved.
260+
*/
261+
describe("thenable probe on a store proxy in a strict-read scope", () => {
262+
it("await refresh(store) on a derived store emits no strict-read diagnostic", async () => {
263+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
264+
const capture = DEV!.diagnostics.capture();
265+
let fetches = 0;
266+
let dispose!: () => void;
267+
let list!: { id: number }[];
268+
createRoot(d => {
269+
dispose = d;
270+
[list] = createStore(
271+
async () => {
272+
fetches++;
273+
await wait(5);
274+
return [{ id: fetches }];
275+
},
276+
[] as { id: number }[]
277+
);
278+
});
279+
flush();
280+
await wait(20);
281+
flush();
282+
expect(fetches).toBe(1);
283+
284+
const delivered = await refresh(list);
285+
flush();
286+
287+
expect(delivered).toBe(list);
288+
expect(fetches).toBe(2);
289+
const events = capture.stop();
290+
expect(events.filter(e => e.code === "STRICT_READ_UNTRACKED")).toEqual([]);
291+
expect(events.filter(e => e.code === "PENDING_ASYNC_UNTRACKED_READ")).toEqual([]);
292+
expect(warn).not.toHaveBeenCalled();
293+
warn.mockRestore();
294+
dispose();
295+
});
296+
297+
it("Promise.resolve(store) in a component body neither warns nor rejects", async () => {
298+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
299+
const capture = DEV!.diagnostics.capture();
300+
let dispose!: () => void;
301+
let p!: Promise<unknown>;
302+
let control!: number;
303+
createRoot(d => {
304+
dispose = d;
305+
const [store] = createStore({ count: 1 });
306+
untrack(() => {
307+
p = Promise.resolve(store);
308+
// Control: a real untracked read still warns.
309+
control = store.count;
310+
}, "App");
311+
});
312+
await expect(p).resolves.toEqual({ count: 1 });
313+
expect(control).toBe(1);
314+
const events = capture.stop();
315+
const strict = events.filter(e => e.code === "STRICT_READ_UNTRACKED");
316+
expect(strict).toHaveLength(1);
317+
expect(strict[0].data?.property).toBe("count");
318+
expect(warn).toHaveBeenCalledTimes(1);
319+
warn.mockRestore();
320+
dispose();
321+
});
322+
323+
it("thenable probe on a refetching derived store does not throw the pending error", async () => {
324+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
325+
const capture = DEV!.diagnostics.capture();
326+
let dispose!: () => void;
327+
let list!: { id: number }[];
328+
let fetches = 0;
329+
createRoot(d => {
330+
dispose = d;
331+
[list] = createStore(
332+
async () => {
333+
fetches++;
334+
await wait(5);
335+
return [{ id: fetches }];
336+
},
337+
[] as { id: number }[]
338+
);
339+
});
340+
flush();
341+
await wait(20);
342+
flush();
343+
344+
// A component-body probe while the store is REFETCHING must still be a
345+
// no-op — the pending escalation would reject the promise.
346+
const first = refresh(list);
347+
let p!: Promise<unknown>;
348+
untrack(() => {
349+
p = Promise.resolve(list);
350+
}, "App");
351+
await expect(p).resolves.toBe(list);
352+
await first;
353+
flush();
354+
const events = capture.stop();
355+
expect(events.filter(e => e.code === "PENDING_ASYNC_UNTRACKED_READ")).toEqual([]);
356+
expect(events.filter(e => e.code === "STRICT_READ_UNTRACKED")).toEqual([]);
357+
warn.mockRestore();
358+
dispose();
359+
});
360+
});

0 commit comments

Comments
 (0)