MCPcopy
hub / github.com/tldraw/tldraw / applyNetworkDiff

Method applyNetworkDiff

packages/sync-core/src/lib/TLSyncClient.ts:873–907  ·  view source on GitHub ↗

* Applies a 'network' diff to the store this does value-based equality checking so that if the * data is the same (as opposed to merely identical with ===), then no change is made and no * changes will be propagated back to store listeners

(diff: NetworkDiff<R>, runCallbacks: boolean)

Source from the content-addressed store, hash-verified

871 * changes will be propagated back to store listeners
872 */
873 private applyNetworkDiff(diff: NetworkDiff<R>, runCallbacks: boolean) {
874 this.debug('applyNetworkDiff', diff)
875 const changes: RecordsDiff<R> = { added: {} as any, updated: {} as any, removed: {} as any }
876 type k = keyof typeof changes.updated
877 let hasChanges = false
878 for (const [id, op] of objectMapEntries(diff)) {
879 if (op[0] === RecordOpType.Put) {
880 const existing = this.store.get(id as RecordId<any>)
881 if (existing && !isEqual(existing, op[1])) {
882 hasChanges = true
883 changes.updated[id as k] = [existing, op[1]]
884 } else {
885 hasChanges = true
886 changes.added[id as k] = op[1]
887 }
888 } else if (op[0] === RecordOpType.Patch) {
889 const record = this.store.get(id as RecordId<any>)
890 if (!record) {
891 // the record was removed upstream
892 continue
893 }
894 const patched = applyObjectDiff(record, op[1])
895 hasChanges = true
896 changes.updated[id as k] = [record, patched]
897 } else if (op[0] === RecordOpType.Remove) {
898 if (this.store.has(id as RecordId<any>)) {
899 hasChanges = true
900 changes.removed[id as k] = this.store.get(id as RecordId<any>)
901 }
902 }
903 }
904 if (hasChanges) {
905 this.store.applyDiff(changes, { runCallbacks })
906 }
907 }
908
909 // eslint-disable-next-line tldraw/prefer-class-methods
910 private rebase = () => {

Callers 2

didReconnectMethod · 0.95
TLSyncClientClass · 0.95

Calls 7

debugMethod · 0.95
objectMapEntriesFunction · 0.90
applyObjectDiffFunction · 0.90
isEqualFunction · 0.85
applyDiffMethod · 0.80
getMethod · 0.65
hasMethod · 0.45

Tested by

no test coverage detected