First.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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<TSource> FirstAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return First.FirstAsync(source, cancellationToken, false);
  12. }
  13. public static UniTask<TSource> FirstAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
  14. {
  15. Error.ThrowArgumentNullException(source, nameof(source));
  16. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  17. return First.FirstAsync(source, predicate, cancellationToken, false);
  18. }
  19. public static UniTask<TSource> FirstAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  20. {
  21. Error.ThrowArgumentNullException(source, nameof(source));
  22. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  23. return First.FirstAwaitAsync(source, predicate, cancellationToken, false);
  24. }
  25. public static UniTask<TSource> FirstAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  26. {
  27. Error.ThrowArgumentNullException(source, nameof(source));
  28. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  29. return First.FirstAwaitWithCancellationAsync(source, predicate, cancellationToken, false);
  30. }
  31. public static UniTask<TSource> FirstOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  32. {
  33. Error.ThrowArgumentNullException(source, nameof(source));
  34. return First.FirstAsync(source, cancellationToken, true);
  35. }
  36. public static UniTask<TSource> FirstOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
  37. {
  38. Error.ThrowArgumentNullException(source, nameof(source));
  39. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  40. return First.FirstAsync(source, predicate, cancellationToken, true);
  41. }
  42. public static UniTask<TSource> FirstOrDefaultAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  43. {
  44. Error.ThrowArgumentNullException(source, nameof(source));
  45. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  46. return First.FirstAwaitAsync(source, predicate, cancellationToken, true);
  47. }
  48. public static UniTask<TSource> FirstOrDefaultAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  49. {
  50. Error.ThrowArgumentNullException(source, nameof(source));
  51. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  52. return First.FirstAwaitWithCancellationAsync(source, predicate, cancellationToken, true);
  53. }
  54. }
  55. internal static class First
  56. {
  57. public static async UniTask<TSource> FirstAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken, bool defaultIfEmpty)
  58. {
  59. var e = source.GetAsyncEnumerator(cancellationToken);
  60. try
  61. {
  62. if (await e.MoveNextAsync())
  63. {
  64. return e.Current;
  65. }
  66. else
  67. {
  68. if (defaultIfEmpty)
  69. {
  70. return default;
  71. }
  72. else
  73. {
  74. throw Error.NoElements();
  75. }
  76. }
  77. }
  78. finally
  79. {
  80. if (e != null)
  81. {
  82. await e.DisposeAsync();
  83. }
  84. }
  85. }
  86. public static async UniTask<TSource> FirstAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
  87. {
  88. var e = source.GetAsyncEnumerator(cancellationToken);
  89. try
  90. {
  91. while (await e.MoveNextAsync())
  92. {
  93. var v = e.Current;
  94. if (predicate(v))
  95. {
  96. return v;
  97. }
  98. }
  99. if (defaultIfEmpty)
  100. {
  101. return default;
  102. }
  103. else
  104. {
  105. throw Error.NoElements();
  106. }
  107. }
  108. finally
  109. {
  110. if (e != null)
  111. {
  112. await e.DisposeAsync();
  113. }
  114. }
  115. }
  116. public static async UniTask<TSource> FirstAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
  117. {
  118. var e = source.GetAsyncEnumerator(cancellationToken);
  119. try
  120. {
  121. while (await e.MoveNextAsync())
  122. {
  123. var v = e.Current;
  124. if (await predicate(v))
  125. {
  126. return v;
  127. }
  128. }
  129. if (defaultIfEmpty)
  130. {
  131. return default;
  132. }
  133. else
  134. {
  135. throw Error.NoElements();
  136. }
  137. }
  138. finally
  139. {
  140. if (e != null)
  141. {
  142. await e.DisposeAsync();
  143. }
  144. }
  145. }
  146. public static async UniTask<TSource> FirstAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
  147. {
  148. var e = source.GetAsyncEnumerator(cancellationToken);
  149. try
  150. {
  151. while (await e.MoveNextAsync())
  152. {
  153. var v = e.Current;
  154. if (await predicate(v, cancellationToken))
  155. {
  156. return v;
  157. }
  158. }
  159. if (defaultIfEmpty)
  160. {
  161. return default;
  162. }
  163. else
  164. {
  165. throw Error.NoElements();
  166. }
  167. }
  168. finally
  169. {
  170. if (e != null)
  171. {
  172. await e.DisposeAsync();
  173. }
  174. }
  175. }
  176. }
  177. }