Unloading all subdocs (e.g., in order to reclaim their resources) seems to be a dangerous operation.
Try the following code
const sharedDoc = new Y.Doc()
sharedDoc.on('subdocs', (Event) => {
const { added, removed, loaded } = Event
console.log(' # added',added.size, '# removed',removed.size, '# loaded',loaded.size)
console.log(' # SubDocs:',sharedDoc.getSubdocs().size)
})
/**** create new subdoc (will also automatically be loaded) ****/
const sharedMap = sharedDoc.getMap('sharedMap')
console.log('adding subdoc to outer map')
sharedMap.set('doc', new Y.Doc())
/**** unload subdoc... ****/
console.log('unloading subdoc')
sharedDoc.getSubdocs().forEach((SubDoc) => {
SubDoc.destroy()
})
and watch your browser’s console growing as the following lines are added over and over again:
# added 1 # removed 1 # loaded 0
# SubDocs: 1
(mind also the “added: 1”!)