forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

capped-list.ts82 lines, 3.4 KiBHistoryRawDownload
1import type { Page, PageParams } from "@/api/client";
2
3/** Default first-load size for list pages that filter and page in memory. */
4export const LIST_CAP = 100;
5
6export type CappedList<T> = {
7 items: T[];
8 /** Row count on the server, before the cap. */
9 total: number;
10};
11
12/**
13 * Fetch the first `cap` rows of a list, or every row once the user asks for
14 * it. The cap keeps big lists cheap; `total` lets the page say when rows were
15 * left out instead of hiding them.
16 */
17export async function loadCappedList<T>(
18 fetch: (params?: PageParams) => Promise<Page<T>>,
19 cap: number,
20 all: boolean,
21): Promise<CappedList<T>> {
22 const page = await fetch(all ? undefined : { limit: cap });
23 const items = Array.isArray(page.items) ? page.items : [];
24 const total = typeof page.total === "number" ? Math.max(page.total, items.length) : items.length;
25 return { items, total };
26}
27
28export function isTruncated(list: CappedList<unknown> | undefined): boolean {
29 return !!list && list.total > list.items.length;
30}
31
32/** Requests in flight at once while a list's later pages are read. */
33const PAGE_CONCURRENCY = 3;
34
35/**
36 * Every row of a list the backend can only page (`limit` / `offset`, no
37 * search or filter): the first page names the total, the rest are read in
38 * parallel, in order. The page then searches, filters and pages in memory
39 * over the whole list, so there is no "first 100 of 161" cut to explain.
40 */
41export async function loadAllPages<T>(
42 fetch: (params?: PageParams) => Promise<Page<T>>,
43 pageSize: number = LIST_CAP,
44): Promise<T[]> {
45 const first = await fetch({ limit: pageSize, offset: 0 });
46 const items = Array.isArray(first.items) ? [...first.items] : [];
47 const total = typeof first.total === "number" ? first.total : items.length;
48 if (items.length < pageSize || items.length >= total) return items;
49 const offsets: number[] = [];
50 for (let offset = items.length; offset < total; offset += pageSize) offsets.push(offset);
51 const pages: T[][] = new Array(offsets.length);
52 for (let start = 0; start < offsets.length; start += PAGE_CONCURRENCY) {
53 const batch = offsets.slice(start, start + PAGE_CONCURRENCY);
54 const results = await Promise.all(batch.map((offset) => fetch({ limit: pageSize, offset })));
55 results.forEach((page, index) => { pages[start + index] = Array.isArray(page.items) ? page.items : []; });
56 }
57 return items.concat(...pages);
58}
59
60
61/**
62 * Fetch every row of an already-windowed list, following offsets until the
63 * envelope total. The window (a date range or filter set) is the caller's
64 * job — this only pages through what the server answers, so no blind cap
65 * decides which rows exist. Stops on an empty page so rows deleted mid-read
66 * cannot loop it forever.
67 */
68export async function loadWindowedList<T>(
69 fetch: (params?: PageParams) => Promise<Page<T>>,
70 pageSize: number,
71): Promise<CappedList<T>> {
72 const first = await fetch({ limit: pageSize, offset: 0 });
73 const items = Array.isArray(first.items) ? [...first.items] : [];
74 let total = typeof first.total === "number" ? Math.max(first.total, items.length) : items.length;
75 for (let offset = items.length; offset < total; offset = items.length) {
76 const page = await fetch({ limit: pageSize, offset });
77 if (!Array.isArray(page.items) || page.items.length === 0) break;
78 items.push(...page.items);
79 total = typeof page.total === "number" ? Math.max(page.total, items.length) : items.length;
80 }
81 return { items, total };
82}