Skip to content

Commit 37ac480

Browse files
committed
fix: address review findings
The projection computed retires STATUS_UNINITIALIZED after a synchronous landing only when the pass is mainline or its transaction is parked by nothing but the node's own flight, so a landing staged by a live action or beside another pending source keeps the seed invisible until that transaction commits. The gate is keyed on the node's own pending-source entry instead of any STATUS_PENDING, so a first run parked on an upstream async source no longer trips it. The tests assert in the flush that lands the sync value, register a length reader, and pin the transaction-held case.
1 parent c79f503 commit 37ac480

3 files changed

Lines changed: 60 additions & 13 deletions

File tree

‎.changeset/derived-store-sync-landing-wakes-readers.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@solidjs/signals": patch
33
---
44

5-
A derived store whose first flight is superseded by a synchronous landing now wakes every reader parked on that flight (#3726). When the derive returned a pending promise and a later source write made it return a value synchronously, the projection computed kept `STATUS_UNINITIALIZED` until the flush commit, so the recompute-side settle walk (#3181) that requires initialization skipped it. Readers whose store node the landing left unchanged (`"length" in store` on an array seed, `Object.keys`, an unchanged `length`) had no value notification to fall back on and stayed pending, blank inside a `<Loading>` boundary while a sibling read of the source updated. The sync commit through the setter now retires the flag the way the async landing does, so the walk releases those readers in the same flush.
5+
A derived store whose pending first flight is superseded by a synchronous landing now wakes the readers parked on that flight in the same flush, including readers whose store node the landing left unchanged (`"length" in store`, `Object.keys`). A landing staged by a transaction keeps the seed invisible until that transaction commits.

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
computed,
1919
CONFIG_AUTO_DISPOSE,
2020
getOwner,
21+
GlobalQueue,
2122
handleAsync,
2223
isDisposed,
2324
STATUS_PENDING,
@@ -27,6 +28,7 @@ import {
2728
} from "../../core/index.js";
2829

2930
import {
31+
activeTransition,
3032
projectionWriteActive,
3133
scheduleWithheld,
3234
setProjectionWriteActive
@@ -240,6 +242,16 @@ function cloneState<T extends object>(v: T, shallow: boolean): T {
240242
return shallow ? (Array.isArray(v) ? (v.slice() as T) : { ...v }) : JSON.parse(JSON.stringify(v));
241243
}
242244

245+
// Held past this flush: the transaction is parked by more than the node's own flight.
246+
function landingHeld(owner: Computed<any>): boolean {
247+
const t = activeTransition;
248+
if (t === null) return false;
249+
if (t._actions.length || GlobalQueue._transitionBlocked?.(t)) return true;
250+
for (const source of t._asyncReporters.keys())
251+
if (source !== owner && source._x?._pendingSources?.size) return true;
252+
return false;
253+
}
254+
243255
export function runProjectionComputedNext<T extends object>(
244256
wrappedStore: Store<T>,
245257
fn: (draft: T) => void | T | Promise<void | T> | AsyncIterable<void | T>,
@@ -304,8 +316,8 @@ export function runProjectionComputedNext<T extends object>(
304316
};
305317
const sync = handleAsync(owner, result, commit);
306318
if (!owner._loading) commit(sync as void | T);
307-
// A sync landing over a pending flight initializes as asyncWrite's does (#3181 walk).
308-
if (owner._x?._inFlight == null && owner._statusFlags & STATUS_PENDING)
319+
// Recompute's settle walk for a superseded flight skips an uninitialized node.
320+
if (owner._x?._pendingSources?.has(owner) && !landingHeld(owner))
309321
owner._statusFlags &= ~STATUS_UNINITIALIZED;
310322
},
311323
false

‎packages/signals/tests/store/derived-presence-async-3726.test.ts‎

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { describe, expect, it } from "vitest";
22
import {
3+
NotReadyError,
4+
action,
35
createLoadingBoundary,
46
createRenderEffect,
57
createRoot,
68
createSignal,
79
createStore,
8-
flush
10+
flush,
11+
untrack,
12+
type Store
913
} from "../../src/index.js";
1014

1115
const tick = () => new Promise(r => setTimeout(r, 0));
@@ -45,29 +49,37 @@ describe("sync landing after a pending first flight wakes unchanged-node readers
4549
expect(sourceState).toBe("pending");
4650

4751
setSource([1]);
48-
resolve([1]);
4952
flush();
53+
expect(sourceState).toBe("resolved");
54+
expect(presence).toBe("present");
55+
56+
resolve([2, 2]);
5057
await tick();
5158
flush();
52-
expect(sourceState).toBe("resolved");
5359
expect(presence).toBe("present");
5460
});
5561

5662
it("readers of presence, length and keys wake when the landing leaves them unchanged", () => {
57-
const [source, setSource] = createSignal<{ a?: number } | null>(null);
63+
const [source, setSource] = createSignal<number[] | null>(null);
5864
const seen: string[] = [];
5965
createRoot(() => {
60-
const [store] = createStore<{ a?: number }>(() => {
66+
const [store] = createStore<number[]>(() => {
6167
const s = source();
6268
if (s) return s;
63-
return new Promise<{ a?: number }>(() => {});
64-
}, {});
69+
return new Promise<number[]>(() => {});
70+
}, []);
6571
createRenderEffect(
66-
() => ("a" in store ? "present" : "missing"),
72+
() => (0 in store ? "present" : "missing"),
6773
v => {
6874
seen.push(`in:${v}`);
6975
}
7076
);
77+
createRenderEffect(
78+
() => store.length,
79+
v => {
80+
seen.push(`length:${v}`);
81+
}
82+
);
7183
createRenderEffect(
7284
() => Object.keys(store).length,
7385
v => {
@@ -78,8 +90,31 @@ describe("sync landing after a pending first flight wakes unchanged-node readers
7890
flush();
7991
expect(seen).toEqual([]);
8092

81-
setSource({});
93+
setSource([]);
94+
flush();
95+
expect(seen.sort()).toEqual(["in:missing", "keys:0", "length:0"]);
96+
});
97+
98+
it("a landing held by a transaction keeps the seed invisible until the commit", async () => {
99+
const [source, setSource] = createSignal<number[] | null>(null);
100+
let release!: () => void;
101+
let store!: Store<number[]>;
102+
createRoot(() => {
103+
[store] = createStore<number[]>(() => source() ?? new Promise(() => {}), [9, 9, 9]);
104+
});
105+
flush();
106+
107+
const done = action(function* () {
108+
setSource([1]);
109+
yield new Promise<void>(r => (release = r));
110+
})();
111+
flush();
112+
expect(() => untrack(() => [...store])).toThrow(NotReadyError);
113+
114+
release();
115+
await done;
116+
await tick();
82117
flush();
83-
expect(seen.sort()).toEqual(["in:missing", "keys:0"]);
118+
expect(untrack(() => [...store])).toEqual([1]);
84119
});
85120
});

0 commit comments

Comments
 (0)