1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- // 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<TSource> IgnoreElements<TSource>(this IAsyncEnumerable<TSource> source)
- {
- if (source == null)
- {
- throw new ArgumentNullException(nameof(source));
- }
- return new IgnoreElementsAsyncIterator<TSource>(source);
- }
- private sealed class IgnoreElementsAsyncIterator<TSource> : AsyncIterator<TSource>
- {
- private readonly IAsyncEnumerable<TSource> source;
- private IAsyncEnumerator<TSource> enumerator;
- public IgnoreElementsAsyncIterator(IAsyncEnumerable<TSource> source)
- {
- Debug.Assert(source != null);
- this.source = source;
- }
- public override AsyncIterator<TSource> Clone()
- {
- return new IgnoreElementsAsyncIterator<TSource>(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<bool> MoveNextCore(CancellationToken cancellationToken)
- {
- 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;
- }
- }
- }
- }
|