automerge / automerge-classic

A JSON-like data structure (a CRDT) that can be modified concurrently by different users, and merged again automatically.
http://automerge.org/
MIT License
14.75k stars 466 forks source link

Error when apply changes on array object #409

Closed FriMay closed 3 years ago

FriMay commented 3 years ago

Versions:

let doc = Automerge.from({x: []})

const oldDoc = doc

for (let i = 0; i < 10; i++) doc = Automerge.change(doc, doc => doc.x.push(i))

const changes = Automerge.getChanges(oldDoc, doc)

// Try to apply changes.
// Expected x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
// Founded: Attempting to use an outdated Automerge document that has already been updated.
// Please use the latest document state, or call 
// Automerge.clone() if you really need to use this old document state.
const newDoc = Automerge.applyChanges(oldDoc, changes)
ept commented 3 years ago

@FriMay You're getting this error because oldDoc is a reference to an object that has since been modified by the call to .change. If you want to make a copy of that document, you can replace the second line with:

const oldDoc = Automerge.clone(doc)
FriMay commented 3 years ago

Okay, thank you very match!