Misordered updates result in unexpected intermediate states (possibly permanent too?)

We’re having an issue where required properties of certain Y.Maps are being removed unexpectedly. I found that it happens when updates are applied out of order, like this code demonstrates:

const Y = require('yjs')

const doc1 = new Y.Doc()
const doc2 = new Y.Doc()

const doc1Map = doc1.getMap('top')
const doc2Map = doc2.getMap('top')

let updates = []
doc1.on('updateV2', update => {

// Set initial values on and apply updates to doc 2
doc1Map.set('x', 1)
doc1Map.set('y', 2)
Y.applyUpdateV2(doc2, updates[0])
Y.applyUpdateV2(doc2, updates[1])
updates = []
console.log(doc2Map.toJSON())   // => { x: 1, y: 2 }

// Change two values, but apply updates to doc 2 in reverse order
doc1Map.set('x', 3)
doc1Map.set('y', 4)
Y.applyUpdateV2(doc2, updates[1])
console.log(doc2Map.toJSON())   // => { x: 1 }  y is entirely missing!
Y.applyUpdateV2(doc2, updates[0])
console.log(doc2Map.toJSON())   // => { x: 3, y: 4 }

At the second console.log, I’d expect any combination of any of the x/y values, but not an absence of either.

I also believe that several documents have permanently lost values in this way, even after applying all updates. However, I have not yet reproduced that case in isolation and will keep looking.

Thank you!!!

Hmmm. That’s strange. I tried it myself and it seems off to me. I could understand if the out-of-order update (updates[1]) did nothing until the missing update (updates[0]) was applied, but it doesn’t seem right that the key disappears.

I suggest opening an issue: Issues · yjs/yjs · GitHub

1 Like

Thank you! I filed this issue:

1 Like