feat(core): load ydoc on demand (#8241)

This commit is contained in:
EYHN 2024-09-13 07:45:49 +00:00
parent 25969a34e8
commit 561f96bb71
No known key found for this signature in database
GPG Key ID: 46C9E26A75AB276C

View File

@ -142,17 +142,22 @@ export class DocEngine {
} }
addDoc(doc: YDoc, withSubDocs = true) { addDoc(doc: YDoc, withSubDocs = true) {
this.localPart.actions.addDoc(doc);
this.remotePart?.actions.addDoc(doc.guid); this.remotePart?.actions.addDoc(doc.guid);
this.localPart.actions.addDoc(doc);
if (withSubDocs) { if (withSubDocs) {
const subdocs = doc.getSubdocs(); doc.on('subdocs', ({ added, loaded }) => {
for (const subdoc of subdocs) { // added: the subdocs that are existing on the ydoc
this.addDoc(subdoc, false); // loaded: the subdocs that have been called `ydoc.load()`
} //
doc.on('subdocs', ({ added }: { added: Set<YDoc> }) => { // we add all existing subdocs to remote part, let them sync between storage and server
// but only add loaded subdocs to local part, let them sync between storage and ydoc
// sync data to ydoc will consume more memory, so we only sync the ydoc that are necessary.
for (const subdoc of added) { for (const subdoc of added) {
this.addDoc(subdoc, false); this.remotePart?.actions.addDoc(subdoc.guid);
}
for (const subdoc of loaded) {
this.localPart.actions.addDoc(subdoc);
} }
}); });
} }