15 lines
415 B
TypeScript
15 lines
415 B
TypeScript
|
import type { Entity } from "~/shared/types/common";
|
||
|
|
||
|
export function applyUpdatesToArray<T extends Entity>(updates: T[], entities: T[]) {
|
||
|
const idMap = new Map(entities.map((e, i) => [e.id, i]));
|
||
|
for (const update of updates) {
|
||
|
const index = idMap.get(update.id);
|
||
|
if (index !== undefined) {
|
||
|
entities[index] = update;
|
||
|
} else {
|
||
|
idMap.set(update.id, entities.length);
|
||
|
entities.push(update);
|
||
|
}
|
||
|
}
|
||
|
}
|