BUG? Y.encodeStateAsUpdate can return different value for the same ydoc

I was expecting Y.encodeStateAsUpdate to return the same value for the same ydoc every single time.

Is my expectation incorrect?

My experiments show that some times it can return differnt value. See output at the bottom.

NOTE: In order to reproduce the problem, please run it from the shell script as shown below. It’s very hard to reproduce this otherwise.

yupdate.js

const Y = require("yjs");

const ydoc = new Y.Doc();

ydoc.getMap("foo").set("a", "a");
ydoc.getMap("foo").set("b", "b");
ydoc.getMap("foo").set("c", "c");

ydoc.getMap("bar").set("d", "d");
ydoc.getMap("bar").set("e", []);
ydoc.getMap("bar").set("f", []);

ydoc.getArray("0").insert(0, []);
ydoc.getArray("1").insert(0, []);
ydoc.getArray("2").insert(0, []);
ydoc.getArray("3").insert(0, []);
ydoc.getArray("4").insert(0, []);

console.log(Y.encodeStateAsUpdate(ydoc).length);

run.sh (chmod +x run.sh)

#!/bin/bash
for (( ; ; ))
do
  node yupdate.js
done

Run the shell script and observe that some times it returns different value for the length of the update.

$ ./run.sh
79
79
79
79
78 ← here
79
79
79
79
79
79
79
79
79
79
78 ← here
79
79
79
79
79
78 ← here

Just to complete this conversation, I’m pasting the reply I received from Kevin for the benefit of anyone else who is interested.

This is the expected behavior and can’t be changed.

Every client session generates a random “client-id”. This is to ensure that updates from different clients are unique. Otherwise, we wouldn’t be able to distinguish between two different client operations.

IMO - it’s easier to think of clientID as a sessionID instead.

2 Likes