Publish.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 IConnectableUniTaskAsyncEnumerable<TSource> Publish<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
  9. {
  10. Error.ThrowArgumentNullException(source, nameof(source));
  11. return new Publish<TSource>(source);
  12. }
  13. }
  14. internal sealed class Publish<TSource> : IConnectableUniTaskAsyncEnumerable<TSource>
  15. {
  16. readonly IUniTaskAsyncEnumerable<TSource> source;
  17. readonly CancellationTokenSource cancellationTokenSource;
  18. TriggerEvent<TSource> trigger;
  19. IUniTaskAsyncEnumerator<TSource> enumerator;
  20. IDisposable connectedDisposable;
  21. bool isCompleted;
  22. public Publish(IUniTaskAsyncEnumerable<TSource> source)
  23. {
  24. this.source = source;
  25. this.cancellationTokenSource = new CancellationTokenSource();
  26. }
  27. public IDisposable Connect()
  28. {
  29. if (connectedDisposable != null) return connectedDisposable;
  30. if (enumerator == null)
  31. {
  32. enumerator = source.GetAsyncEnumerator(cancellationTokenSource.Token);
  33. }
  34. ConsumeEnumerator().Forget();
  35. connectedDisposable = new ConnectDisposable(cancellationTokenSource);
  36. return connectedDisposable;
  37. }
  38. async UniTaskVoid ConsumeEnumerator()
  39. {
  40. try
  41. {
  42. try
  43. {
  44. while (await enumerator.MoveNextAsync())
  45. {
  46. trigger.SetResult(enumerator.Current);
  47. }
  48. trigger.SetCompleted();
  49. }
  50. catch (Exception ex)
  51. {
  52. trigger.SetError(ex);
  53. }
  54. }
  55. finally
  56. {
  57. isCompleted = true;
  58. await enumerator.DisposeAsync();
  59. }
  60. }
  61. public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  62. {
  63. return new _Publish(this, cancellationToken);
  64. }
  65. sealed class ConnectDisposable : IDisposable
  66. {
  67. readonly CancellationTokenSource cancellationTokenSource;
  68. public ConnectDisposable(CancellationTokenSource cancellationTokenSource)
  69. {
  70. this.cancellationTokenSource = cancellationTokenSource;
  71. }
  72. public void Dispose()
  73. {
  74. this.cancellationTokenSource.Cancel();
  75. }
  76. }
  77. sealed class _Publish : MoveNextSource, IUniTaskAsyncEnumerator<TSource>, ITriggerHandler<TSource>
  78. {
  79. static readonly Action<object> CancelDelegate = OnCanceled;
  80. readonly Publish<TSource> parent;
  81. CancellationToken cancellationToken;
  82. CancellationTokenRegistration cancellationTokenRegistration;
  83. bool isDisposed;
  84. public _Publish(Publish<TSource> parent, CancellationToken cancellationToken)
  85. {
  86. if (cancellationToken.IsCancellationRequested) return;
  87. this.parent = parent;
  88. this.cancellationToken = cancellationToken;
  89. if (cancellationToken.CanBeCanceled)
  90. {
  91. this.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(CancelDelegate, this);
  92. }
  93. parent.trigger.Add(this);
  94. TaskTracker.TrackActiveTask(this, 3);
  95. }
  96. public TSource Current { get; private set; }
  97. ITriggerHandler<TSource> ITriggerHandler<TSource>.Prev { get; set; }
  98. ITriggerHandler<TSource> ITriggerHandler<TSource>.Next { get; set; }
  99. public UniTask<bool> MoveNextAsync()
  100. {
  101. cancellationToken.ThrowIfCancellationRequested();
  102. if (parent.isCompleted) return CompletedTasks.False;
  103. completionSource.Reset();
  104. return new UniTask<bool>(this, completionSource.Version);
  105. }
  106. static void OnCanceled(object state)
  107. {
  108. var self = (_Publish)state;
  109. self.completionSource.TrySetCanceled(self.cancellationToken);
  110. self.DisposeAsync().Forget();
  111. }
  112. public UniTask DisposeAsync()
  113. {
  114. if (!isDisposed)
  115. {
  116. isDisposed = true;
  117. TaskTracker.RemoveTracking(this);
  118. cancellationTokenRegistration.Dispose();
  119. parent.trigger.Remove(this);
  120. }
  121. return default;
  122. }
  123. public void OnNext(TSource value)
  124. {
  125. Current = value;
  126. completionSource.TrySetResult(true);
  127. }
  128. public void OnCanceled(CancellationToken cancellationToken)
  129. {
  130. completionSource.TrySetCanceled(cancellationToken);
  131. }
  132. public void OnCompleted()
  133. {
  134. completionSource.TrySetResult(false);
  135. }
  136. public void OnError(Exception ex)
  137. {
  138. completionSource.TrySetException(ex);
  139. }
  140. }
  141. }
  142. }