about summary refs log tree commit diff
path: root/src/lib/async/accumulate.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/async/accumulate.ts')
-rw-r--r--src/lib/async/accumulate.ts25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/lib/async/accumulate.ts b/src/lib/async/accumulate.ts
new file mode 100644
index 000000000..99226418e
--- /dev/null
+++ b/src/lib/async/accumulate.ts
@@ -0,0 +1,25 @@
+export interface AccumulateResponse<T> {
+  cursor?: string
+  items: T[]
+}
+
+export type AccumulateFetchFn<T> = (
+  cursor: string | undefined,
+) => Promise<AccumulateResponse<T>>
+
+export async function accumulate<T>(
+  fn: AccumulateFetchFn<T>,
+  pageLimit = 100,
+): Promise<T[]> {
+  let cursor: string | undefined
+  let acc: T[] = []
+  for (let i = 0; i < pageLimit; i++) {
+    const res = await fn(cursor)
+    cursor = res.cursor
+    acc = acc.concat(res.items)
+    if (!cursor) {
+      break
+    }
+  }
+  return acc
+}