Last.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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> LastAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return Last.LastAsync(source, cancellationToken, false);
  12. }
  13. public static UniTask<TSource> LastAsync<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 Last.LastAsync(source, predicate, cancellationToken, false);
  18. }
  19. public static UniTask<TSource> LastAwaitAsync<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 Last.LastAwaitAsync(source, predicate, cancellationToken, false);
  24. }
  25. public static UniTask<TSource> LastAwaitWithCancellationAsync<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 Last.LastAwaitWithCancellationAsync(source, predicate, cancellationToken, false);
  30. }
  31. public static UniTask<TSource> LastOrDefaultAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
  32. {
  33. Error.ThrowArgumentNullException(source, nameof(source));
  34. return Last.LastAsync(source, cancellationToken, true);
  35. }
  36. public static UniTask<TSource> LastOrDefaultAsync<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 Last.LastAsync(source, predicate, cancellationToken, true);
  41. }
  42. public static UniTask<TSource> LastOrDefaultAwaitAsync<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 Last.LastAwaitAsync(source, predicate, cancellationToken, true);
  47. }
  48. public static UniTask<TSource> LastOrDefaultAwaitWithCancellationAsync<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 Last.LastAwaitWithCancellationAsync(source, predicate, cancellationToken, true);
  53. }
  54. }
  55. internal static class Last
  56. {
  57. public static async UniTask<TSource> LastAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken, bool defaultIfEmpty)
  58. {
  59. var e = source.GetAsyncEnumerator(cancellationToken);
  60. try
  61. {
  62. TSource value = default;
  63. if (await e.MoveNextAsync())
  64. {
  65. value = e.Current;
  66. }
  67. else
  68. {
  69. if (defaultIfEmpty)
  70. {
  71. return value;
  72. }
  73. else
  74. {
  75. throw Error.NoElements();
  76. }
  77. }
  78. while (await e.MoveNextAsync())
  79. {
  80. value = e.Current;
  81. }
  82. return value;
  83. }
  84. finally
  85. {
  86. if (e != null)
  87. {
  88. await e.DisposeAsync();
  89. }
  90. }
  91. }
  92. public static async UniTask<TSource> LastAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
  93. {
  94. var e = source.GetAsyncEnumerator(cancellationToken);
  95. try
  96. {
  97. TSource value = default;
  98. bool found = false;
  99. while (await e.MoveNextAsync())
  100. {
  101. var v = e.Current;
  102. if (predicate(v))
  103. {
  104. found = true;
  105. value = v;
  106. }
  107. }
  108. if (defaultIfEmpty)
  109. {
  110. return value;
  111. }
  112. else
  113. {
  114. if (found)
  115. {
  116. return value;
  117. }
  118. else
  119. {
  120. throw Error.NoElements();
  121. }
  122. }
  123. }
  124. finally
  125. {
  126. if (e != null)
  127. {
  128. await e.DisposeAsync();
  129. }
  130. }
  131. }
  132. public static async UniTask<TSource> LastAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
  133. {
  134. var e = source.GetAsyncEnumerator(cancellationToken);
  135. try
  136. {
  137. TSource value = default;
  138. bool found = false;
  139. while (await e.MoveNextAsync())
  140. {
  141. var v = e.Current;
  142. if (await predicate(v))
  143. {
  144. found = true;
  145. value = v;
  146. }
  147. }
  148. if (defaultIfEmpty)
  149. {
  150. return value;
  151. }
  152. else
  153. {
  154. if (found)
  155. {
  156. return value;
  157. }
  158. else
  159. {
  160. throw Error.NoElements();
  161. }
  162. }
  163. }
  164. finally
  165. {
  166. if (e != null)
  167. {
  168. await e.DisposeAsync();
  169. }
  170. }
  171. }
  172. public static async UniTask<TSource> LastAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken, bool defaultIfEmpty)
  173. {
  174. var e = source.GetAsyncEnumerator(cancellationToken);
  175. try
  176. {
  177. TSource value = default;
  178. bool found = false;
  179. while (await e.MoveNextAsync())
  180. {
  181. var v = e.Current;
  182. if (await predicate(v, cancellationToken))
  183. {
  184. found = true;
  185. value = v;
  186. }
  187. }
  188. if (defaultIfEmpty)
  189. {
  190. return value;
  191. }
  192. else
  193. {
  194. if (found)
  195. {
  196. return value;
  197. }
  198. else
  199. {
  200. throw Error.NoElements();
  201. }
  202. }
  203. }
  204. finally
  205. {
  206. if (e != null)
  207. {
  208. await e.DisposeAsync();
  209. }
  210. }
  211. }
  212. }
  213. }