ScheduledAsyncObserverBase.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Reactive.Threading;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Reactive
  9. {
  10. internal abstract class ScheduledAsyncObserverBase<T> : AsyncObserverBase<T>, IScheduledAsyncObserver<T>
  11. {
  12. private readonly IAsyncObserver<T> _observer;
  13. private readonly IAsyncGate _lock = AsyncGate.Create();
  14. private readonly Queue<T> _queue = new();
  15. private bool _hasFaulted = false;
  16. private bool _busy = false;
  17. private bool _done;
  18. private Exception _error;
  19. public ScheduledAsyncObserverBase(IAsyncObserver<T> observer)
  20. {
  21. _observer = observer;
  22. }
  23. public ValueTask EnsureActive() => EnsureActive(1);
  24. public async ValueTask EnsureActive(int count)
  25. {
  26. var shouldRun = false;
  27. using (await _lock.LockAsync().ConfigureAwait(false))
  28. {
  29. if (!_hasFaulted && !_busy)
  30. {
  31. _busy = true;
  32. shouldRun = true;
  33. }
  34. }
  35. if (shouldRun)
  36. {
  37. await ScheduleAsync().ConfigureAwait(false);
  38. }
  39. }
  40. protected abstract ValueTask ScheduleAsync();
  41. protected async ValueTask RunAsync(CancellationToken token)
  42. {
  43. while (!token.IsCancellationRequested)
  44. {
  45. var values = default(T[]);
  46. var error = default(Exception);
  47. var done = false;
  48. using (await RendezVous(_lock.LockAsync()))
  49. {
  50. if (_queue.Count > 0)
  51. {
  52. values = _queue.ToArray();
  53. _queue.Clear();
  54. }
  55. if (_done)
  56. {
  57. done = _done;
  58. error = _error;
  59. }
  60. if (values == null && !done)
  61. {
  62. _busy = false;
  63. break;
  64. }
  65. }
  66. try
  67. {
  68. if (values != null)
  69. {
  70. foreach (var value in values)
  71. {
  72. await RendezVous(_observer.OnNextAsync(value));
  73. }
  74. }
  75. if (done)
  76. {
  77. if (error != null)
  78. {
  79. await RendezVous(_observer.OnErrorAsync(error));
  80. }
  81. else
  82. {
  83. await RendezVous(_observer.OnCompletedAsync());
  84. }
  85. break;
  86. }
  87. }
  88. catch
  89. {
  90. using (await RendezVous(_lock.LockAsync()))
  91. {
  92. _hasFaulted = true;
  93. _queue.Clear();
  94. }
  95. throw;
  96. }
  97. }
  98. }
  99. protected override async ValueTask OnCompletedAsyncCore()
  100. {
  101. using (await _lock.LockAsync().ConfigureAwait(false))
  102. {
  103. if (!_hasFaulted)
  104. {
  105. _done = true;
  106. }
  107. }
  108. }
  109. protected override async ValueTask OnErrorAsyncCore(Exception error)
  110. {
  111. using (await _lock.LockAsync().ConfigureAwait(false))
  112. {
  113. if (!_hasFaulted)
  114. {
  115. _done = true;
  116. _error = error;
  117. }
  118. }
  119. }
  120. protected override async ValueTask OnNextAsyncCore(T value)
  121. {
  122. using (await _lock.LockAsync().ConfigureAwait(false))
  123. {
  124. if (!_hasFaulted)
  125. {
  126. _queue.Enqueue(value);
  127. }
  128. }
  129. }
  130. protected abstract ValueTaskAwaitable RendezVous(ValueTask task);
  131. protected abstract ValueTaskAwaitable<R> RendezVous<R>(ValueTask<R> task);
  132. public abstract ValueTask DisposeAsync();
  133. }
  134. }