Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/query-baseline-ignore.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@retreejs/query": patch
---

Fixed an "already has a structural parent" error when a query reused a backend-cached result after an overlapping optimistic update rolled back.
53 changes: 53 additions & 0 deletions packages/retree-query/src/QueryNode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,59 @@ describe("QueryNode optimistic updates", () => {
expect(node.error?.message).toBe("Mutation failed");
});

it("re-observes a cached result that an overlapping rollback restored", async () => {
const source = new FakeQuerySource<ITaskArgs, ITask[]>();
const first = Retree.root(
new QueryNode(source, { args: { listId: "today" } })
);
const second = Retree.root(
new QueryNode(source, { args: { listId: "today" } })
);
let resolveFirstMutation!: () => void;
const firstMutation = new Promise<null>((resolve) => {
resolveFirstMutation = () => resolve(null);
});
let rejectSecondMutation!: () => void;
const secondMutation = new Promise<null>((_, reject) => {
rejectSecondMutation = () => reject(new Error("Mutation failed"));
});
const stopFirst = Retree.on(first, "nodeChanged", () => undefined);
source.subscriptions[0].onValue([
{ id: "task-1", text: "Buy groceries", isCompleted: false },
]);

first.optimisticUpdate({
ctx: { promise: firstMutation },
apply(tasks) {
tasks[0].isCompleted = true;
},
});
first.optimisticUpdate({
ctx: { promise: secondMutation },
apply(tasks) {
tasks[0].text = "Optimistic text";
},
});
resolveFirstMutation();
await firstMutation;
// The backend caches the object it emits, so the next subscriber
// receives this same instance.
const cached = [
{ id: "task-1", text: "Buy groceries", isCompleted: true },
];
source.nextCurrentValue = cached;
source.subscriptions[0].onValue(cached);
rejectSecondMutation();
await secondMutation.catch(() => undefined);
stopFirst();

Retree.on(second, "nodeChanged", () => undefined);

expect(first.state).toEqual(cached);
expect(second.state).toEqual(cached);
expect(source.subscriptions).toHaveLength(2);
});

it("warns in dev mode when optimisticUpdate no-ops because state is undefined", () => {
const warn = vi
.spyOn(console, "warn")
Expand Down
2 changes: 2 additions & 0 deletions packages/retree-query/src/QueryNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export class QueryNode<TArgs, TState> extends ReactiveNode {
* Last state emitted by the subscription that Retree accepted as a clean
* server baseline. During overlapping optimistic updates this may advance
* even when `state` intentionally keeps the newer optimistic value.
* Not an owned child: the backend may share this object with another query.
*/
@ignore
private lastEmittedState: TState | undefined;
/**
* True while local optimistic state differs from the last clean server
Expand Down
Loading