All.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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<Boolean> AllAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken = default)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  12. return All.AllAsync(source, predicate, cancellationToken);
  13. }
  14. public static UniTask<Boolean> AllAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  15. {
  16. Error.ThrowArgumentNullException(source, nameof(source));
  17. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  18. return All.AllAwaitAsync(source, predicate, cancellationToken);
  19. }
  20. public static UniTask<Boolean> AllAwaitWithCancellationAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken = default)
  21. {
  22. Error.ThrowArgumentNullException(source, nameof(source));
  23. Error.ThrowArgumentNullException(predicate, nameof(predicate));
  24. return All.AllAwaitWithCancellationAsync(source, predicate, cancellationToken);
  25. }
  26. }
  27. internal static class All
  28. {
  29. internal static async UniTask<bool> AllAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, Boolean> predicate, CancellationToken cancellationToken)
  30. {
  31. var e = source.GetAsyncEnumerator(cancellationToken);
  32. try
  33. {
  34. while (await e.MoveNextAsync())
  35. {
  36. if (!predicate(e.Current))
  37. {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. finally
  44. {
  45. if (e != null)
  46. {
  47. await e.DisposeAsync();
  48. }
  49. }
  50. }
  51. internal static async UniTask<bool> AllAwaitAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
  52. {
  53. var e = source.GetAsyncEnumerator(cancellationToken);
  54. try
  55. {
  56. while (await e.MoveNextAsync())
  57. {
  58. if (!await predicate(e.Current))
  59. {
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. finally
  66. {
  67. if (e != null)
  68. {
  69. await e.DisposeAsync();
  70. }
  71. }
  72. }
  73. internal static async UniTask<bool> AllAwaitWithCancellationAsync<TSource>(IUniTaskAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, UniTask<Boolean>> predicate, CancellationToken cancellationToken)
  74. {
  75. var e = source.GetAsyncEnumerator(cancellationToken);
  76. try
  77. {
  78. while (await e.MoveNextAsync())
  79. {
  80. if (!await predicate(e.Current, cancellationToken))
  81. {
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. finally
  88. {
  89. if (e != null)
  90. {
  91. await e.DisposeAsync();
  92. }
  93. }
  94. }
  95. }
  96. }