Subdocs cleared after moving a doc

Hi,

I want to move a document that contain subdocuments. I do it with two transactions:

(1) I remove the document from the original YArray
(2) I insert it to the new YArray

It works fine for the document itself. Its properties are well-preserved.

But then, its subdocuments are cleared.

After a little investigation, it seems that the transaction (1) indicates that all subdocuments should be destroyed.

I don’t want to destroy any document or subdocument, since my goal is to move them.

It seems weird to me that subdocuments are lost, but not the moved document itself–because it’s destroyed as well (I’ve put on some trace logs). But that’s not the key point.

Is there a way to remove a document from an YArray without triggering any destroy?

I’ve put up a very dirty hack that works:

  1. I’ve added an optional field cannotBeDestroyed in the interface of Y.Doc
  2. I’ve overloaded the method Y.Doc::destroy so that it instantly returns if this.cannotBeDestroyed is set to true
  3. Before deleting the document from the array, I loop over all the subdocuments of the document and I set subdoc.cannotBeDestroyed = true
  4. Just after the deletion is done, I mark all subdocuments as destroyable again

It’s extremely dirty, but it also works extremely well.

I also tried to apply cannotBeDestroyed to the main document that is moved, but that triggered critical errors. There must be a reason why you can prevent destroying subdocuments safely, but not the main moved document. I’d be curious to know, I’m starting to have some expertise about YJS now.