Skip to content

Commit

Permalink
fix deduplication logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Jan 16, 2025
1 parent 86dc8f7 commit 9df1567
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/spreadsheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ const performUpdate = async (params: UpdateParams) => {
};

async function processGroupedUpdates(updates: UpdateParams[]) {
// If we have multiple updates for the same cell, we only need to perform the last one
deduplicate(updates, ({ x, y }) => CellIdMap.getCellIndex(y, x));
await Promise.all(updates.map(performUpdate));
}
Expand Down Expand Up @@ -362,9 +363,13 @@ function deduplicate<T, Y>(arr: T[], getKey: (item: T) => Y): void {
for (let readAt = 0; readAt < arr.length; readAt++) {
const item = arr[readAt];
const key = getKey(item);
const writePos = keyPositions.get(key) ?? writeAt++;
keyPositions.set(key, readAt);
arr[writePos] = item;
const keyPos = keyPositions.get(key);
if (keyPos === undefined) {
keyPositions.set(key, writeAt);
arr[writeAt++] = item;
} else {
arr[keyPos] = item;
}
}

arr.length = writeAt;
Expand Down

0 comments on commit 9df1567

Please sign in to comment.