Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

freiondrej
Copy link

@freiondrej freiondrej commented Sep 23, 2024

Changes

Allow traversing a paginable resource with constant memory usage.

Checklist

  • Apply one of following labels; major, minor, patch or skip-release
  • I've updated the documentation, or no changes were necessary
  • I've updated the tests, or no changes were necessary

@@ -201,3 +201,22 @@ export async function getPaginatedEntriesByHasMore<T extends Record<string, unkn

return resultArray
}

export async function traversePaginatedSourceByHasMore<
Copy link
Author

@freiondrej freiondrej Sep 23, 2024

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.

Comment on lines +204 to +205

export async function traversePaginatedSourceByHasMore<
Copy link
Collaborator

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

Comment on lines +210 to +211
processPage: (pageData: PaginatedResponse<T>['data']) => Promise<void>,
): Promise<void> {
Copy link
Collaborator

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants