All files / src/utils liveEvents.js

100% Statements 30/30
100% Branches 12/12
100% Functions 2/2
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 341x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x   9x 9x 15x 15x 9x   9x 9x  
/**
 * Live activity stream helpers (SB-1329).
 *
 * Chat no longer streams over Realtime: clients cannot read chat rows. When
 * a match's chat changes, the client re-reads the newest page of events from
 * the API and merges it into what it already shows.
 */
 
/**
 * Merge a freshly fetched newest-first page into the current list.
 *
 * Inside the page's window the fetch is authoritative: rows it returns are
 * added or replaced, and rows it no longer returns (deleted messages) are
 * dropped. Older rows outside the window — pages loaded with "Load more" —
 * are kept as they are. A page shorter than `limit` is the whole stream, so
 * its window is everything.
 */
export function mergeEventWindow(current, fetched, limit) {
  const fresh = Array.isArray(fetched) ? fetched : [];
  const complete = fresh.length < limit;
  const oldestFetched = fresh.length
    ? Math.min(...fresh.map(e => e.id))
    : -Infinity;
  const inWindow = event => complete || event.id >= oldestFetched;
 
  const byId = new Map();
  for (const event of current) {
    if (!inWindow(event)) byId.set(event.id, event);
  }
  for (const event of fresh) byId.set(event.id, event);
 
  return [...byId.values()].sort((a, b) => b.id - a.id);
}