fix(frontend): loadFromState race condition + plugin stale closure (Issues #7, #8)

This commit is contained in:
2026-06-29 23:50:18 +02:00
parent 8493138b4b
commit 275b863f03
3 changed files with 108 additions and 16 deletions
+19 -11
View File
@@ -122,25 +122,33 @@ export class YjsDocument {
bgConfigs?: BackgroundConfig[];
}): void {
this.doc.transact(() => {
this.data.elements.clear();
for (const el of state.elements) {
this.data.elements.set(el.id, el);
// Issue #7: Only clear and replace a map if the incoming array is non-empty.
// Clearing maps when data is empty wipes CRDT data from other connected clients.
if (state.elements.length > 0) {
this.data.elements.clear();
for (const el of state.elements) {
this.data.elements.set(el.id, el);
}
}
this.data.layers.clear();
for (const layer of state.layers) {
this.data.layers.set(layer.id, layer);
if (state.layers.length > 0) {
this.data.layers.clear();
for (const layer of state.layers) {
this.data.layers.set(layer.id, layer);
}
}
this.data.blocks.clear();
for (const block of state.blocks) {
this.data.blocks.set(block.id, block);
if (state.blocks.length > 0) {
this.data.blocks.clear();
for (const block of state.blocks) {
this.data.blocks.set(block.id, block);
}
}
if (state.groups) {
if (state.groups && state.groups.length > 0) {
this.data.groups.clear();
for (const group of state.groups) {
this.data.groups.set(group.id, group);
}
}
if (state.bgConfigs) {
if (state.bgConfigs && state.bgConfigs.length > 0) {
this.data.bgConfig.clear();
for (const cfg of state.bgConfigs) {
// Use 'default' as the key since there is typically one background config