| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Reactive.Disposables; |
| 4 |
|
| 5 |
namespace System.Reactive.Subjects |
| 6 |
{ |
| 7 |
/// <summary> |
| 8 |
/// Represents a value that changes over time. |
| 9 |
/// Observers can subscribe to the subject to receive the last (or initial) value and all subsequent notifications. |
| 10 |
/// </summary> |
| 11 |
/// <typeparam name="T">The type of the elements processed by the subject.</typeparam> |
| 12 |
public sealed class BehaviorSubject<T> : ISubject<T>, IDisposable |
| 13 |
{ |
| 14 |
/// <summary> |
| 15 |
/// Gets the current value or throws an exception. |
| 16 |
/// </summary> |
| 17 |
/// <value>The initial value passed to the constructor until <see cref="OnNext"/> is called; after which, the last value passed to <see cref="OnNext"/>.</value> |
| 18 |
/// <remarks> |
| 19 |
/// <para><see cref="Value"/> is frozen after <see cref="OnCompleted"/> is called.</para> |
| 20 |
/// <para>After <see cref="OnError"/> is called, <see cref="Value"/> always throws the specified exception.</para> |
| 21 |
/// <para>An exception is always thrown after <see cref="Dispose"/> is called.</para> |
| 22 |
/// <alert type="caller"> |
| 23 |
/// Reading <see cref="Value"/> is a thread-safe operation, though there's a potential race condition when <see cref="OnNext"/> or <see cref="OnError"/> are being invoked concurrently. |
| 24 |
/// In some cases, it may be necessary for a caller to use external synchronization to avoid race conditions. |
| 25 |
/// </alert> |
| 26 |
/// </remarks> |
| 27 |
/// <exception cref="ObjectDisposedException">Dispose was called.</exception> |
| 28 |
public T Value |
| 29 |
{ |
| 30 |
get |
| 31 |
{ |
| 32 |
lock (_gate) |
| 33 |
{ |
| 34 |
CheckDisposed(); |
| 35 |
|
| 36 |
if (_exception != null) |
| 37 |
{ |
| 38 |
throw _exception; |
| 39 |
} |
| 40 |
|
| 41 |
return _value; |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
private readonly object _gate = new object(); |
| 47 |
|
| 48 |
private ImmutableList<IObserver<T>> _observers; |
| 49 |
private bool _isStopped; |
| 50 |
private T _value; |
| 51 |
private Exception _exception; |
| 52 |
private bool _isDisposed; |
| 53 |
|
| 54 |
/// <summary> |
| 55 |
/// Initializes a new instance of the <see cref="System.Reactive.Subjects.BehaviorSubject<T>"/> class which creates a subject that caches its last value and starts with the specified value. |
| 56 |
/// </summary> |
| 57 |
/// <param name="value">Initial value sent to observers when no other value has been received by the subject yet.</param> |
| 58 |
public BehaviorSubject(T value) |
| 59 |
{ |
| 60 |
_value = value; |
| 61 |
_observers = new ImmutableList<IObserver<T>>(); |
| 62 |
} |
| 63 |
|
| 64 |
/// <summary> |
| 65 |
/// Indicates whether the subject has observers subscribed to it. |
| 66 |
/// </summary> |
| 67 |
public bool HasObservers |
| 68 |
{ |
| 69 |
get |
| 70 |
{ |
| 71 |
var observers = _observers; |
| 72 |
return observers != null && observers.Data.Length > 0; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/// <summary> |
| 77 |
/// Notifies all subscribed observers about the end of the sequence. |
| 78 |
/// </summary> |
| 79 |
public void OnCompleted() |
| 80 |
{ |
| 81 |
var os = default(IObserver<T>[]); |
| 82 |
lock (_gate) |
| 83 |
{ |
| 84 |
CheckDisposed(); |
| 85 |
|
| 86 |
if (!_isStopped) |
| 87 |
{ |
| 88 |
os = _observers.Data; |
| 89 |
_observers = new ImmutableList<IObserver<T>>(); |
| 90 |
_isStopped = true; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (os != null) |
| 95 |
{ |
| 96 |
foreach (var o in os) |
| 97 |
o.OnCompleted(); |
| 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 null.</exception> |
| 106 |
public void OnError(Exception error) |
| 107 |
{ |
| 108 |
if (error == null) |
| 109 |
throw new ArgumentNullException("error"); |
| 110 |
|
| 111 |
var os = default(IObserver<T>[]); |
| 112 |
lock (_gate) |
| 113 |
{ |
| 114 |
CheckDisposed(); |
| 115 |
|
| 116 |
if (!_isStopped) |
| 117 |
{ |
| 118 |
os = _observers.Data; |
| 119 |
_observers = new ImmutableList<IObserver<T>>(); |
| 120 |
_isStopped = true; |
| 121 |
_exception = error; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if (os != null) |
| 126 |
foreach (var o in os) |
| 127 |
o.OnError(error); |
| 128 |
} |
| 129 |
|
| 130 |
/// <summary> |
| 131 |
/// Notifies all subscribed observers about the arrival of the specified element in the sequence. |
| 132 |
/// </summary> |
| 133 |
/// <param name="value">The value to send to all observers.</param> |
| 134 |
public void OnNext(T value) |
| 135 |
{ |
| 136 |
var os = default(IObserver<T>[]); |
| 137 |
lock (_gate) |
| 138 |
{ |
| 139 |
CheckDisposed(); |
| 140 |
|
| 141 |
if (!_isStopped) |
| 142 |
{ |
| 143 |
_value = value; |
| 144 |
os = _observers.Data; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
if (os != null) |
| 149 |
{ |
| 150 |
foreach (var o in os) |
| 151 |
o.OnNext(value); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/// <summary> |
| 156 |
/// Subscribes an observer to the subject. |
| 157 |
/// </summary> |
| 158 |
/// <param name="observer">Observer to subscribe to the subject.</param> |
| 159 |
/// <returns>Disposable object that can be used to unsubscribe the observer from the subject.</returns> |
| 160 |
/// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception> |
| 161 |
public IDisposable Subscribe(IObserver<T> observer) |
| 162 |
{ |
| 163 |
if (observer == null) |
| 164 |
throw new ArgumentNullException("observer"); |
| 165 |
|
| 166 |
var ex = default(Exception); |
| 167 |
|
| 168 |
lock (_gate) |
| 169 |
{ |
| 170 |
CheckDisposed(); |
| 171 |
|
| 172 |
if (!_isStopped) |
| 173 |
{ |
| 174 |
_observers = _observers.Add(observer); |
| 175 |
observer.OnNext(_value); |
| 176 |
return new Subscription(this, observer); |
| 177 |
} |
| 178 |
|
| 179 |
ex = _exception; |
| 180 |
} |
| 181 |
|
| 182 |
if (ex != null) |
| 183 |
observer.OnError(ex); |
| 184 |
else |
| 185 |
observer.OnCompleted(); |
| 186 |
|
| 187 |
return Disposable.Empty; |
| 188 |
} |
| 189 |
|
| 190 |
class Subscription : IDisposable |
| 191 |
{ |
| 192 |
private readonly BehaviorSubject<T> _subject; |
| 193 |
private IObserver<T> _observer; |
| 194 |
|
| 195 |
public Subscription(BehaviorSubject<T> subject, IObserver<T> observer) |
| 196 |
{ |
| 197 |
_subject = subject; |
| 198 |
_observer = observer; |
| 199 |
} |
| 200 |
|
| 201 |
public void Dispose() |
| 202 |
{ |
| 203 |
if (_observer != null) |
| 204 |
{ |
| 205 |
lock (_subject._gate) |
| 206 |
{ |
| 207 |
if (!_subject._isDisposed && _observer != null) |
| 208 |
{ |
| 209 |
_subject._observers = _subject._observers.Remove(_observer); |
| 210 |
_observer = null; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
void CheckDisposed() |
| 218 |
{ |
| 219 |
if (_isDisposed) |
| 220 |
throw new ObjectDisposedException(string.Empty); |
| 221 |
} |
| 222 |
|
| 223 |
/// <summary> |
| 224 |
/// Unsubscribe all observers and release resources. |
| 225 |
/// </summary> |
| 226 |
public void Dispose() |
| 227 |
{ |
| 228 |
lock (_gate) |
| 229 |
{ |
| 230 |
_isDisposed = true; |
| 231 |
_observers = null; |
| 232 |
_value = default(T); |
| 233 |
_exception = null; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |