y-crdt / ypy

Python bindings to y-crdt
https://ypy.readthedocs.io/en/latest/
Other
184 stars 22 forks source link

Error when moving an element from yjs #93

Open hbcarlos opened 1 year ago

hbcarlos commented 1 year ago

When using the new move feature from a Yjs client, If there is a Ypy client connected, the first client (the Yjs client that moved an element) receives an event undoing the move.

Everything works fine if there is not a Ypy client connected or if the client moving an element is a python client (Ypy) . I suspect the problem comes from my PR for the move feature #83, which I guess is missing handling a moving event coming from another client.

I have been looking into the code to see how Yrs or Ypy handles the events coming from other clients but I couldn't find anything. @Waidhoferj, @Horusiath or @dmonad do you know where should I look in the code to debug it?

Context

To reproduce the bug you can use a Yjs client like:

const doc = new Doc();
const test = doc.getArray('test');
const provider = new WebsocketProvider('ws://localhost:8888', 'rtc_yjs_test', doc);

const observe = (event) => console.log("OBSERVER:", event.changes);
test.observe(observe);

test.push([0,1,2,3,4]);
test.move(0, 2);

and a Ypy client like:

def callbackArray(event):
    print("OBSERVER:", event.delta)

doc = Y.YDoc()
test = doc.get_array('test')
idTest = test.observe(callbackArray)

ws = await connect("ws://localhost:8888/rtc_yjs_test")
WebsocketProvider(doc, ws)

and you can start a WebSocket server with: HOST=localhost PORT=8888 npx y-websocket

When the Yjs client moves an element it receives two events, the first event is the correct move of the element:

{
  "added": {},
  "deleted": {},
  "delta": [
    { "delete": 1 },
    { "retain": 1 },
    { "insert": [0] }
  ]
}

But the second event is reverting the move:

{
  "added": { Item([0]) },
  "deleted": {},
  "delta": [
    { "insert": [0]  },
    { "retain": 1 },
    { "delete": 1}
  ]
}
SylvainCorlay commented 1 year ago

Ping @dmonad in case you have an idea of what the issue may be here.