diff --git a/src/stores/core/entities.spec.ts b/src/stores/core/entities.spec.ts index 2b405ebe..f05f5cc1 100644 --- a/src/stores/core/entities.spec.ts +++ b/src/stores/core/entities.spec.ts @@ -110,9 +110,26 @@ describe("newEntityStore", () => { const store = newEntityStore({ name: "test", ydoc }); store.addEntity({ id: "a", findex: "0" }); store.addEntity({ id: "b", findex: "1" }); + store.getEntities(); store.deleteEntities(["a"]); expect(store.getEntities()).toEqual([{ id: "b", findex: "1" }]); }); + + test("should remove multiple entities: complex deletion order", () => { + const ydoc = new Y.Doc(); + const store = newEntityStore({ name: "test", ydoc }); + store.addEntity({ id: "a", findex: "0" }); + store.addEntity({ id: "b", findex: "1" }); + store.addEntity({ id: "c", findex: "2" }); + store.addEntity({ id: "d", findex: "3" }); + store.addEntity({ id: "e", findex: "4" }); + store.getEntities(); + store.deleteEntities(["e", "b", "d"]); + expect(store.getEntities()).toEqual([ + { id: "a", findex: "0" }, + { id: "c", findex: "2" }, + ]); + }); }); describe("patchEntity", () => { diff --git a/src/stores/core/entities.ts b/src/stores/core/entities.ts index 9be058d3..d51fe6f9 100644 --- a/src/stores/core/entities.ts +++ b/src/stores/core/entities.ts @@ -244,19 +244,33 @@ function newEntityListCache(entityMap: Y.Map>) { if (dirtyKeyMap.size === 0) return; const indexMap = new Map(); + const targetInOrder = new Set(); let shift = 0; + + // Check concurrent entities in order. entityListCache.forEach((entity, i) => { const v = dirtyKeyMap!.get(entity.id); if (!v) return; + targetInOrder.add(entity.id); indexMap.set(entity.id, i + shift); if (v.action === "delete") { shift--; } }); + // Check new entities. + for (const [id] of dirtyKeyMap) { + if (!targetInOrder.has(id)) { + targetInOrder.add(id); + } + } + entityListCache = entityListCache.concat(); - for (const [id, v] of dirtyKeyMap) { + for (const id of targetInOrder) { + const v = dirtyKeyMap.get(id); + if (!v) continue; + switch (v.action) { case "add": case "update":