mirror of
https://github.com/toeverything/AFFiNE.git
synced 2025-01-03 16:29:16 +03:00
feat(core): load ydoc on demand (#8241)
This commit is contained in:
parent
25969a34e8
commit
561f96bb71
@ -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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user