IgnoreElements.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. /// <summary>
  12. /// Ignores all elements in an async-enumerable sequence leaving only the termination messages.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">Source sequence.</param>
  16. /// <returns>An empty async-enumerable sequence that signals termination, successful or exceptional, of the source sequence.</returns>
  17. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  18. public static IAsyncEnumerable<TSource> IgnoreElements<TSource>(this IAsyncEnumerable<TSource> source)
  19. {
  20. if (source == null)
  21. throw Error.ArgumentNull(nameof(source));
  22. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  23. return Core(source);
  24. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  25. #else
  26. return AsyncEnumerable.Create(Core);
  27. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  28. #endif
  29. {
  30. await foreach (var _ in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  31. {
  32. }
  33. yield break;
  34. }
  35. }
  36. }
  37. }