Skip to content
Open
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
45 changes: 1 addition & 44 deletions src/plugins/liveobjects/livemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,13 @@ export class LiveMap<T extends Record<string, Value> = Record<string, Value>>
return { noop: true };
}

const previousDataRef = this._dataRef;
let update: LiveMapUpdate<T>;
if (objectState.tombstone) {
// tombstone this object and ignore the data from the object state message
update = this.tombstone(objectMessage);
} else {
// otherwise override data for this object with data from the object state
const previousDataRef = this._dataRef;
this._createOperationIsMerged = false; // RTLM6b
this._clearTimeserial = objectState.map?.clearTimeserial; // RTLM6i
this._dataRef = this._liveMapDataFromMapEntries(objectState.map?.entries ?? {}); // RTLM6c
Expand All @@ -458,9 +458,6 @@ export class LiveMap<T extends Record<string, Value> = Record<string, Value>>
update.objectMessage = objectMessage;
}

// Update parent references based on the calculated diff
this._updateParentReferencesFromUpdate(update, previousDataRef);

return update;
}

Expand Down Expand Up @@ -1095,44 +1092,4 @@ export class LiveMap<T extends Record<string, Value> = Record<string, Value>>

return false;
}

/**
* Update parent references based on the calculated update diff.
*/
private _updateParentReferencesFromUpdate(update: LiveMapUpdate<T>, previousDataRef: LiveMapData): void {
for (const [key, changeType] of Object.entries(update.update)) {
if (changeType === 'removed') {
// Key was removed - remove parent reference from the old object if it was referencing one
const previousEntry = previousDataRef.data.get(key);
if (previousEntry?.data && 'objectId' in previousEntry.data) {
const oldReferencedObject = this._realtimeObject.getPool().get(previousEntry.data.objectId);
if (oldReferencedObject) {
oldReferencedObject.removeParentReference(this, key);
}
}
}

if (changeType === 'updated') {
// Key was updated - need to handle both removal of old reference and addition of new reference
const previousEntry = previousDataRef.data.get(key);
const newEntry = this._dataRef.data.get(key);

// Remove old parent reference if there was one
if (previousEntry?.data && 'objectId' in previousEntry.data) {
const oldReferencedObject = this._realtimeObject.getPool().get(previousEntry.data.objectId);
if (oldReferencedObject) {
oldReferencedObject.removeParentReference(this, key);
}
}

// Add new parent reference if the new value references an object
if (newEntry?.data && 'objectId' in newEntry.data) {
const newReferencedObject = this._realtimeObject.getPool().get(newEntry.data.objectId);
if (newReferencedObject) {
newReferencedObject.addParentReference(this, key);
}
}
}
}
}
}
Loading