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

Ix: Do not dispose the enumerator while enumerating in the ToObservable operator. #915

Merged
merged 9 commits into from Nov 20, 2019
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Threading.Tasks;

namespace System.Linq
{
Expand Down Expand Up @@ -32,30 +31,35 @@ public IDisposable Subscribe(IObserver<T> observer)

async void Core()
{
try
// REVIEW: fire-and-forget DisposeAsync?
await using (var e = _source.GetAsyncEnumerator(ctd.Token))
quinmars marked this conversation as resolved.
Show resolved Hide resolved
{
await foreach (var element in _source.WithCancellation(ctd.Token).ConfigureAwait(false))
do
{
observer.OnNext(element);
bool hasNext;
try
{
hasNext = await e.MoveNextAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
if (!ctd.Token.IsCancellationRequested)
{
observer.OnError(ex);
}

if (ctd.Token.IsCancellationRequested)
return;
}

if (!hasNext)
{
observer.OnCompleted();
return;
}
}
}
catch (Exception error)
{
if (!ctd.Token.IsCancellationRequested)
{
observer.OnError(error);
}
return;
}

if (!ctd.Token.IsCancellationRequested)
{
observer.OnCompleted();
observer.OnNext(e.Current);
quinmars marked this conversation as resolved.
Show resolved Hide resolved
}
while (!ctd.Token.IsCancellationRequested);
}
}

Expand Down