Repeat.cs 4.2 KB

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