IntroToRx: Creating Observable Sequences
- Dominant language
- C#
- Stars
- 7.2k
- Forks
- 798
- PR merge metrics
- No merged PRs in 30d
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:
```csharp
IObservable ReadFileLines(string path) =>
Observable.Create(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();
}
});
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.