// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Diagnostics; using System.Threading; using System.Threading.Tasks; using static System.Linq.AsyncEnumerable; namespace System.Linq { public static partial class AsyncEnumerableEx { public static IAsyncEnumerable Defer(Func> factory) { if (factory == null) throw Error.ArgumentNull(nameof(factory)); return CreateEnumerable(ct => factory().GetAsyncEnumerator(ct)); } public static IAsyncEnumerable Defer(Func>> factory) { if (factory == null) throw Error.ArgumentNull(nameof(factory)); return new AnonymousAsyncEnumerableWithTask(async ct => (await factory().ConfigureAwait(false)).GetAsyncEnumerator(ct)); } private sealed class AnonymousAsyncEnumerableWithTask : IAsyncEnumerable { private readonly Func>> _getEnumerator; public AnonymousAsyncEnumerableWithTask(Func>> getEnumerator) { Debug.Assert(getEnumerator != null); _getEnumerator = getEnumerator; } public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken) => new Enumerator(_getEnumerator, cancellationToken); private sealed class Enumerator : IAsyncEnumerator { private Func>> _getEnumerator; private readonly CancellationToken _cancellationToken; private IAsyncEnumerator _enumerator; public Enumerator(Func>> getEnumerator, CancellationToken cancellationToken) { Debug.Assert(getEnumerator != null); _getEnumerator = getEnumerator; _cancellationToken = cancellationToken; } public T Current { get { if (_enumerator == null) throw new InvalidOperationException(); return _enumerator.Current; } } public async ValueTask DisposeAsync() { var old = Interlocked.Exchange(ref _enumerator, DisposedEnumerator.Instance); if (_enumerator != null) { await _enumerator.DisposeAsync().ConfigureAwait(false); } } public ValueTask MoveNextAsync() { if (_enumerator == null) { return InitAndMoveNextAsync(); } return _enumerator.MoveNextAsync(); } private async ValueTask InitAndMoveNextAsync() { try { _enumerator = await _getEnumerator(_cancellationToken).ConfigureAwait(false); } catch (Exception ex) { _enumerator = Throw(ex).GetAsyncEnumerator(_cancellationToken); throw; } finally { _getEnumerator = null; } return await _enumerator.MoveNextAsync().ConfigureAwait(false); } private sealed class DisposedEnumerator : IAsyncEnumerator { public static readonly DisposedEnumerator Instance = new DisposedEnumerator(); public T Current => throw new ObjectDisposedException("this"); public ValueTask DisposeAsync() => default; public ValueTask MoveNextAsync() => throw new ObjectDisposedException("this"); } } } } }