I am trying to sync a nested yDoc using y-webrtc but getting unexpected results when nesting
Scenario 1 → Working as expected
Creating shared types at root level → Stackblitz link
Relevant code
const ydoc = new Y.Doc();
const elements = ydoc.getArray('elements');
const assets = ydoc.getMap('assets');
...
React.useEffect(() => {
if (!api) return;
const binding = new ExcalidrawBinding(
elements as any, // Passing elements to an external binding
api,
provider.awareness
);
setBindings(binding);
const assetBinding = new ExcalidrawAssetsBinding(assets, api);
return () => {
setBindings(null);
binding.destroy();
assetBinding.destroy();
};
}, [api]);
When two links are opened the documents sync correctly between them
Scenario 2 → Not working as expected
Creating nested shared types → Stackblitz link
Relevant code
const ydoc = new Y.Doc();
const root = ydoc.getMap('excalidraw');
const elements = new Y.Array();
const assets = new Y.Map();
root.set('elements', elements);
root.set('assets', assets);
...
React.useEffect(() => {
if (!api) return;
const binding = new ExcalidrawBinding(
elements as any, // Passing elements to an external binding
api,
provider.awareness
);
setBindings(binding);
const assetBinding = new ExcalidrawAssetsBinding(assets, api);
return () => {
setBindings(null);
binding.destroy();
assetBinding.destroy();
};
}, [api]);
As soon as the 2nd instance of the link is opened, it crashes with an error in console.
I noticed the following behavior in the 2nd peer
console.log(elements.length) // prints 0, even though there are some elements in the document (from first peer)
console.log(elements.doc.getMap('excalidraw').get("elements").length) // prints 1 (correct)
console.log(elements == elements.doc.getMap('excalidraw').get("elements")) // Prints false
Why is nesting the types not working but creating at root level is?