Skip to content

Commit 9df1567

Browse files
committed
fix deduplication logic
1 parent 86dc8f7 commit 9df1567

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/spreadsheet.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ const performUpdate = async (params: UpdateParams) => {
184184
};
185185

186186
async function processGroupedUpdates(updates: UpdateParams[]) {
187+
// If we have multiple updates for the same cell, we only need to perform the last one
187188
deduplicate(updates, ({ x, y }) => CellIdMap.getCellIndex(y, x));
188189
await Promise.all(updates.map(performUpdate));
189190
}
@@ -362,9 +363,13 @@ function deduplicate<T, Y>(arr: T[], getKey: (item: T) => Y): void {
362363
for (let readAt = 0; readAt < arr.length; readAt++) {
363364
const item = arr[readAt];
364365
const key = getKey(item);
365-
const writePos = keyPositions.get(key) ?? writeAt++;
366-
keyPositions.set(key, readAt);
367-
arr[writePos] = item;
366+
const keyPos = keyPositions.get(key);
367+
if (keyPos === undefined) {
368+
keyPositions.set(key, writeAt);
369+
arr[writeAt++] = item;
370+
} else {
371+
arr[keyPos] = item;
372+
}
368373
}
369374

370375
arr.length = writeAt;

0 commit comments

Comments
 (0)