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

Replace tail recursion with while loop #1088

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -269,6 +269,34 @@ public void ToObservable_DesNotCallMoveNextAgainWhenSubscriptionIsDisposed()
Assert.Equal(1, moveNextCount);
Assert.False(fail);
}

[Fact]
public void ToObservable_SupportsLargeEnumerable()
{
using var evt = new ManualResetEvent(false);

var fail = false;

var xs = AsyncEnumerable.Range(0, 10000).ToObservable();
xs.Subscribe(new MyObserver<int>(
x =>
{
// ok
},
ex =>
{
fail = true;
evt.Set();
},
() =>
{
evt.Set();
}
));

evt.WaitOne();
Assert.False(fail);
}

private sealed class MyObserver<T> : IObserver<T>
{
Expand Down
Expand Up @@ -32,39 +32,44 @@ public IDisposable Subscribe(IObserver<T> observer)

async void Core()
{
bool hasNext;
try
while (true)
{
hasNext = await e.MoveNextAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
if (!ctd.Token.IsCancellationRequested)
bool hasNext;
try
{
observer.OnError(ex);
await e.DisposeAsync().ConfigureAwait(false);
hasNext = await e.MoveNextAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
if (!ctd.Token.IsCancellationRequested)
{
observer.OnError(ex);
await e.DisposeAsync().ConfigureAwait(false);
}

return;
}
return;
}

if (hasNext)
{
observer.OnNext(e.Current);
if (hasNext)
{
observer.OnNext(e.Current);

if (!ctd.Token.IsCancellationRequested)
{
continue;
}

if (!ctd.Token.IsCancellationRequested)
// In case cancellation is requested, this could only have happened
// by disposing the returned composite disposable (see below).
// In that case, e will be disposed too, so there is no need to dispose e here.
}
else
{
Core();
observer.OnCompleted();
await e.DisposeAsync().ConfigureAwait(false);
}

// In case cancellation is requested, this could only have happened
// by disposing the returned composite disposable (see below).
// In that case, e will be disposed too, so there is no need to dispose e here.
}
else
{
observer.OnCompleted();
await e.DisposeAsync().ConfigureAwait(false);
break;
}
}

Expand Down