Description
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();
}
});