AsyncSubject.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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.Reactive.Disposables;
  5. using System.Runtime.CompilerServices;
  6. using System.Threading;
  7. namespace System.Reactive.Subjects
  8. {
  9. /// <summary>
  10. /// Represents the result of an asynchronous operation.
  11. /// The last value before the OnCompleted notification, or the error received through OnError, is sent to all subscribed observers.
  12. /// </summary>
  13. /// <typeparam name="T">The type of the elements processed by the subject.</typeparam>
  14. public sealed class AsyncSubject<T> : SubjectBase<T>, INotifyCompletion
  15. {
  16. #region Fields
  17. private AsyncSubjectDisposable[] _observers;
  18. private T _value;
  19. private bool _hasValue;
  20. private Exception _exception;
  21. /// <summary>
  22. /// A pre-allocated empty array for the no-observers state.
  23. /// </summary>
  24. private static readonly AsyncSubjectDisposable[] EMPTY = new AsyncSubjectDisposable[0];
  25. /// <summary>
  26. /// A pre-allocated empty array indicating the AsyncSubject has terminated
  27. /// </summary>
  28. private static readonly AsyncSubjectDisposable[] TERMINATED = new AsyncSubjectDisposable[0];
  29. /// <summary>
  30. /// A pre-allocated empty array indicating the AsyncSubject has terminated
  31. /// </summary>
  32. private static readonly AsyncSubjectDisposable[] DISPOSED = new AsyncSubjectDisposable[0];
  33. #endregion
  34. #region Constructors
  35. /// <summary>
  36. /// Creates a subject that can only receive one value and that value is cached for all future observations.
  37. /// </summary>
  38. public AsyncSubject()
  39. {
  40. _observers = EMPTY;
  41. }
  42. #endregion
  43. #region Properties
  44. /// <summary>
  45. /// Indicates whether the subject has observers subscribed to it.
  46. /// </summary>
  47. public override bool HasObservers => _observers.Length != 0;
  48. /// <summary>
  49. /// Indicates whether the subject has been disposed.
  50. /// </summary>
  51. public override bool IsDisposed => Volatile.Read(ref _observers) == DISPOSED;
  52. #endregion
  53. #region Methods
  54. #region IObserver<T> implementation
  55. /// <summary>
  56. /// Notifies all subscribed observers about the end of the sequence, also causing the last received value to be sent out (if any).
  57. /// </summary>
  58. public override void OnCompleted()
  59. {
  60. for (; ; )
  61. {
  62. var observers = Volatile.Read(ref _observers);
  63. if (observers == DISPOSED)
  64. {
  65. _exception = null;
  66. ThrowDisposed();
  67. break;
  68. }
  69. if (observers == TERMINATED)
  70. {
  71. break;
  72. }
  73. if (Interlocked.CompareExchange(ref _observers, TERMINATED, observers) == observers)
  74. {
  75. var hasValue = _hasValue;
  76. if (hasValue)
  77. {
  78. var value = _value;
  79. foreach (var o in observers)
  80. {
  81. if (!o.IsDisposed())
  82. {
  83. o.Downstream.OnNext(value);
  84. o.Downstream.OnCompleted();
  85. }
  86. }
  87. }
  88. else
  89. {
  90. foreach (var o in observers)
  91. {
  92. if (!o.IsDisposed())
  93. {
  94. o.Downstream.OnCompleted();
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. /// <summary>
  102. /// Notifies all subscribed observers about the exception.
  103. /// </summary>
  104. /// <param name="error">The exception to send to all observers.</param>
  105. /// <exception cref="ArgumentNullException"><paramref name="error"/> is <c>null</c>.</exception>
  106. public override void OnError(Exception error)
  107. {
  108. if (error == null)
  109. {
  110. throw new ArgumentNullException(nameof(error));
  111. }
  112. for (; ; )
  113. {
  114. var observers = Volatile.Read(ref _observers);
  115. if (observers == DISPOSED)
  116. {
  117. _exception = null;
  118. _value = default;
  119. ThrowDisposed();
  120. break;
  121. }
  122. if (observers == TERMINATED)
  123. {
  124. break;
  125. }
  126. _exception = error;
  127. if (Interlocked.CompareExchange(ref _observers, TERMINATED, observers) == observers)
  128. {
  129. foreach (var o in observers)
  130. {
  131. if (!o.IsDisposed())
  132. {
  133. o.Downstream.OnError(error);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// Sends a value to the subject. The last value received before successful termination will be sent to all subscribed and future observers.
  141. /// </summary>
  142. /// <param name="value">The value to store in the subject.</param>
  143. public override void OnNext(T value)
  144. {
  145. var observers = Volatile.Read(ref _observers);
  146. if (observers == DISPOSED)
  147. {
  148. _value = default;
  149. _exception = null;
  150. ThrowDisposed();
  151. return;
  152. }
  153. if (observers == TERMINATED)
  154. {
  155. return;
  156. }
  157. _value = value;
  158. _hasValue = true;
  159. }
  160. #endregion
  161. #region IObservable<T> implementation
  162. /// <summary>
  163. /// Subscribes an observer to the subject.
  164. /// </summary>
  165. /// <param name="observer">Observer to subscribe to the subject.</param>
  166. /// <returns>Disposable object that can be used to unsubscribe the observer from the subject.</returns>
  167. /// <exception cref="ArgumentNullException"><paramref name="observer"/> is <c>null</c>.</exception>
  168. public override IDisposable Subscribe(IObserver<T> observer)
  169. {
  170. if (observer == null)
  171. {
  172. throw new ArgumentNullException(nameof(observer));
  173. }
  174. var parent = new AsyncSubjectDisposable(this, observer);
  175. if (!Add(parent))
  176. {
  177. var ex = _exception;
  178. if (ex != null)
  179. {
  180. observer.OnError(ex);
  181. }
  182. else
  183. {
  184. if (_hasValue)
  185. {
  186. observer.OnNext(_value);
  187. }
  188. observer.OnCompleted();
  189. }
  190. return Disposable.Empty;
  191. }
  192. return parent;
  193. }
  194. private bool Add(AsyncSubjectDisposable inner)
  195. {
  196. for (; ; )
  197. {
  198. var a = Volatile.Read(ref _observers);
  199. if (a == DISPOSED)
  200. {
  201. _value = default;
  202. _exception = null;
  203. ThrowDisposed();
  204. return true;
  205. }
  206. if (a == TERMINATED)
  207. {
  208. return false;
  209. }
  210. var n = a.Length;
  211. var b = new AsyncSubjectDisposable[n + 1];
  212. Array.Copy(a, 0, b, 0, n);
  213. b[n] = inner;
  214. if (Interlocked.CompareExchange(ref _observers, b, a) == a)
  215. {
  216. return true;
  217. }
  218. }
  219. }
  220. private void Remove(AsyncSubjectDisposable inner)
  221. {
  222. for (; ; )
  223. {
  224. var a = Volatile.Read(ref _observers);
  225. var n = a.Length;
  226. if (n == 0)
  227. {
  228. break;
  229. }
  230. var j = -1;
  231. for (var i = 0; i < n; i++)
  232. {
  233. if (a[i] == inner)
  234. {
  235. j = i;
  236. break;
  237. }
  238. }
  239. if (j < 0)
  240. {
  241. break;
  242. }
  243. var b = default(AsyncSubjectDisposable[]);
  244. if (n == 1)
  245. {
  246. b = EMPTY;
  247. }
  248. else
  249. {
  250. b = new AsyncSubjectDisposable[n - 1];
  251. Array.Copy(a, 0, b, 0, j);
  252. Array.Copy(a, j + 1, b, j, n - j - 1);
  253. }
  254. if (Interlocked.CompareExchange(ref _observers, b, a) == a)
  255. {
  256. break;
  257. }
  258. }
  259. }
  260. /// <summary>
  261. /// A disposable connecting the AsyncSubject and an IObserver.
  262. /// </summary>
  263. private sealed class AsyncSubjectDisposable : IDisposable
  264. {
  265. internal readonly IObserver<T> Downstream;
  266. private AsyncSubject<T> _parent;
  267. public AsyncSubjectDisposable(AsyncSubject<T> parent, IObserver<T> downstream)
  268. {
  269. _parent = parent;
  270. Downstream = downstream;
  271. }
  272. public void Dispose()
  273. {
  274. Interlocked.Exchange(ref _parent, null)?.Remove(this);
  275. }
  276. internal bool IsDisposed()
  277. {
  278. return Volatile.Read(ref _parent) == null;
  279. }
  280. }
  281. #endregion
  282. #region IDisposable implementation
  283. private void ThrowDisposed()
  284. {
  285. throw new ObjectDisposedException(string.Empty);
  286. }
  287. /// <summary>
  288. /// Unsubscribe all observers and release resources.
  289. /// </summary>
  290. public override void Dispose()
  291. {
  292. if (Interlocked.Exchange(ref _observers, DISPOSED) != DISPOSED)
  293. {
  294. _exception = null;
  295. _value = default;
  296. _hasValue = false;
  297. }
  298. }
  299. #endregion
  300. #region Await support
  301. /// <summary>
  302. /// Gets an awaitable object for the current AsyncSubject.
  303. /// </summary>
  304. /// <returns>Object that can be awaited.</returns>
  305. public AsyncSubject<T> GetAwaiter() => this;
  306. /// <summary>
  307. /// Specifies a callback action that will be invoked when the subject completes.
  308. /// </summary>
  309. /// <param name="continuation">Callback action that will be invoked when the subject completes.</param>
  310. /// <exception cref="ArgumentNullException"><paramref name="continuation"/> is <c>null</c>.</exception>
  311. public void OnCompleted(Action continuation)
  312. {
  313. if (continuation == null)
  314. {
  315. throw new ArgumentNullException(nameof(continuation));
  316. }
  317. OnCompleted(continuation, originalContext: true);
  318. }
  319. private void OnCompleted(Action continuation, bool originalContext)
  320. {
  321. //
  322. // [OK] Use of unsafe Subscribe: this type's Subscribe implementation is safe.
  323. //
  324. Subscribe/*Unsafe*/(new AwaitObserver(continuation, originalContext));
  325. }
  326. private sealed class AwaitObserver : IObserver<T>
  327. {
  328. private readonly SynchronizationContext _context;
  329. private readonly Action _callback;
  330. public AwaitObserver(Action callback, bool originalContext)
  331. {
  332. if (originalContext)
  333. {
  334. _context = SynchronizationContext.Current;
  335. }
  336. _callback = callback;
  337. }
  338. public void OnCompleted() => InvokeOnOriginalContext();
  339. public void OnError(Exception error) => InvokeOnOriginalContext();
  340. public void OnNext(T value) { }
  341. private void InvokeOnOriginalContext()
  342. {
  343. if (_context != null)
  344. {
  345. //
  346. // No need for OperationStarted and OperationCompleted calls here;
  347. // this code is invoked through await support and will have a way
  348. // to observe its start/complete behavior, either through returned
  349. // Task objects or the async method builder's interaction with the
  350. // SynchronizationContext object.
  351. //
  352. _context.Post(c => ((Action)c)(), _callback);
  353. }
  354. else
  355. {
  356. _callback();
  357. }
  358. }
  359. }
  360. /// <summary>
  361. /// Gets whether the AsyncSubject has completed.
  362. /// </summary>
  363. public bool IsCompleted => Volatile.Read(ref _observers) == TERMINATED;
  364. /// <summary>
  365. /// Gets the last element of the subject, potentially blocking until the subject completes successfully or exceptionally.
  366. /// </summary>
  367. /// <returns>The last element of the subject. Throws an InvalidOperationException if no element was received.</returns>
  368. /// <exception cref="InvalidOperationException">The source sequence is empty.</exception>
  369. [Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Await pattern for C# and VB compilers.")]
  370. public T GetResult()
  371. {
  372. if (Volatile.Read(ref _observers) != TERMINATED)
  373. {
  374. var e = new ManualResetEvent(initialState: false);
  375. OnCompleted(() => e.Set(), originalContext: false);
  376. e.WaitOne();
  377. }
  378. _exception.ThrowIfNotNull();
  379. if (!_hasValue)
  380. {
  381. throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);
  382. }
  383. return _value;
  384. }
  385. #endregion
  386. #endregion
  387. }
  388. }