AppendPrepend.cs 4.9 KB

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