Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/ObserverBase.cs

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