// 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; namespace System.Linq { public static partial class AsyncEnumerableEx { public static IAsyncEnumerable IgnoreElements(this IAsyncEnumerable source) { if (source == null) throw Error.ArgumentNull(nameof(source)); #if USE_ASYNC_ITERATOR return AsyncEnumerable.Create(Core); async IAsyncEnumerator Core(CancellationToken cancellationToken) { await foreach (var _ in AsyncEnumerableExtensions.WithCancellation(source, cancellationToken).ConfigureAwait(false)) { } yield break; } #else return new IgnoreElementsAsyncIterator(source); #endif } #if !USE_ASYNC_ITERATOR private sealed class IgnoreElementsAsyncIterator : AsyncIterator { private readonly IAsyncEnumerable _source; private IAsyncEnumerator _enumerator; public IgnoreElementsAsyncIterator(IAsyncEnumerable source) { Debug.Assert(source != null); _source = source; } public override AsyncIteratorBase Clone() { return new IgnoreElementsAsyncIterator(_source); } public override async ValueTask DisposeAsync() { if (_enumerator != null) { await _enumerator.DisposeAsync().ConfigureAwait(false); _enumerator = null; } await base.DisposeAsync().ConfigureAwait(false); } protected override async ValueTask MoveNextCore() { switch (_state) { case AsyncIteratorState.Allocated: _enumerator = _source.GetAsyncEnumerator(_cancellationToken); _state = AsyncIteratorState.Iterating; goto case AsyncIteratorState.Iterating; case AsyncIteratorState.Iterating: while (await _enumerator.MoveNextAsync().ConfigureAwait(false)) { } break; // case } await DisposeAsync().ConfigureAwait(false); return false; } } #endif } }