Repeat.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element, int count)
  14. {
  15. if (count < 0)
  16. throw new ArgumentOutOfRangeException(nameof(count));
  17. return Enumerable.Repeat(element, count)
  18. .ToAsyncEnumerable();
  19. }
  20. public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element)
  21. {
  22. return CreateEnumerable(
  23. () =>
  24. {
  25. return CreateEnumerator(
  26. ct => TaskExt.True,
  27. () => element,
  28. () => { }
  29. );
  30. });
  31. }
  32. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source, int count)
  33. {
  34. if (source == null)
  35. throw new ArgumentNullException(nameof(source));
  36. if (count < 0)
  37. throw new ArgumentOutOfRangeException(nameof(count));
  38. return CreateEnumerable(
  39. () =>
  40. {
  41. var e = default(IAsyncEnumerator<TSource>);
  42. var a = new AssignableDisposable();
  43. var n = count;
  44. var current = default(TSource);
  45. var cts = new CancellationTokenDisposable();
  46. var d = Disposable.Create(cts, a);
  47. var f = default(Func<CancellationToken, Task<bool>>);
  48. f = async ct =>
  49. {
  50. if (e == null)
  51. {
  52. if (n-- == 0)
  53. {
  54. return false;
  55. }
  56. e = source.GetEnumerator();
  57. a.Disposable = e;
  58. }
  59. if (await e.MoveNext(ct)
  60. .ConfigureAwait(false))
  61. {
  62. current = e.Current;
  63. return true;
  64. }
  65. e = null;
  66. return await f(ct)
  67. .ConfigureAwait(false);
  68. };
  69. return CreateEnumerator(
  70. f,
  71. () => current,
  72. d.Dispose,
  73. e
  74. );
  75. });
  76. }
  77. public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source)
  78. {
  79. if (source == null)
  80. throw new ArgumentNullException(nameof(source));
  81. return CreateEnumerable(
  82. () =>
  83. {
  84. var e = default(IAsyncEnumerator<TSource>);
  85. var a = new AssignableDisposable();
  86. var current = default(TSource);
  87. var cts = new CancellationTokenDisposable();
  88. var d = Disposable.Create(cts, a);
  89. var f = default(Func<CancellationToken, Task<bool>>);
  90. f = async ct =>
  91. {
  92. if (e == null)
  93. {
  94. e = source.GetEnumerator();
  95. a.Disposable = e;
  96. }
  97. if (await e.MoveNext(ct)
  98. .ConfigureAwait(false))
  99. {
  100. current = e.Current;
  101. return true;
  102. }
  103. e = null;
  104. return await f(ct)
  105. .ConfigureAwait(false);
  106. };
  107. return CreateEnumerator(
  108. f,
  109. () => current,
  110. d.Dispose,
  111. e
  112. );
  113. });
  114. }
  115. }
  116. }