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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 375x 375x 1709x 371x 375x 4x 4x 4x 14x 4x 14x 4x 4x 4x 375x 197x 197x 19x 19x 57x 44x 19x 19x 47x 47x 5x 4x 47x 19x | /**
* Paging a feed merged from several independently paged sources.
*
* Each source is read newest first, a page at a time. Merging what has been
* loaded is a sort, but the result is only *complete* down to a horizon: if
* one source has loaded back to 10:00 and still has more, an item from
* another source at 09:59 cannot be shown yet, because the first source may
* hold rows between 10:00 and 09:59 that would belong above it. So the feed
* shows items at or after the horizon, and reaching further back means
* loading the next page of the source that set it.
*/
export interface SourceWindow {
/** Everything the server holds has been loaded. */
exhausted: boolean;
/** Epoch ms of the oldest loaded row, or null if nothing is loaded. */
oldest: number | null;
}
/**
* The oldest timestamp the merged feed is complete down to: the newest of
* the non-exhausted sources' oldest rows. `-Infinity` when every source is
* exhausted (everything is settled); `+Infinity` while a source that has
* more has loaded nothing yet (nothing is).
*/
export function settledHorizon(sources: readonly SourceWindow[]): number {
let horizon = -Infinity;
for (const source of sources) {
if (source.exhausted) continue;
horizon = Math.max(horizon, source.oldest ?? Infinity);
}
return horizon;
}
/**
* The index of the source whose next page moves the horizon back — the one
* that set it — or -1 when every source is exhausted.
*/
export function horizonSource(sources: readonly SourceWindow[]): number {
let index = -1;
let horizon = -Infinity;
sources.forEach((source, i) => {
if (source.exhausted) return;
const oldest = source.oldest ?? Infinity;
if (oldest > horizon) {
horizon = oldest;
index = i;
}
});
return index;
}
/** How many pages `count` items fill — never fewer than one, so an empty
* feed still has a page to be on. */
export function pageCount(count: number, pageSize: number): number {
return Math.max(1, Math.ceil(count / pageSize));
}
/** The items on a 1-based page. */
export function pageSlice<T>(
items: readonly T[],
page: number,
pageSize: number,
): T[] {
const start = (page - 1) * pageSize;
return items.slice(start, start + pageSize);
}
/**
* The page buttons to draw: always the first and last, the current page and
* its neighbours, and `"gap"` where a run is elided — `1 … 4 5 6 … 12`.
*/
export function pageNumbers(
page: number,
count: number,
siblings = 1,
): (number | "gap")[] {
const shown = new Set<number>([1, count]);
for (let p = page - siblings; p <= page + siblings; p++) {
if (p >= 1 && p <= count) shown.add(p);
}
const sorted = [...shown].sort((a, b) => a - b);
const out: (number | "gap")[] = [];
sorted.forEach((p, i) => {
const prev = sorted[i - 1];
if (prev !== undefined && p - prev > 1) {
// A gap of exactly one page is drawn as that page — "…" hiding a
// single number saves nothing.
if (p - prev === 2) out.push(prev + 1);
else out.push("gap");
}
out.push(p);
});
return out;
}
|