Error when moving an YMap item inside YArray


I get an unexpected error when moving an YArray item, when the item is a YMap. The idea is to remove an item and then re-insert it into another position, but the problem can be re-produced with the following snippet of code:

  const ydoc = new Y.Doc();
  const items = ydoc.getArray("items");
  items.push([new Y.Map(), new Y.Map()]);
  const removedItem = items.get(0);

When pushing the removed item back into the array, I get an error like

Uncaught TypeError: Cannot read properties of null (reading 'forEach')
    at YMap._integrate (yjs.cjs:6005:60)
    at ContentType.integrate (yjs.cjs:9387:15)
    at Item.integrate (yjs.cjs:9948:20)
    at eval (yjs.cjs:5394:22)
    at Array.forEach (<anonymous>)
    at typeListInsertGenericsAfter (yjs.cjs:5367:11)
    at typeListPushGenerics (yjs.cjs:5477:10)
    at eval (yjs.cjs:5828:9)
    at transact (yjs.cjs:3416:14)
    at YArray.push (yjs.cjs:5827:7)

You can see it in action in this CodeSandbox:

If I use strings instead of YMaps as items, there are no errors. Is there something special I should do in order to be able to re-insert a Y.Map into an array?

Thanks for Y.js btw, it’s awesome! Hoping to be able to contribute at some point, still learning the ropes.

Having looked at source code it seems to me that the “integration” of a YMap or YArray to a YDoc is a one-way process and there’s no way to “un-integrate” or “re-integrate” if I’ve understood correctly. Does this mean that I am to change the ordering of (YMap or YArray) elements in a list, I’ll practically have to treat the moved content as new content? This would be bad for collaborative editing of nested data of course.

By treating as new content, I mean that I would practically insert a new YMap with identical content to the original one. And if the moved item is a deep hierarchical structure of YMaps and YArrays, I would have to traverse the graph and create a deep clone. Sounds suboptimal for changing item ordering :sweat_smile: