3 ms·
This is because you are updating properties, not adding items to a list in parallel. It's totally not obvious, but the lines let doc1 = Automerge.from({ card
by xyzzy_plugh 5y ago
This is because you are updating properties, not adding items to a list in parallel. It's totally not obvious, but the lines
let doc1 = Automerge.from({ cards: [] });
let doc2 = Automerge.from({ cards: [] });
describe two different property updates, as I understand it. This is equivalent to
let doc1 = Automerge.change(Automerge.init(), doc => {
doc.cards = [];
});
doc1 = Automerge.change(doc1, doc => {
doc.cards.push({ title: 'card1' });
});
for each document. It's that initial assignment that is causing you trouble. If you start from a shared state, then adding items in parallel works exactly how you would expect:
let doc1 = Automerge.from({ cards: [] })
let doc2 = Automerge.init()
// Merge doc1 into doc2
doc2 = Automerge.merge(doc2, doc1)
doc1 = Automerge.change(doc1, doc => {
doc.cards.push({ title: 'card1' })
});
doc2 = Automerge.change(doc2, doc => {
doc.cards.push({ title: 'card2' })
})
doc1 = Automerge.merge(doc1, doc2)
doc2 = Automerge.merge(doc2, doc1)
Both docs are equal.
N.B. this is just my understanding of how this works based on reading the docs, I haven't actually done much depth of confirming beyond running the above code. In particular the warning about properties is extremely important:
> The only case Automerge cannot handle automatically, because there is no well-defined resolution, is when users concurrently update the same property in the same object (or, similarly, the same index in the same list). In this case, Automerge arbitrarily picks one of the concurrently written values as the "winner"
which is exactly what you are seeing here.
- Tade0 5y agoThanks for the writeup, makes sense now.