From 3b67b7d19519a4716b25e70e32b275b2c3f10200 Mon Sep 17 00:00:00 2001 From: Bernhard Seefeld Date: Tue, 4 Mar 2025 14:01:33 -0800 Subject: [PATCH] don't recreate document, but reuse existing document, when updating an array value --- .../packages/common-runner/src/utils.ts | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/typescript/packages/common-runner/src/utils.ts b/typescript/packages/common-runner/src/utils.ts index 7cdc4dff9..59b202bbc 100644 --- a/typescript/packages/common-runner/src/utils.ts +++ b/typescript/packages/common-runner/src/utils.ts @@ -16,7 +16,7 @@ import { } from "./query-result-proxy.ts"; import { isCell } from "./cell.ts"; import { type ReactivityLog } from "./scheduler.ts"; -import { createRef } from "./doc-map.ts"; +import { createRef, getDocByEntityId } from "./doc-map.ts"; export function extractDefaultValues(schema: any): any { if (typeof schema !== "object" || schema === null) return undefined; @@ -556,10 +556,20 @@ export function normalizeToDocLinks( // transition from a previous run, but only if the value didn't // change as well. } else { - value[i] = { cell: getDoc(value[i]), path: [] } satisfies DocLink; - value[i].cell.entityId = itemId; - value[i].cell.space = parentDoc.space; - value[i].cell.sourceCell = parentDoc; + const previousDoc = getDocByEntityId(parentDoc.space!, itemId, false); + if (previousDoc) { + // TODO(seefeld): We should instead accumulate the changes and send + // them back as a whole. Do that together with no longer changing + // data inline? + previousDoc.send(value[i]); + value[i] = previousDoc; + } else { + value[i] = { cell: getDoc(value[i]), path: [] } satisfies DocLink; + // Have to do it manually, since we're not specifying the cause, but the entityId directly. + value[i].cell.entityId = itemId; + value[i].cell.space = parentDoc.space; + value[i].cell.sourceCell = parentDoc; + } preceedingItemId = itemId; log?.writes.push(value[i]);