Skip to content

Commit 0102a57

Browse files
brenelzclaude
andcommitted
fix(signals): an optimistic store's first flight declares no transaction
The flight-owned transaction (#3146) was declared for the uninitialized first ask too, so every transition-riding consumer — render()'s scheduled root insert included — was held until the initial fetch landed: the page stayed blank (content outside the boundary included) and the Loading boundary's fallback never showed, while createStore(fn, seed) and createOptimistic(fn, seed) in the same spot showed it. Nothing has committed on a first flight, so there is no truth to keep on screen and no optimistic state to protect: it now declares nothing, like the loading window (#2933). Refetch flights declare exactly as before, so bare optimistic writes during an in-flight refetch still ride the flight's transaction (#2951). Regressions in the #3146 suite (a user effect is what reproduces the hold) and in the web Loading spec through render(). Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 439a2fe commit 0102a57

4 files changed

Lines changed: 236 additions & 2 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
An optimistic store's first flight suspends into its Loading boundary again.
6+
The flight-owned transaction (#3146) was declared for the uninitialized
7+
first ask too, so every transition-riding consumer — `render()`'s scheduled
8+
root insert included — was held until the initial fetch landed: the page
9+
stayed blank (content outside the boundary included) and the boundary's
10+
fallback never showed, while `createStore(fn, seed)` and
11+
`createOptimistic(fn, seed)` in the same spot showed it. Nothing has
12+
committed on a first flight, so there is no truth to keep on screen and no
13+
optimistic state to protect: it now declares nothing, like the loading
14+
window (#2933). Refetch flights declare exactly as before, so bare
15+
optimistic writes during an in-flight refetch still ride the flight's
16+
transaction (#2951) and content stays put until the new truth lands.

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
CONFIG_OPTIMISTIC,
2828
NOT_PENDING,
2929
STATUS_PENDING,
30+
STATUS_UNINITIALIZED,
3031
unwrapOverride
3132
} from "../../core/constants.js";
3233
import {
@@ -277,7 +278,16 @@ export function createOptimisticStoreNext<T extends object = {}>(
277278
if (!self._loading) fam.ft = null;
278279
return;
279280
}
280-
if (self._loading) return;
281+
// First flight (#3146 carve-out): nothing has ever committed, so there
282+
// is no truth to keep on screen and no optimistic state to protect. An
283+
// uninitialized ask suspends its readers into their Loading boundary
284+
// exactly like a plain derived store's first flight — declaring a
285+
// transaction here instead held the ROOT MOUNT (render()'s scheduled
286+
// insert rides transitions) until the fetch landed, so the boundary's
287+
// fallback never showed and the whole page stayed blank. The loading
288+
// window (#2933) already declares nothing for the same reason; once
289+
// the first truth lands, every refetch flight declares as before.
290+
if (self._loading || self._statusFlags & STATUS_UNINITIALIZED) return;
281291
let txn = activeTransition;
282292
if (txn === null) globalQueue.initTransition((txn = createTransition()));
283293
fam.ft = txn;

‎packages/signals/tests/store/flight-owned-transaction.test.ts‎

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
import { expect, test } from "vitest";
1414
import {
1515
createEffect,
16+
createLoadingBoundary,
1617
createOptimisticStore,
1718
createRoot,
1819
createSignal,
19-
flush
20+
createStore,
21+
flush,
22+
isPending
2023
} from "../../src/index.js";
2124
import type { Store } from "../../src/store.js";
2225

@@ -138,3 +141,113 @@ test("#3146: per-yield landings still reveal on arrival under the flight-owned t
138141
dispose();
139142
flush();
140143
});
144+
145+
// The FIRST flight is the carve-out: nothing has committed yet, so there is
146+
// no truth to keep on screen and no optimistic state to protect. Declaring
147+
// the owned transaction for the uninitialized ask held every
148+
// transition-riding consumer — render()'s scheduled root insert included —
149+
// until the fetch landed: the page stayed blank and the Loading boundary's
150+
// fallback never showed, while createStore(fn, seed) and
151+
// createOptimistic(fn, seed) in the same spot showed it. A first flight
152+
// declares nothing, like the loading window (#2933); refetch flights
153+
// (initialized) declare exactly as the tests above pin.
154+
type Resolver = (items: string[]) => void;
155+
156+
function boundaryHarness(make: (dep: () => number, resolvers: Resolver[]) => { items: string[] }) {
157+
const resolvers: Resolver[] = [];
158+
let setDep!: (v: number) => void;
159+
let result: unknown = "never-ran";
160+
let store!: { items: string[] };
161+
let dispose!: () => void;
162+
createRoot(d => {
163+
dispose = d;
164+
const [dep, _setDep] = createSignal(0);
165+
setDep = _setDep;
166+
store = make(dep, resolvers);
167+
const boundary = createLoadingBoundary(
168+
() => store.items.join(","),
169+
() => "loading"
170+
);
171+
// A USER effect, like render()'s scheduled root insert: its apply is
172+
// stashed by an open transition until that transition settles.
173+
createEffect(
174+
() => boundary(),
175+
v => {
176+
result = v;
177+
}
178+
);
179+
});
180+
return {
181+
resolvers,
182+
setDep,
183+
get result() {
184+
return result;
185+
},
186+
get store() {
187+
return store;
188+
},
189+
dispose
190+
};
191+
}
192+
193+
test("#3146 carve-out: the first flight shows the boundary fallback; the refetch keeps content", async () => {
194+
const h = boundaryHarness((dep, resolvers) => {
195+
const [store] = createOptimisticStore<{ items: string[] }>(
196+
async () => {
197+
dep();
198+
const items = await new Promise<string[]>(r => resolvers.push(r));
199+
return { items };
200+
},
201+
{ items: [] }
202+
);
203+
return store;
204+
});
205+
flush();
206+
// The boundary's effect must APPLY on the initial flush (not be stashed
207+
// by a flight transaction) and show the fallback.
208+
expect(h.result).toBe("loading");
209+
210+
h.resolvers[0](["A"]);
211+
await tick();
212+
flush();
213+
expect(h.result).toBe("A");
214+
215+
// Refetch: stale-while-revalidate — content stays, isPending flips.
216+
h.setDep(1);
217+
flush();
218+
await tick();
219+
expect(h.result).toBe("A");
220+
expect(isPending(() => h.store.items.length)).toBe(true);
221+
222+
h.resolvers[1](["A", "B"]);
223+
await tick();
224+
flush();
225+
await tick();
226+
flush();
227+
expect(h.result).toBe("A,B");
228+
expect(isPending(() => h.store.items.length)).toBe(false);
229+
h.dispose();
230+
flush();
231+
});
232+
233+
test("#3146 carve-out control: createStore(fn, seed) first flight shows the same fallback", async () => {
234+
const h = boundaryHarness((dep, resolvers) => {
235+
const [store] = createStore<{ items: string[] }>(
236+
async () => {
237+
dep();
238+
const items = await new Promise<string[]>(r => resolvers.push(r));
239+
return { items };
240+
},
241+
{ items: [] }
242+
);
243+
return store;
244+
});
245+
flush();
246+
expect(h.result).toBe("loading");
247+
h.resolvers[0](["A"]);
248+
await tick();
249+
flush();
250+
expect(h.result).toBe("A");
251+
h.dispose();
252+
flush();
253+
});

‎packages/web/test/loading.spec.tsx‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ import { describe, expect, test, beforeEach, afterEach, vi } from "vitest";
77
import {
88
lazy,
99
type Component,
10+
createOptimisticStore,
1011
createSignal,
1112
createMemo,
13+
createStore,
14+
For,
1215
Loading,
1316
Errored,
1417
Show,
@@ -825,3 +828,95 @@ describe("Testing Loading", () => {
825828
disposer();
826829
});
827830
});
831+
832+
// An optimistic store's first flight must suspend into the nearest <Loading>
833+
// like createStore(fn, seed) does. Its flight-owned transaction (#3146),
834+
// declared for the uninitialized first ask too, held render()'s scheduled
835+
// root insert until the fetch landed: the page stayed blank — not even
836+
// content OUTSIDE the boundary mounted — and the fallback never showed.
837+
describe("<Loading> around an optimistic store's first flight", () => {
838+
// Real timers: the fetches below settle on their own schedule.
839+
beforeEach(() => {
840+
vi.useRealTimers();
841+
});
842+
const wait = (ms: number) => new Promise<void>(r => setTimeout(r, ms));
843+
844+
type Item = { id: number; label: string; votes: number };
845+
function poll(make: typeof createStore | typeof createOptimisticStore) {
846+
const [tick, setTick] = createSignal(0);
847+
let fetches = 0;
848+
const [list] = make<Item[]>(async () => {
849+
tick();
850+
const n = ++fetches;
851+
await wait(10);
852+
return [{ id: 1, label: "Tacos", votes: n - 1 }];
853+
}, [] as Item[]);
854+
return { list, setTick };
855+
}
856+
857+
function mount(make: typeof createStore | typeof createOptimisticStore) {
858+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
859+
const div = document.createElement("div");
860+
let setTick!: (v: number) => void;
861+
const dispose = render(() => {
862+
const p = poll(make);
863+
setTick = p.setTick;
864+
return (
865+
<>
866+
<h1>Poll</h1>
867+
<Loading fallback={<div>Loading...</div>}>
868+
<ul>
869+
<For each={p.list}>
870+
{item => (
871+
<li>
872+
{item.label} - {item.votes}
873+
</li>
874+
)}
875+
</For>
876+
</ul>
877+
</Loading>
878+
</>
879+
);
880+
}, div);
881+
flush();
882+
// Comment markers from insert() are noise for these assertions.
883+
const html = () => div.innerHTML.replace(/<!---->/g, "");
884+
return {
885+
html,
886+
refetch: () => setTick(1),
887+
dispose: () => {
888+
dispose();
889+
warn.mockRestore();
890+
}
891+
};
892+
}
893+
894+
test("createOptimisticStore(fn, seed) in the component body: fallback at mount, list after landing, content kept on refetch", async () => {
895+
const { html, dispose, refetch } = mount(createOptimisticStore);
896+
expect(html()).toBe("<h1>Poll</h1><div>Loading...</div>");
897+
898+
await wait(30);
899+
flush();
900+
expect(html()).toBe("<h1>Poll</h1><ul><li>Tacos - 0</li></ul>");
901+
902+
// Refetch: stale-while-revalidate — no fallback, then the new truth.
903+
refetch();
904+
flush();
905+
await wait(2);
906+
flush();
907+
expect(html()).toBe("<h1>Poll</h1><ul><li>Tacos - 0</li></ul>");
908+
await wait(30);
909+
flush();
910+
expect(html()).toBe("<h1>Poll</h1><ul><li>Tacos - 1</li></ul>");
911+
dispose();
912+
});
913+
914+
test("control: createStore(fn, seed) in the same spot", async () => {
915+
const { html, dispose } = mount(createStore);
916+
expect(html()).toBe("<h1>Poll</h1><div>Loading...</div>");
917+
await wait(30);
918+
flush();
919+
expect(html()).toBe("<h1>Poll</h1><ul><li>Tacos - 0</li></ul>");
920+
dispose();
921+
});
922+
});

0 commit comments

Comments
 (0)