@@ -16,7 +16,7 @@ import {
1616} from "./query-result-proxy.ts" ;
1717import { isCell } from "./cell.ts" ;
1818import { type ReactivityLog } from "./scheduler.ts" ;
19- import { createRef } from "./doc-map.ts" ;
19+ import { createRef , getDocByEntityId } from "./doc-map.ts" ;
2020
2121export 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