Skip to content

Commit ac02100

Browse files
authored
Fix: Updating values in normalized-to-docs arrays (#515)
don't recreate document, but reuse existing document, when updating an array value
1 parent 6cdd27d commit ac02100

File tree

1 file changed

+15
-5
lines changed
  • typescript/packages/common-runner/src

1 file changed

+15
-5
lines changed

typescript/packages/common-runner/src/utils.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from "./query-result-proxy.ts";
1717
import { isCell } from "./cell.ts";
1818
import { type ReactivityLog } from "./scheduler.ts";
19-
import { createRef } from "./doc-map.ts";
19+
import { createRef, getDocByEntityId } from "./doc-map.ts";
2020

2121
export function extractDefaultValues(schema: any): any {
2222
if (typeof schema !== "object" || schema === null) return undefined;
@@ -556,10 +556,20 @@ export function normalizeToDocLinks(
556556
// transition from a previous run, but only if the value didn't
557557
// change as well.
558558
} else {
559-
value[i] = { cell: getDoc(value[i]), path: [] } satisfies DocLink;
560-
value[i].cell.entityId = itemId;
561-
value[i].cell.space = parentDoc.space;
562-
value[i].cell.sourceCell = parentDoc;
559+
const previousDoc = getDocByEntityId(parentDoc.space!, itemId, false);
560+
if (previousDoc) {
561+
// TODO(seefeld): We should instead accumulate the changes and send
562+
// them back as a whole. Do that together with no longer changing
563+
// data inline?
564+
previousDoc.send(value[i]);
565+
value[i] = previousDoc;
566+
} else {
567+
value[i] = { cell: getDoc(value[i]), path: [] } satisfies DocLink;
568+
// Have to do it manually, since we're not specifying the cause, but the entityId directly.
569+
value[i].cell.entityId = itemId;
570+
value[i].cell.space = parentDoc.space;
571+
value[i].cell.sourceCell = parentDoc;
572+
}
563573

564574
preceedingItemId = itemId;
565575
log?.writes.push(value[i]);

0 commit comments

Comments
 (0)