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

IntroToRx: Creating Observable Sequences #2183

Open
xoma-zver opened this issue Dec 18, 2024 · 0 comments
Open

IntroToRx: Creating Observable Sequences #2183

xoma-zver opened this issue Dec 18, 2024 · 0 comments

Comments

@xoma-zver
Copy link

Bug

Which library version?

The issue is in the documentation of the library, not in a specific version of the library itself.

What are the platform(s), environment(s) and related component version(s)?

Not applicable.

What is the use case or problem?

There is a typo in the documentation code snippet for reading file lines using Observable.Create.
https://github.com/dotnet/reactive/blob/main/Rx.NET/Documentation/IntroToRx/03_CreatingObservableSequences.md#observablecreate

What is the expected outcome?

The code snippet should correctly handle the cancellation token in the while loop.

What is the actual outcome?

The current code uses while (cancellationToken.IsCancellationRequested) which will break the loop when the cancellation is requested. It should use while (!cancellationToken.IsCancellationRequested) instead.

What is the stacktrace of the exception(s) if any?

Not applicable.

Do you have a code snippet or project that reproduces the problem?

Yes, here is the corrected code snippet:

IObservable<string> ReadFileLines(string path) =>
    Observable.Create<string>(async (observer, cancellationToken) =>
    {
        using (StreamReader reader = File.OpenText(path))
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                string? line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false);
                if (line is null)
                {
                    break;
                }

                observer.OnNext(line);
            }

            observer.OnCompleted();
        }
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant