forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1export interface AccumulateResponse<T> {
2 cursor?: string
3 items: T[]
4}
5
6export type AccumulateFetchFn<T> = (
7 cursor: string | undefined,
8) => Promise<AccumulateResponse<T>>
9
10export async function accumulate<T>(
11 fn: AccumulateFetchFn<T>,
12 pageLimit = 100,
13): Promise<T[]> {
14 let cursor: string | undefined
15 let acc: T[] = []
16 for (let i = 0; i < pageLimit; i++) {
17 const res = await fn(cursor)
18 cursor = res.cursor
19 acc = acc.concat(res.items)
20 if (!cursor) {
21 break
22 }
23 }
24 return acc
25}