Skip to content

Commit 961ec4d

Browse files
committed
fix: tree event handle
1 parent 51a3e81 commit 961ec4d

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

packages/core/src/core/loroEventApply.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function applySingleEventToDraft(
140140
target = parent && key !== undefined ? getAt(parent, key)! : [];
141141
}
142142
if (isJSONArray(target)) {
143-
applyTreeDiff(target, e.diff.diff, ignoreSet);
143+
applyTreeDiff(target, e.diff.diff);
144144
// Invalidate cache for this roots array after structural change
145145
ROOTS_TREE_INDEX_CACHE.delete(target as JSONValue[]);
146146
}
@@ -423,7 +423,6 @@ function applyTreeDiff(
423423
oldIndex: number;
424424
}
425425
>,
426-
ignoreSet: Set<ContainerID>,
427426
) {
428427
type Node = StateTreeNode;
429428

@@ -443,7 +442,6 @@ function applyTreeDiff(
443442
};
444443
const idx = clampIndex(d.index, arr.length + 1);
445444
arr.splice(idx, 0, node);
446-
ignoreSet.add(`cid:${d.target}:Map`);
447445
} else if (d.action === "delete") {
448446
const arr = getChildrenArray(d.oldParent);
449447
if (!arr) continue;

packages/core/src/core/mirror.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,8 @@ export class Mirror<S extends SchemaType> {
478478
this.syncing = true;
479479
try {
480480
// Incrementally update state using event deltas
481-
this.state = applyEventBatchToState(
482-
this.state,
483-
event,
484-
(id) => this.doc.getContainerById(id),
481+
this.state = applyEventBatchToState(this.state, event, (id) =>
482+
this.doc.getContainerById(id),
485483
);
486484
// Notify subscribers of the update
487485
this.notifySubscribers(SyncDirection.FROM_LORO);
@@ -1582,18 +1580,19 @@ export class Mirror<S extends SchemaType> {
15821580
const shouldCheck =
15831581
this.options.debug || this.options.checkStateConsistency;
15841582
if (shouldCheck) {
1585-
this.checkStateConsistency(newState);
1583+
this.checkStateConsistency();
15861584
}
15871585

15881586
// Notify subscribers
15891587
this.notifySubscribers(SyncDirection.TO_LORO, tags);
15901588
}
15911589

1592-
checkStateConsistency(newState: InferType<S>) {
1593-
if (!deepEqual(newState, toNormalizedJson(this.doc))) {
1590+
checkStateConsistency() {
1591+
const state = this.state;
1592+
if (!deepEqual(state, toNormalizedJson(this.doc))) {
15941593
console.error(
15951594
"State diverged",
1596-
JSON.stringify(newState, null, 2),
1595+
JSON.stringify(state, null, 2),
15971596
JSON.stringify(toNormalizedJson(this.doc), null, 2),
15981597
);
15991598
throw new Error("[InternalError] State diverged");

packages/core/tests/core/mirror-tree.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ describe("LoroTree integration", () => {
158158
return roots;
159159
};
160160

161-
const runOnce = (seed: number) => {
161+
const runOnce = async (seed: number) => {
162162
const rng = mulberry32(seed);
163163
const doc = new LoroDoc();
164164
const s = schema({
@@ -174,6 +174,12 @@ describe("LoroTree integration", () => {
174174
checkStateConsistency: true,
175175
});
176176

177+
const docB = new LoroDoc();
178+
const mB = new Mirror({
179+
doc: docB,
180+
schema: s,
181+
});
182+
177183
// 1) Create an initial random tree
178184
const initial = buildInitial(rng, 12);
179185
m.setState({ tree: initial } as any);
@@ -217,12 +223,18 @@ describe("LoroTree integration", () => {
217223
);
218224
// If the move is a no-op (same parent + index), skip sometimes to avoid churn
219225
m.setState({ tree: nextTree } as any);
226+
docB.import(
227+
doc.export({ mode: "update", from: docB.version() }),
228+
);
229+
await Promise.resolve();
230+
expect(mB.getState()).toStrictEqual(m.getState());
231+
mB.checkStateConsistency();
220232
}
221233
};
222234

223235
// Try multiple seeds
224236
for (const seed of [1, 42, 2025]) {
225-
runOnce(seed);
237+
await runOnce(seed);
226238
}
227239
});
228240

0 commit comments

Comments
 (0)