-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce traversePaginatedSourceByHasMore() #340
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,3 +201,22 @@ export async function getPaginatedEntriesByHasMore<T extends Record<string, unkn | |
|
||
return resultArray | ||
} | ||
|
||
export async function traversePaginatedSourceByHasMore< | ||
Comment on lines
+204
to
+205
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Could we add JSDoc as in the other methods? just to explain clearly how the method works and how it is intended to be used |
||
T extends Record<string, unknown>, | ||
>( | ||
pagination: OptionalPaginationParams, | ||
apiCall: (params: OptionalPaginationParams) => Promise<PaginatedResponse<T>>, | ||
processPage: (pageData: PaginatedResponse<T>['data']) => Promise<void>, | ||
): Promise<void> { | ||
Comment on lines
+210
to
+211
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Could be interesting to be able to return the results of the processing step? something like
An example usage could be to insert something on DB and return all new IDs. What do you think? |
||
let hasMore: boolean | undefined | ||
let currentCursor: string | undefined = pagination.after | ||
|
||
do { | ||
const pageResult = await apiCall({ ...pagination, after: currentCursor }) | ||
hasMore = pageResult.meta.hasMore | ||
currentCursor = pageResult.meta.cursor | ||
|
||
await processPage(pageResult.data) | ||
} while (hasMore) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it stands, this is pretty much just a copy of
getPaginatedEntriesByHasMore
, with an addition of the processPage() call.In the future, I can imagine something more parallelized, with apiCall being called in a promise pool as fast as the processPage() is able to process it (as I assume processPage will be the bottleneck), so that we wouldn't need to keep things in memory.
EDIT: https://superchargejs.com/docs/3.x/promise-pool#create-a-promise-pool-from-an-async-iterable might work nicely :) I'll try it if I get a chance, but cannot make any promises ATM.