1 import chunk from '@proton/utils/chunk';
3 import { runChunksDelayed } from '../../helpers/promise';
11 const queryPages = async <T extends { Total: number }>(
12 requestPage: (page: number, pageSize: number) => Promise<T>,
13 { pageSize = 50, pagesPerChunk = 10, delayPerChunk = 100 }: Partial<Arguments> = {}
15 const firstPage = await requestPage(0, pageSize);
16 const n = Math.ceil((firstPage?.Total || 0) / pageSize) - 1; // First page already loaded
22 const pages = Array.from({ length: n }, (a, i) => i + 1);
23 const chunks = chunk(pages, pagesPerChunk);
26 chunks.length > 0 ? await runChunksDelayed(chunks, (idx) => requestPage(idx, pageSize), delayPerChunk) : [];
28 return [firstPage].concat(restPages);
31 export default queryPages;