Skip to content

Commit 561f96b

Browse files
committed
feat(core): load ydoc on demand (#8241)
1 parent 25969a3 commit 561f96b

File tree

1 file changed

+12
-7
lines changed
  • packages/common/infra/src/sync/doc

1 file changed

+12
-7
lines changed

packages/common/infra/src/sync/doc/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,22 @@ export class DocEngine {
142142
}
143143

144144
addDoc(doc: YDoc, withSubDocs = true) {
145-
this.localPart.actions.addDoc(doc);
146145
this.remotePart?.actions.addDoc(doc.guid);
146+
this.localPart.actions.addDoc(doc);
147147

148148
if (withSubDocs) {
149-
const subdocs = doc.getSubdocs();
150-
for (const subdoc of subdocs) {
151-
this.addDoc(subdoc, false);
152-
}
153-
doc.on('subdocs', ({ added }: { added: Set<YDoc> }) => {
149+
doc.on('subdocs', ({ added, loaded }) => {
150+
// added: the subdocs that are existing on the ydoc
151+
// loaded: the subdocs that have been called `ydoc.load()`
152+
//
153+
// we add all existing subdocs to remote part, let them sync between storage and server
154+
// but only add loaded subdocs to local part, let them sync between storage and ydoc
155+
// sync data to ydoc will consume more memory, so we only sync the ydoc that are necessary.
154156
for (const subdoc of added) {
155-
this.addDoc(subdoc, false);
157+
this.remotePart?.actions.addDoc(subdoc.guid);
158+
}
159+
for (const subdoc of loaded) {
160+
this.localPart.actions.addDoc(subdoc);
156161
}
157162
});
158163
}

0 commit comments

Comments
 (0)