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

Feature Request: Queries that return IAsyncEnumerable To Iterate Through Cursors #290

Open
bbrandt opened this issue Aug 16, 2022 · 4 comments
Labels
PR welcome Please help out with this issue

Comments

@bbrandt
Copy link
Contributor

bbrandt commented Aug 16, 2022

Please provide either an extension method to convert ItemsWithCursor<T> to an IAsyncEnumerable<T> for any method or another set of methods for each query returning a cursor such as

public async IAsyncEnumerable<TimeSeries> ListAllAsync(TimeSeriesQuery query, [EnumeratorCancellation] CancellationToken token = default)
@bbrandt
Copy link
Contributor Author

bbrandt commented Aug 16, 2022

@bbrandt
Copy link
Contributor Author

bbrandt commented Aug 16, 2022

My completely untested attempt in my layer wrapping the Cognite SDK:

        public static async IAsyncEnumerable<TResult> AsAsyncEnumerable<TResult, TQuery>(
            Func<TQuery, CancellationToken, Task<ItemsWithCursor<TResult>>> listAsync,
            TQuery query,
            [EnumeratorCancellation] CancellationToken token)
            where TQuery : CursorQueryBase
        {
            var itemsWithCursor = await listAsync(query, token);
            foreach (var item in itemsWithCursor.Items)
            {
                yield return item;
            }

            while (!string.IsNullOrEmpty(itemsWithCursor.NextCursor))
            {
                query.Cursor = itemsWithCursor.NextCursor;

                itemsWithCursor = await listAsync(query, token);

                foreach (var item in itemsWithCursor.Items)
                {
                    yield return item;
                }
            }
        }

        public IAsyncEnumerable<TimeSeries> ListAllAsync(TimeSeriesQuery query, CancellationToken token = default)
        {
            return AsAsyncEnumerable(ListAsync, query, token);
        }

Feel free to sprinkle in your own F#/Oryx magic.
https://github.com/fsprojects/FSharp.Control.AsyncSeq
fsprojects/FSharp.Control.AsyncSeq#96

@polomani polomani added the PR welcome Please help out with this issue label Aug 17, 2022
@bbrandt
Copy link
Contributor Author

bbrandt commented Aug 18, 2022

Thanks for the tag, @polomani. Can you give a bit more detail around what would be expected from a PR from the community? Does the implementation need to follow the current pattern of being written in Oryx with F# and having a thin C# wrapper around it or would we be good with a C#-only implementation in this case?

@polomani
Copy link
Collaborator

@bbrandt I assume your use-case requires C#? In that case, feel free to make a pure c# implementation.
We can then move this into Oryx when we feel a need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR welcome Please help out with this issue
Projects
None yet
Development

No branches or pull requests

2 participants