Repeat.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /// Repeats the element indefinitely.
  13. /// </summary>
  14. /// <typeparam name="TResult">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="element">Element to repeat.</param>
  16. /// <returns>The async-enumerable sequence producing the element repeatedly and sequentially.</returns>
  17. public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element)
  18. {
  19. #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
  20. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  21. return Core(element);
  22. static async IAsyncEnumerable<TResult> Core(TResult element, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  23. #else
  24. return AsyncEnumerable.Create(Core);
  25. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  26. #endif
  27. {
  28. while (true)
  29. {
  30. cancellationToken.ThrowIfCancellationRequested();
  31. yield return element;
  32. }
  33. }
  34. #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
  35. }
  36. /// <summary>
  37. /// Repeats the async-enumerable sequence indefinitely.
  38. /// </summary>
  39. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  40. /// <param name="source">Observable sequence to repeat.</param>
  41. /// <returns>The async-enumerable sequence producing the elements of the given sequence repeatedly and sequentially.</returns>
  42. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  43. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source)
  44. {
  45. if (source == null)
  46. throw Error.ArgumentNull(nameof(source));
  47. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  48. return Core(source);
  49. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  50. #else
  51. return AsyncEnumerable.Create(Core);
  52. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  53. #endif
  54. {
  55. while (true)
  56. {
  57. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  58. {
  59. yield return item;
  60. }
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// Repeats the async-enumerable sequence a specified number of times.
  66. /// </summary>
  67. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  68. /// <param name="source">Observable sequence to repeat.</param>
  69. /// <param name="count">Number of times to repeat the sequence.</param>
  70. /// <returns>The async-enumerable sequence producing the elements of the given sequence repeatedly.</returns>
  71. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  72. /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero.</exception>
  73. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source, int count)
  74. {
  75. if (source == null)
  76. throw Error.ArgumentNull(nameof(source));
  77. if (count < 0)
  78. throw Error.ArgumentOutOfRange(nameof(count));
  79. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  80. return Core(source, count);
  81. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, int count, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  82. #else
  83. return AsyncEnumerable.Create(Core);
  84. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  85. #endif
  86. {
  87. for (var i = 0; i < count; i++)
  88. {
  89. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  90. {
  91. yield return item;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }