ForEach.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static UniTask ForEachAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken = default)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. Error.ThrowArgumentNullException(action, nameof(action));
  12. return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAsync(source, action, cancellationToken);
  13. }
  14. public static UniTask ForEachAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Action<TSource, Int32> action, CancellationToken cancellationToken = default)
  15. {
  16. Error.ThrowArgumentNullException(source, nameof(source));
  17. Error.ThrowArgumentNullException(action, nameof(action));
  18. return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAsync(source, action, cancellationToken);
  19. }
  20. /// <summary>Obsolete(Error), Use Use ForEachAwaitAsync instead.</summary>
  21. [Obsolete("Use ForEachAwaitAsync instead.", true)]
  22. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
  23. public static UniTask ForEachAsync<T>(this IUniTaskAsyncEnumerable<T> source, Func<T, UniTask> action, CancellationToken cancellationToken = default)
  24. {
  25. throw new NotSupportedException("Use ForEachAwaitAsync instead.");
  26. }
  27. /// <summary>Obsolete(Error), Use Use ForEachAwaitAsync instead.</summary>
  28. [Obsolete("Use ForEachAwaitAsync instead.", true)]
  29. [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
  30. public static UniTask ForEachAsync<T>(this IUniTaskAsyncEnumerable<T> source, Func<T, int, UniTask> action, CancellationToken cancellationToken = default)
  31. {
  32. throw new NotSupportedException("Use ForEachAwaitAsync instead.");
  33. }
  34. public static UniTask ForEachAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> action, CancellationToken cancellationToken = default)
  35. {
  36. Error.ThrowArgumentNullException(source, nameof(source));
  37. Error.ThrowArgumentNullException(action, nameof(action));
  38. return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitAsync(source, action, cancellationToken);
  39. }
  40. public static UniTask ForEachAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask> action, CancellationToken cancellationToken = default)
  41. {
  42. Error.ThrowArgumentNullException(source, nameof(source));
  43. Error.ThrowArgumentNullException(action, nameof(action));
  44. return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitAsync(source, action, cancellationToken);
  45. }
  46. public static UniTask ForEachAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> action, CancellationToken cancellationToken = default)
  47. {
  48. Error.ThrowArgumentNullException(source, nameof(source));
  49. Error.ThrowArgumentNullException(action, nameof(action));
  50. return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitWithCancellationAsync(source, action, cancellationToken);
  51. }
  52. public static UniTask ForEachAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask> action, CancellationToken cancellationToken = default)
  53. {
  54. Error.ThrowArgumentNullException(source, nameof(source));
  55. Error.ThrowArgumentNullException(action, nameof(action));
  56. return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAwaitWithCancellationAsync(source, action, cancellationToken);
  57. }
  58. }
  59. internal static class ForEach
  60. {
  61. public static async UniTask ForEachAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken)
  62. {
  63. var e = source.GetAsyncEnumerator(cancellationToken);
  64. try
  65. {
  66. while (await e.MoveNextAsync())
  67. {
  68. action(e.Current);
  69. }
  70. }
  71. finally
  72. {
  73. if (e != null)
  74. {
  75. await e.DisposeAsync();
  76. }
  77. }
  78. }
  79. public static async UniTask ForEachAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Action<TSource, Int32> action, CancellationToken cancellationToken)
  80. {
  81. var e = source.GetAsyncEnumerator(cancellationToken);
  82. try
  83. {
  84. int index = 0;
  85. while (await e.MoveNextAsync())
  86. {
  87. action(e.Current, checked(index++));
  88. }
  89. }
  90. finally
  91. {
  92. if (e != null)
  93. {
  94. await e.DisposeAsync();
  95. }
  96. }
  97. }
  98. public static async UniTask ForEachAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> action, CancellationToken cancellationToken)
  99. {
  100. var e = source.GetAsyncEnumerator(cancellationToken);
  101. try
  102. {
  103. while (await e.MoveNextAsync())
  104. {
  105. await action(e.Current);
  106. }
  107. }
  108. finally
  109. {
  110. if (e != null)
  111. {
  112. await e.DisposeAsync();
  113. }
  114. }
  115. }
  116. public static async UniTask ForEachAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, UniTask> action, CancellationToken cancellationToken)
  117. {
  118. var e = source.GetAsyncEnumerator(cancellationToken);
  119. try
  120. {
  121. int index = 0;
  122. while (await e.MoveNextAsync())
  123. {
  124. await action(e.Current, checked(index++));
  125. }
  126. }
  127. finally
  128. {
  129. if (e != null)
  130. {
  131. await e.DisposeAsync();
  132. }
  133. }
  134. }
  135. public static async UniTask ForEachAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask> action, CancellationToken cancellationToken)
  136. {
  137. var e = source.GetAsyncEnumerator(cancellationToken);
  138. try
  139. {
  140. while (await e.MoveNextAsync())
  141. {
  142. await action(e.Current, cancellationToken);
  143. }
  144. }
  145. finally
  146. {
  147. if (e != null)
  148. {
  149. await e.DisposeAsync();
  150. }
  151. }
  152. }
  153. public static async UniTask ForEachAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Int32, CancellationToken, UniTask> action, CancellationToken cancellationToken)
  154. {
  155. var e = source.GetAsyncEnumerator(cancellationToken);
  156. try
  157. {
  158. int index = 0;
  159. while (await e.MoveNextAsync())
  160. {
  161. await action(e.Current, checked(index++), cancellationToken);
  162. }
  163. }
  164. finally
  165. {
  166. if (e != null)
  167. {
  168. await e.DisposeAsync();
  169. }
  170. }
  171. }
  172. }
  173. }