11 lines
552 B
TypeScript
11 lines
552 B
TypeScript
import type { Activity } from "./types";
|
|
|
|
// History and live responses can overlap or arrive out of order. A persisted
|
|
// activity's seq is its identity; replay must not duplicate rows or token totals.
|
|
export function mergeActivities(current: Activity[], incoming: Activity[]): Activity[] {
|
|
const bySeq = new Map<number, Activity>();
|
|
for (const activity of current) bySeq.set(activity.seq, activity);
|
|
for (const activity of incoming) bySeq.set(activity.seq, activity);
|
|
return [...bySeq.values()].sort((left, right) => left.seq - right.seq);
|
|
}
|