FastImmediateAsyncObserver.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 sealed class FastImmediateAsyncObserver<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 FastImmediateAsyncObserver(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. while (true)
  37. {
  38. var values = default(T[]);
  39. var error = default(Exception);
  40. var done = false;
  41. using (await _lock.LockAsync().ConfigureAwait(false))
  42. {
  43. if (_queue.Count > 0)
  44. {
  45. values = _queue.ToArray();
  46. _queue.Clear();
  47. }
  48. if (_done)
  49. {
  50. done = _done;
  51. error = _error;
  52. }
  53. if (values == null && !done)
  54. {
  55. _busy = false;
  56. break;
  57. }
  58. }
  59. try
  60. {
  61. if (values != null)
  62. {
  63. foreach (var value in values)
  64. {
  65. await _observer.OnNextAsync(value).ConfigureAwait(false);
  66. }
  67. }
  68. if (done)
  69. {
  70. if (error != null)
  71. {
  72. await _observer.OnErrorAsync(error).ConfigureAwait(false);
  73. }
  74. else
  75. {
  76. await _observer.OnCompletedAsync().ConfigureAwait(false);
  77. }
  78. break;
  79. }
  80. }
  81. catch
  82. {
  83. using (await _lock.LockAsync().ConfigureAwait(false))
  84. {
  85. _hasFaulted = true;
  86. _queue.Clear();
  87. }
  88. throw;
  89. }
  90. }
  91. }
  92. }
  93. public async Task OnCompletedAsync()
  94. {
  95. using (await _lock.LockAsync().ConfigureAwait(false))
  96. {
  97. if (!_hasFaulted)
  98. {
  99. _done = true;
  100. }
  101. }
  102. }
  103. public async Task OnErrorAsync(Exception error)
  104. {
  105. using (await _lock.LockAsync().ConfigureAwait(false))
  106. {
  107. if (!_hasFaulted)
  108. {
  109. _done = true;
  110. _error = error;
  111. }
  112. }
  113. }
  114. public async Task OnNextAsync(T value)
  115. {
  116. using (await _lock.LockAsync().ConfigureAwait(false))
  117. {
  118. if (!_hasFaulted)
  119. {
  120. _queue.Enqueue(value);
  121. }
  122. }
  123. }
  124. }
  125. }