[2.0 / next] createOptimisticStore: add, delete, add with saves pending hands <For> an undefined row
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 25/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- javascript, typescript
- 领域
- frontend
调研方向
Start by running the two vitest specs in packages/web/test/ against commit 42350195f to confirm the Cannot read properties of undefined throw. The bug sits in how createOptimisticStore records pending guesses and replays them by position when action saves and refresh(todos) land, so read that code path and how <For> consumes the store. Done means both fixture-driven add/delete/add cases render [milk][jam][tea] and [milk][eggs][tea], and the related list-first and declined-delete specs also pass.
由索引模型根据 Issue 内容生成。
描述
Describe the bug
The todo list from the 2.0 cheatsheet: createOptimisticStore(() => api.list(), []), with add and remove actions that each write a guess, yield the save, then call refresh(todos). With both saves still out, add a todo, delete one (another row or the one just added), then add a second todo. The second add's guess hands <For> an undefined row, so t.title in the row callback throws Cannot read properties of undefined (reading 'title') and the new row never renders.
No server answer is needed after the list's first load: three clicks against any latency at all reproduce it. Push then filter, or filter then push, are fine; it takes push, filter, push with the earlier actions still pending. In the fuzz harness, where the throw did not stop the run, the second row stayed missing after every save and list refetch landed.
Found by fuzzing random sequences of these actions against a fake server and comparing the settled screen with the server's list.
Your Example Website or App
The vitest spec below (in packages/web/test/), on next @ 42350195f.
Steps to Reproduce the Bug or Issue
import { render } from "@solidjs/web";
import { action, createOptimisticStore, flush, For, Loading, refresh } from "solid-js";
import { expect, it } from "vitest";
const tick = async () => { for (let i = 0; i < 2; i++) { await new Promise(r => setTimeout(r, 0)); flush(); } };
type Todo = { id: number; title: string };
// The cheatsheet's todo list: add, delete, add, with every save still out.
for (const removeWhich of ["other", "just-added"] as const)
it(`add, delete (${removeWhich}), add: the second add's guess renders`, async () => {
const out: { key: string; resolve: (v: any) => void }[] = [];
const call = (key: string) => new Promise<any>(resolve => out.push({ key, resolve }));
const el = document.createElement("div");
let add!: (t: Todo) => Promise<unknown>, remove!: (id: number) => Promise<unknown>;
const dispose = render(() => {
const [todos, setTodos] = createOptimisticStore<Todo[]>(() => call("list"), []);
add = action(function* (todo: Todo) { setTodos(s => { s.push(todo); }); yield call("add:" + todo.id); refresh(todos); });
remove = action(function* (id: number) { setTodos(s => s.filter(x => x.id !== id)); yield call("remove:" + id); refresh(todos); });
return <Loading fallback={<p>loading</p>}><For each={todos}>{t => <li>[{t.title}]</li>}</For></Loading>;
}, el);
await tick();
out.shift()!.resolve([{ id: 1, title: "milk" }, { id: 2, title: "eggs" }]); await tick();
add({ id: 3, title: "jam" }); flush(); await tick();
remove(removeWhich === "other" ? 2 : 3); flush(); await tick();
let error: string | null = null;
try { add({ id: 4, title: "tea" }); flush(); await tick(); } catch (e) { error = (e as Error).message; }
const frame = el.textContent;
dispose();
expect({ error, frame }).toEqual({ error: null, frame: removeWhich === "other" ? "[milk][jam][tea]" : "[milk][eggs][tea]" });
});
npx vitest run in packages/web. Both tests fail with error: "Cannot read properties of undefined (reading 'title')", and the frame stays at [milk][jam] (or [milk][eggs]).
Expected behavior
Each guess applies to the list as it currently stands: the second add shows tea at the end of the list, and the row callback never receives undefined.
Related shapes, likely the same cause
The same list corrupts in two other everyday sequences. Both settle to a wrong list that only a later, unrelated refresh repairs. In both, the guess looks recorded against one version of the list and applied by position to another.
- Add a todo before the list's first load lands. The list settles to
[milk][eggs][eggs]or[milk][milk][eggs], where the server has[milk][eggs][jam]. - Delete a todo, the server declines, then add a todo. The list settles to
[milk][eggs][milk], where the server has[milk][eggs][jam].
Spec for the add-during-first-load case
import { render } from "@solidjs/web";
import { action, createOptimisticStore, flush, For, Loading, refresh } from "solid-js";
import { expect, it } from "vitest";
const tick = async () => { for (let i = 0; i < 2; i++) { await new Promise(r => setTimeout(r, 0)); flush(); } };
type Todo = { id: number; title: string };
// The cheatsheet's todo list; the user adds a todo before the list has loaded.
async function run(order: "list-first" | "save-first") {
let server: Todo[] = [{ id: 1, title: "milk" }, { id: 2, title: "eggs" }];
const out: { key: string; go: () => void }[] = [];
const call = (key: string, apply?: () => void) =>
new Promise<any>(resolve => out.push({ key, go: () => { apply?.(); resolve(key === "list" ? server.map(t => ({ ...t })) : true); } }));
const land = (key: string) => out.splice(out.findIndex(f => f.key === key), 1)[0].go();
const el = document.createElement("div");
const frames: string[] = [];
let add!: (t: Todo) => Promise<unknown>;
const dispose = render(() => {
const [todos, setTodos] = createOptimisticStore<Todo[]>(() => call("list"), []);
add = action(function* (todo: Todo) {
setTodos(s => { s.push(todo); });
yield call("add:" + todo.id, () => { server = [...server, todo]; });
refresh(todos);
});
return <Loading fallback={<p>loading</p>}><For each={todos}>{t => <li>[{t.title}]</li>}</For></Loading>;
}, el);
await tick();
add({ id: 3, title: "jam" }); flush(); await tick();
if (order === "list-first") { land("list"); await tick(); frames.push(el.textContent!); land("add:3"); await tick(); }
else { land("add:3"); await tick(); land("list"); await tick(); frames.push(el.textContent!); }
while (out.length) { land(out[0].key); await tick(); } // the add's refresh
frames.push(el.textContent!);
dispose();
return frames;
}
it("the list lands while the add is saving", async () => {
expect(await run("list-first")).toEqual(["[milk][eggs][jam]", "[milk][eggs][jam]"]);
});
it("the add is saved before the list lands", async () => {
expect(await run("save-first")).toEqual(["[milk][eggs][jam]", "[milk][eggs][jam]"]);
});
Spec for the declined-delete case
import { render } from "@solidjs/web";
import { action, createOptimisticStore, flush, For, Loading, refresh } from "solid-js";
import { expect, it } from "vitest";
const tick = async () => { for (let i = 0; i < 2; i++) { await new Promise(r => setTimeout(r, 0)); flush(); } };
type Todo = { id: number; title: string };
// The cheatsheet's todo list: delete a todo, the server declines, then add another.
for (const renameFirst of [false, true])
it(`a declined delete, then an add, settles to the server's list (rename first=${renameFirst})`, async () => {
let server: Todo[] = [{ id: 1, title: "milk" }, { id: 2, title: "eggs" }];
const out: { key: string; go: (ok: boolean) => void }[] = [];
const call = (key: string, apply?: () => void) => new Promise<any>((resolve, reject) =>
out.push({ key, go: ok => { if (!ok) return reject(new Error(key)); apply?.(); resolve(key === "list" ? server.map(t => ({ ...t })) : true); } }));
const land = (key: string, ok = true) => out.splice(out.findIndex(f => f.key === key), 1)[0].go(ok);
const el = document.createElement("div");
let rename!: (id: number, t: string) => Promise<unknown>, remove!: (id: number) => Promise<unknown>, add!: (t: Todo) => Promise<unknown>;
const dispose = render(() => {
const [todos, setTodos] = createOptimisticStore<Todo[]>(() => call("list"), []);
rename = action(function* (id: number, title: string) {
setTodos(s => { const t = s.find(x => x.id === id); if (t) t.title = title; });
try { yield call("rename:" + id, () => { server = server.map(t => t.id === id ? { ...t, title } : t); }); } catch {}
refresh(todos);
});
remove = action(function* (id: number) {
setTodos(s => s.filter(x => x.id !== id));
try { yield call("remove:" + id, () => { server = server.filter(t => t.id !== id); }); } catch {}
refresh(todos);
});
add = action(function* (todo: Todo) {
setTodos(s => { s.push(todo); });
try { yield call("add:" + todo.id, () => { server = [...server, todo]; }); } catch {}
refresh(todos);
});
return <Loading fallback={<p>loading</p>}><For each={todos}>{t => <li>[{t.title}]</li>}</For></Loading>;
}, el);
const step = async (f: () => void) => { f(); flush(); await tick(); };
await step(() => land("list"));
if (renameFirst) await step(() => { rename(1, "oat milk"); });
await step(() => { remove(1); });
await step(() => land("remove:1", false)); // the server declines the delete
await step(() => { add({ id: 3, title: "jam" }); });
while (out.length) await step(() => land(out[0].key));
const settled = el.textContent;
dispose();
expect(settled).toBe(renameFirst ? "[oat milk][eggs][jam]" : "[milk][eggs][jam]");
});
Platform
- OS: macOS
- Browser: n/a (jsdom)
- Version: solid-js 2.0.0-rc.20,
next@ 42350195f
- 主要语言
- TypeScript
- 星标
- 36.2k
- 派生
- 1.1k
- 平均合并
- 9 小时 56 分钟
- 30 天内合并 PR
- 487
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
solidjs/solid 的其他 Issue
-
难度 4/5 3-5 天 新手友好度 55/100
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 45/100
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 35/100
维护者通常 1 天内回复
-
难度 5/5 一周以上 新手友好度 22/100
维护者通常 1 天内回复
-
What gets passed to the `when` prop of a `Show` still gets executed twice on SSR可能已有人在做 @brenelz 于 5 天前认领。 未关闭1.x
难度 3/5 1-2 天 新手友好度 52/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 2 天内回复
-
reproduced
难度 2/5 1-3 小时 新手友好度 74/100
anomalyco/opencode#62052 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
strapi/design-system#2084 ·
维护者通常 3 天内回复
-
check:passed streams:remove
难度 1/5 1 小时以内 新手友好度 67/100
维护者通常 1 天内回复
-
OAuth documentation for GET /api/v2/users/me/ appears inconsistent with required global read scope未关闭
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复