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

The cursor in list result return should be actually called nextCursor #75

Closed
chrisli30 opened this issue Dec 16, 2024 · 3 comments
Closed
Labels
completed P2 Priority Level 2

Comments

@chrisli30
Copy link
Member

chrisli30 commented Dec 16, 2024

Problem

Currently it’s hard to detect the end of loop, using cursor for pagination.

Command to run:

AvaProtocol/ava-sdk-js#45
npx jest tests/getExecutions.test.ts -t "options.cursor works as pagination" (this test need modification for this issue)

If the cursor points to the id of the last element, we will need a hasMore: true to indicate whether there are more, otherwise we can’t tell if there is more.

In the current implementation, the cursor returns "" at the end, so it’s the cursor of the next element, which doesn’t exsit. However, in client’s code, if they take "" and pass it back into the loop, the list result will start over, retuning the 1st element of the list.

A working case would be, the current iteration takes the returned cursor value, and pass it into the next iteration, and the list retuns empty result.

If we renames the cursor to nextCursor, empty string could be the indicator that we have reached the end of the list; however, we need to fix the empty string causing the list to start over problem. Basically, the beginning of the list should be distinctive from the end.

Could we refer to some standard design for the implementation of the cursor?

@chrisli30 chrisli30 added the P2 Priority Level 2 label Dec 16, 2024
@v9n
Copy link
Member

v9n commented Dec 16, 2024

When the next cursor is empty we should stop.

Morallis does that

https://docs.moralis.com/web3-data-api/evm/reference/pagination

Every request will return a cursor that can be used to get the next result until there are no more results to return.

} while (cursor != "" && cursor != null);

We can do the same as well?

@chrisli30
Copy link
Member Author

Yes, we can do the same, but could you give it a try, their end of list is a null or ""?

@v9n
Copy link
Member

v9n commented Dec 27, 2024

Yes, we can do the same, but could you give it a try, their end of list is a null or ""?

They used "" .

CleanShot 2024-12-27 at 11 51 46

Regardless, I had implement hasMore. Moving forward, you can use it to see if you can load more or not. When hasMore is false, the cursor always empty(a blank string).
https://github.com/AvaProtocol/ava-sdk-js/pull/46/files#diff-03eace3589a555a22cc7fbf829b3a3cd9b5783349437d2ef867164da8d8d605cR109-R123

Essentially to load all you can do as below(pseudo code)

while (result.hasMore) {
  result = load({cursor: result.cursor})
}

@v9n v9n added the completed label Dec 27, 2024
@v9n v9n closed this as completed Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed P2 Priority Level 2
Projects
None yet
Development

No branches or pull requests

2 participants