DefaultIfEmpty.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 IUniTaskAsyncEnumerable<TSource> DefaultIfEmpty<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return new DefaultIfEmpty<TSource>(source, default);
  12. }
  13. public static IUniTaskAsyncEnumerable<TSource> DefaultIfEmpty<TSource>(this IUniTaskAsyncEnumerable<TSource> source, TSource defaultValue)
  14. {
  15. Error.ThrowArgumentNullException(source, nameof(source));
  16. return new DefaultIfEmpty<TSource>(source, defaultValue);
  17. }
  18. }
  19. internal sealed class DefaultIfEmpty<TSource> : IUniTaskAsyncEnumerable<TSource>
  20. {
  21. readonly IUniTaskAsyncEnumerable<TSource> source;
  22. readonly TSource defaultValue;
  23. public DefaultIfEmpty(IUniTaskAsyncEnumerable<TSource> source, TSource defaultValue)
  24. {
  25. this.source = source;
  26. this.defaultValue = defaultValue;
  27. }
  28. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  29. {
  30. return new _DefaultIfEmpty(source, defaultValue, cancellationToken);
  31. }
  32. sealed class _DefaultIfEmpty : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
  33. {
  34. enum IteratingState : byte
  35. {
  36. Empty,
  37. Iterating,
  38. Completed
  39. }
  40. static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
  41. readonly IUniTaskAsyncEnumerable<TSource> source;
  42. readonly TSource defaultValue;
  43. CancellationToken cancellationToken;
  44. IteratingState iteratingState;
  45. IUniTaskAsyncEnumerator<TSource> enumerator;
  46. UniTask<bool>.Awaiter awaiter;
  47. public _DefaultIfEmpty(IUniTaskAsyncEnumerable<TSource> source, TSource defaultValue, CancellationToken cancellationToken)
  48. {
  49. this.source = source;
  50. this.defaultValue = defaultValue;
  51. this.cancellationToken = cancellationToken;
  52. this.iteratingState = IteratingState.Empty;
  53. TaskTracker.TrackActiveTask(this, 3);
  54. }
  55. public TSource Current { get; private set; }
  56. public UniTask<bool> MoveNextAsync()
  57. {
  58. cancellationToken.ThrowIfCancellationRequested();
  59. completionSource.Reset();
  60. if (iteratingState == IteratingState.Completed)
  61. {
  62. return CompletedTasks.False;
  63. }
  64. if (enumerator == null)
  65. {
  66. enumerator = source.GetAsyncEnumerator(cancellationToken);
  67. }
  68. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  69. if (awaiter.IsCompleted)
  70. {
  71. MoveNextCore(this);
  72. }
  73. else
  74. {
  75. awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
  76. }
  77. return new UniTask<bool>(this, completionSource.Version);
  78. }
  79. static void MoveNextCore(object state)
  80. {
  81. var self = (_DefaultIfEmpty)state;
  82. if (self.TryGetResult(self.awaiter, out var result))
  83. {
  84. if (result)
  85. {
  86. self.iteratingState = IteratingState.Iterating;
  87. self.Current = self.enumerator.Current;
  88. self.completionSource.TrySetResult(true);
  89. }
  90. else
  91. {
  92. if (self.iteratingState == IteratingState.Empty)
  93. {
  94. self.iteratingState = IteratingState.Completed;
  95. self.Current = self.defaultValue;
  96. self.completionSource.TrySetResult(true);
  97. }
  98. else
  99. {
  100. self.completionSource.TrySetResult(false);
  101. }
  102. }
  103. }
  104. }
  105. public UniTask DisposeAsync()
  106. {
  107. TaskTracker.RemoveTracking(this);
  108. if (enumerator != null)
  109. {
  110. return enumerator.DisposeAsync();
  111. }
  112. return default;
  113. }
  114. }
  115. }
  116. }