I’m building a collaborative-code editor, and it seems to be working fine for the most part
I have stored the shared state as a base64 string in the backend as this
const defEncoding = fromUint8Array(Y.encodeStateAsUpdate(ydocDefault));
...and later
ydoc : {type : typeof defEncoding, default: Buffer.from(defEncoding)}
this is part of my room schema, and it does seem to work perfectly…
..until i want to send an update to my database/save my content
what I’m doing for this is :
async function handleSave(){
yMapRef.current?.set(‘content’, {
…yMapRef.current!.get(‘content’),
[yMapRef.current?.get(‘lang’)] : ytextRef.current?.toString()
}) //this is to make sure the content gets saved up in the map
const encoding = Y.encodeStateAsUpdate(ydocRef.current)
const encodingBase64 = fromUint8Array(encoding)
await fetch(${backend_url}/saveCode,{
method : “PUT”,
credentials:‘include’,
headers:{
“Content-type”:“application/json”,
},
body : JSON.stringify({
encodingBase64,
hash : room_id
})
})
ToastSuccess(“Saved Successfully”);
}
In the backend :
room.ydoc = encodingBase64
await room.save();
and for the editor itself, i’ve used yCollab with y-codemirror to bind the ytext
const extensions = [ langContext[lang], yCollab(ytextRef.current!, providerRef.current!.awareness, { undoManager: undoManagerRef.current!, }), indentUnit.of(" "), // 4 spaces EditorState.readOnly.of(readOnly), ];
the problem is, after the handleSave is called, the ytext doesn’t sync to user’s content properly. im unable to understand why this is happening