-
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?
Introduce traversePaginatedSourceByHasMore() #340
Conversation
@@ -201,3 +201,22 @@ export async function getPaginatedEntriesByHasMore<T extends Record<string, unkn | |||
|
|||
return resultArray | |||
} | |||
|
|||
export async function traversePaginatedSourceByHasMore< |
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.
|
||
export async function traversePaginatedSourceByHasMore< |
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.
🟢 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
processPage: (pageData: PaginatedResponse<T>['data']) => Promise<void>, | ||
): Promise<void> { |
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.
🟢 Could be interesting to be able to return the results of the processing step? something like
export async function traversePaginatedSourceByHasMore<
T extends Record<string, unknown>,
ReturnType = void,
>(
pagination: OptionalPaginationParams,
apiCall: (params: OptionalPaginationParams) => Promise<PaginatedResponse<T>>,
processPage: (pageData: PaginatedResponse<T>['data']) => Promise<ReturnType> | ReturnType,
): Promise<ReturnType> {
An example usage could be to insert something on DB and return all new IDs. What do you think?
Changes
Allow traversing a paginable resource with constant memory usage.
Checklist
major
,minor
,patch
orskip-release