Storing diff updates to y-leveldb

I can store and retrive full update to leveldb, but I can’t do the same with diff updates.

What am I doing wrong here?

async function diffUpdates() {
  let ydoc = new Y.Doc();
  let fullUpdate, diffUpdate;
  let stateVector;

  // compute full update and store it
  ydoc.getArray("arr").insert(0, [1, 2, 3]);
  fullUpdate = Y.encodeStateAsUpdate(ydoc);
  await persistence.storeUpdate("myDoc", fullUpdate);
  ydoc.destroy();

  // get ydoc from leveldb and make some changes to it
  ydoc = await persistence.getYDoc("myDoc");
  ydoc.getArray("arr").insert(0, [4, 5, 6]);

  // compute diff update and store it
  stateVector = await persistence.getStateVector("myDoc");
  diffUpdate = await persistence.getDiff("myDoc", stateVector);
  await persistence.storeUpdate("myDoc", diffUpdate);
  ydoc.destroy();

  ydoc = await persistence.getYDoc("myDoc");
  console.log("from leveldb", ydoc.getArray("arr").toJSON());  // expected [4, 5, 6, 1, 2, 3] 
  // actual [1, 2, 3]
}

I found the problem. I was using getDiff to compute the diff update, which isn’t probably what it’s used for in this situation. If I computed diffUpdate using

diffUpdate = Y.encodeStateAsUpdate(ydoc, stateVector);

it works as expected.

1 Like