ScheduledAsyncObserverBase.cs 4.4 KB

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