ObserverBase.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.Threading;
  3. namespace System.Reactive
  4. {
  5. /// <summary>
  6. /// Abstract base class for implementations of the IObserver&lt;T&gt; interface.
  7. /// </summary>
  8. /// <remarks>This base class enforces the grammar of observers where OnError and OnCompleted are terminal messages.</remarks>
  9. /// <typeparam name="T">The type of the elements in the sequence.</typeparam>
  10. public abstract class ObserverBase<T> : IObserver<T>, IDisposable
  11. {
  12. private int isStopped;
  13. /// <summary>
  14. /// Creates a new observer in a non-stopped state.
  15. /// </summary>
  16. protected ObserverBase()
  17. {
  18. isStopped = 0;
  19. }
  20. /// <summary>
  21. /// Notifies the observer of a new element in the sequence.
  22. /// </summary>
  23. /// <param name="value">Next element in the sequence.</param>
  24. public void OnNext(T value)
  25. {
  26. if (isStopped == 0)
  27. OnNextCore(value);
  28. }
  29. /// <summary>
  30. /// Implement this method to react to the receival of a new element in the sequence.
  31. /// </summary>
  32. /// <param name="value">Next element in the sequence.</param>
  33. /// <remarks>This method only gets called when the observer hasn't stopped yet.</remarks>
  34. protected abstract void OnNextCore(T value);
  35. /// <summary>
  36. /// Notifies the observer that an exception has occurred.
  37. /// </summary>
  38. /// <param name="error">The error that has occurred.</param>
  39. /// <exception cref="ArgumentNullException"><paramref name="error"/> is null.</exception>
  40. public void OnError(Exception error)
  41. {
  42. if (error == null)
  43. throw new ArgumentNullException("error");
  44. if (Interlocked.Exchange(ref isStopped, 1) == 0)
  45. {
  46. OnErrorCore(error);
  47. }
  48. }
  49. /// <summary>
  50. /// Implement this method to react to the occurrence of an exception.
  51. /// </summary>
  52. /// <param name="error">The error that has occurred.</param>
  53. /// <remarks>This method only gets called when the observer hasn't stopped yet, and causes the observer to stop.</remarks>
  54. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Error", Justification = "Same name as in the IObserver<T> definition of OnError in the BCL.")]
  55. protected abstract void OnErrorCore(Exception error);
  56. /// <summary>
  57. /// Notifies the observer of the end of the sequence.
  58. /// </summary>
  59. public void OnCompleted()
  60. {
  61. if (Interlocked.Exchange(ref isStopped, 1) == 0)
  62. {
  63. OnCompletedCore();
  64. }
  65. }
  66. /// <summary>
  67. /// Implement this method to react to the end of the sequence.
  68. /// </summary>
  69. /// <remarks>This method only gets called when the observer hasn't stopped yet, and causes the observer to stop.</remarks>
  70. protected abstract void OnCompletedCore();
  71. /// <summary>
  72. /// Disposes the observer, causing it to transition to the stopped state.
  73. /// </summary>
  74. public void Dispose()
  75. {
  76. Dispose(true);
  77. GC.SuppressFinalize(this);
  78. }
  79. /// <summary>
  80. /// Core implementation of IDisposable.
  81. /// </summary>
  82. /// <param name="disposing">true if the Dispose call was triggered by the IDisposable.Dispose method; false if it was triggered by the finalizer.</param>
  83. protected virtual void Dispose(bool disposing)
  84. {
  85. if (disposing)
  86. {
  87. isStopped = 1;
  88. }
  89. }
  90. internal bool Fail(Exception error)
  91. {
  92. if (Interlocked.Exchange(ref isStopped, 1) == 0)
  93. {
  94. OnErrorCore(error);
  95. return true;
  96. }
  97. return false;
  98. }
  99. }
  100. }