| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if !NO_PERF |
| 4 |
using System.Reactive.Disposables; |
| 5 |
using System.Threading; |
| 6 |
|
| 7 |
namespace System.Reactive.Subjects |
| 8 |
{ |
| 9 |
/// <summary> |
| 10 |
/// Represents an object that is both an observable sequence as well as an observer. |
| 11 |
/// Each notification is broadcasted to all subscribed observers. |
| 12 |
/// </summary> |
| 13 |
/// <typeparam name="T">The type of the elements processed by the subject.</typeparam> |
| 14 |
public sealed class Subject<T> : ISubject<T>, IDisposable |
| 15 |
{ |
| 16 |
private volatile IObserver<T> _observer; |
| 17 |
|
| 18 |
/// <summary> |
| 19 |
/// Creates a subject. |
| 20 |
/// </summary> |
| 21 |
public Subject() |
| 22 |
{ |
| 23 |
_observer = NopObserver<T>.Instance; |
| 24 |
} |
| 25 |
|
| 26 |
/// <summary> |
| 27 |
/// Indicates whether the subject has observers subscribed to it. |
| 28 |
/// </summary> |
| 29 |
public bool HasObservers |
| 30 |
{ |
| 31 |
get |
| 32 |
{ |
| 33 |
return _observer != NopObserver<T>.Instance && !(_observer is DoneObserver<T>) && _observer != DisposedObserver<T>.Instance; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/// <summary> |
| 38 |
/// Notifies all subscribed observers about the end of the sequence. |
| 39 |
/// </summary> |
| 40 |
public void OnCompleted() |
| 41 |
{ |
| 42 |
var oldObserver = default(IObserver<T>); |
| 43 |
var newObserver = DoneObserver<T>.Completed; |
| 44 |
|
| 45 |
do |
| 46 |
{ |
| 47 |
oldObserver = _observer; |
| 48 |
|
| 49 |
if (oldObserver == DisposedObserver<T>.Instance || oldObserver is DoneObserver<T>) |
| 50 |
break; |
| 51 |
#pragma warning disable 0420 |
| 52 |
} while (Interlocked.CompareExchange(ref _observer, newObserver, oldObserver) != oldObserver); |
| 53 |
#pragma warning restore 0420 |
| 54 |
|
| 55 |
oldObserver.OnCompleted(); |
| 56 |
} |
| 57 |
|
| 58 |
/// <summary> |
| 59 |
/// Notifies all subscribed observers about the specified exception. |
| 60 |
/// </summary> |
| 61 |
/// <param name="error">The exception to send to all currently subscribed observers.</param> |
| 62 |
/// <exception cref="ArgumentNullException"><paramref name="error"/> is null.</exception> |
| 63 |
public void OnError(Exception error) |
| 64 |
{ |
| 65 |
if (error == null) |
| 66 |
throw new ArgumentNullException("error"); |
| 67 |
|
| 68 |
var oldObserver = default(IObserver<T>); |
| 69 |
var newObserver = new DoneObserver<T> { Exception = error }; |
| 70 |
|
| 71 |
do |
| 72 |
{ |
| 73 |
oldObserver = _observer; |
| 74 |
|
| 75 |
if (oldObserver == DisposedObserver<T>.Instance || oldObserver is DoneObserver<T>) |
| 76 |
break; |
| 77 |
#pragma warning disable 0420 |
| 78 |
} while (Interlocked.CompareExchange(ref _observer, newObserver, oldObserver) != oldObserver); |
| 79 |
#pragma warning restore 0420 |
| 80 |
|
| 81 |
oldObserver.OnError(error); |
| 82 |
} |
| 83 |
|
| 84 |
/// <summary> |
| 85 |
/// Notifies all subscribed observers about the arrival of the specified element in the sequence. |
| 86 |
/// </summary> |
| 87 |
/// <param name="value">The value to send to all currently subscribed observers.</param> |
| 88 |
public void OnNext(T value) |
| 89 |
{ |
| 90 |
_observer.OnNext(value); |
| 91 |
} |
| 92 |
|
| 93 |
/// <summary> |
| 94 |
/// Subscribes an observer to the subject. |
| 95 |
/// </summary> |
| 96 |
/// <param name="observer">Observer to subscribe to the subject.</param> |
| 97 |
/// <returns>Disposable object that can be used to unsubscribe the observer from the subject.</returns> |
| 98 |
/// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception> |
| 99 |
public IDisposable Subscribe(IObserver<T> observer) |
| 100 |
{ |
| 101 |
if (observer == null) |
| 102 |
throw new ArgumentNullException("observer"); |
| 103 |
|
| 104 |
var oldObserver = default(IObserver<T>); |
| 105 |
var newObserver = default(IObserver<T>); |
| 106 |
|
| 107 |
do |
| 108 |
{ |
| 109 |
oldObserver = _observer; |
| 110 |
|
| 111 |
if (oldObserver == DisposedObserver<T>.Instance) |
| 112 |
{ |
| 113 |
throw new ObjectDisposedException(""); |
| 114 |
} |
| 115 |
|
| 116 |
if (oldObserver == DoneObserver<T>.Completed) |
| 117 |
{ |
| 118 |
observer.OnCompleted(); |
| 119 |
return Disposable.Empty; |
| 120 |
} |
| 121 |
|
| 122 |
var done = oldObserver as DoneObserver<T>; |
| 123 |
if (done != null) |
| 124 |
{ |
| 125 |
observer.OnError(done.Exception); |
| 126 |
return Disposable.Empty; |
| 127 |
} |
| 128 |
|
| 129 |
if (oldObserver == NopObserver<T>.Instance) |
| 130 |
{ |
| 131 |
newObserver = observer; |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
var obs = oldObserver as Observer<T>; |
| 136 |
if (obs != null) |
| 137 |
{ |
| 138 |
newObserver = obs.Add(observer); |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
newObserver = new Observer<T>(new ImmutableList<IObserver<T>>(new[] { oldObserver, observer })); |
| 143 |
} |
| 144 |
} |
| 145 |
#pragma warning disable 0420 |
| 146 |
} while (Interlocked.CompareExchange(ref _observer, newObserver, oldObserver) != oldObserver); |
| 147 |
#pragma warning restore 0420 |
| 148 |
|
| 149 |
return new Subscription(this, observer); |
| 150 |
} |
| 151 |
|
| 152 |
class Subscription : IDisposable |
| 153 |
{ |
| 154 |
private Subject<T> _subject; |
| 155 |
private IObserver<T> _observer; |
| 156 |
|
| 157 |
public Subscription(Subject<T> subject, IObserver<T> observer) |
| 158 |
{ |
| 159 |
_subject = subject; |
| 160 |
_observer = observer; |
| 161 |
} |
| 162 |
|
| 163 |
public void Dispose() |
| 164 |
{ |
| 165 |
var observer = Interlocked.Exchange(ref _observer, null); |
| 166 |
if (observer == null) |
| 167 |
return; |
| 168 |
|
| 169 |
_subject.Unsubscribe(observer); |
| 170 |
_subject = null; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
private void Unsubscribe(IObserver<T> observer) |
| 175 |
{ |
| 176 |
var oldObserver = default(IObserver<T>); |
| 177 |
var newObserver = default(IObserver<T>); |
| 178 |
|
| 179 |
do |
| 180 |
{ |
| 181 |
oldObserver = _observer; |
| 182 |
|
| 183 |
if (oldObserver == DisposedObserver<T>.Instance || oldObserver is DoneObserver<T>) |
| 184 |
return; |
| 185 |
|
| 186 |
var obs = oldObserver as Observer<T>; |
| 187 |
if (obs != null) |
| 188 |
{ |
| 189 |
newObserver = obs.Remove(observer); |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
if (oldObserver != observer) |
| 194 |
return; |
| 195 |
|
| 196 |
newObserver = NopObserver<T>.Instance; |
| 197 |
} |
| 198 |
#pragma warning disable 0420 |
| 199 |
} while (Interlocked.CompareExchange(ref _observer, newObserver, oldObserver) != oldObserver); |
| 200 |
#pragma warning restore 0420 |
| 201 |
} |
| 202 |
|
| 203 |
/// <summary> |
| 204 |
/// Releases all resources used by the current instance of the <see cref="System.Reactive.Subjects.Subject<T>"/> class and unsubscribes all observers. |
| 205 |
/// </summary> |
| 206 |
public void Dispose() |
| 207 |
{ |
| 208 |
_observer = DisposedObserver<T>.Instance; |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
#else |
| 213 |
using System.Reactive.Disposables; |
| 214 |
using System.Threading; |
| 215 |
|
| 216 |
namespace System.Reactive.Subjects |
| 217 |
{ |
| 218 |
/// <summary> |
| 219 |
/// Represents an object that is both an observable sequence as well as an observer. |
| 220 |
/// Each notification is broadcasted to all subscribed observers. |
| 221 |
/// </summary> |
| 222 |
/// <typeparam name="T">The type of the elements processed by the subject.</typeparam> |
| 223 |
public sealed class Subject<T> : ISubject<T>, IDisposable |
| 224 |
{ |
| 225 |
bool isDisposed; |
| 226 |
bool isStopped; |
| 227 |
ImmutableList<IObserver<T>> observers; |
| 228 |
object gate = new object(); |
| 229 |
Exception exception; |
| 230 |
|
| 231 |
/// <summary> |
| 232 |
/// Creates a subject. |
| 233 |
/// </summary> |
| 234 |
public Subject() |
| 235 |
{ |
| 236 |
observers = new ImmutableList<IObserver<T>>(); |
| 237 |
} |
| 238 |
|
| 239 |
/// <summary> |
| 240 |
/// Notifies all subscribed observers about the end of the sequence. |
| 241 |
/// </summary> |
| 242 |
public void OnCompleted() |
| 243 |
{ |
| 244 |
var os = default(IObserver<T>[]); |
| 245 |
lock (gate) |
| 246 |
{ |
| 247 |
CheckDisposed(); |
| 248 |
|
| 249 |
if (!isStopped) |
| 250 |
{ |
| 251 |
os = observers.Data; |
| 252 |
observers = new ImmutableList<IObserver<T>>(); |
| 253 |
isStopped = true; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
if (os != null) |
| 258 |
foreach (var o in os) |
| 259 |
o.OnCompleted(); |
| 260 |
} |
| 261 |
|
| 262 |
/// <summary> |
| 263 |
/// Notifies all subscribed observers with the exception. |
| 264 |
/// </summary> |
| 265 |
/// <param name="error">The exception to send to all subscribed observers.</param> |
| 266 |
/// <exception cref="ArgumentNullException"><paramref name="error"/> is null.</exception> |
| 267 |
public void OnError(Exception error) |
| 268 |
{ |
| 269 |
if (error == null) |
| 270 |
throw new ArgumentNullException("error"); |
| 271 |
|
| 272 |
var os = default(IObserver<T>[]); |
| 273 |
lock (gate) |
| 274 |
{ |
| 275 |
CheckDisposed(); |
| 276 |
|
| 277 |
if (!isStopped) |
| 278 |
{ |
| 279 |
os = observers.Data; |
| 280 |
observers = new ImmutableList<IObserver<T>>(); |
| 281 |
isStopped = true; |
| 282 |
exception = error; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
if (os != null) |
| 287 |
foreach (var o in os) |
| 288 |
o.OnError(error); |
| 289 |
} |
| 290 |
|
| 291 |
/// <summary> |
| 292 |
/// Notifies all subscribed observers with the value. |
| 293 |
/// </summary> |
| 294 |
/// <param name="value">The value to send to all subscribed observers.</param> |
| 295 |
public void OnNext(T value) |
| 296 |
{ |
| 297 |
var os = default(IObserver<T>[]); |
| 298 |
lock (gate) |
| 299 |
{ |
| 300 |
CheckDisposed(); |
| 301 |
|
| 302 |
if (!isStopped) |
| 303 |
{ |
| 304 |
os = observers.Data; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
if (os != null) |
| 309 |
foreach (var o in os) |
| 310 |
o.OnNext(value); |
| 311 |
} |
| 312 |
|
| 313 |
/// <summary> |
| 314 |
/// Subscribes an observer to the subject. |
| 315 |
/// </summary> |
| 316 |
/// <param name="observer">Observer to subscribe to the subject.</param> |
| 317 |
/// <remarks>IDisposable object that can be used to unsubscribe the observer from the subject.</remarks> |
| 318 |
/// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception> |
| 319 |
public IDisposable Subscribe(IObserver<T> observer) |
| 320 |
{ |
| 321 |
if (observer == null) |
| 322 |
throw new ArgumentNullException("observer"); |
| 323 |
|
| 324 |
lock (gate) |
| 325 |
{ |
| 326 |
CheckDisposed(); |
| 327 |
|
| 328 |
if (!isStopped) |
| 329 |
{ |
| 330 |
observers = observers.Add(observer); |
| 331 |
return new Subscription(this, observer); |
| 332 |
} |
| 333 |
else if (exception != null) |
| 334 |
{ |
| 335 |
observer.OnError(exception); |
| 336 |
return Disposable.Empty; |
| 337 |
} |
| 338 |
else |
| 339 |
{ |
| 340 |
observer.OnCompleted(); |
| 341 |
return Disposable.Empty; |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
void Unsubscribe(IObserver<T> observer) |
| 347 |
{ |
| 348 |
lock (gate) |
| 349 |
{ |
| 350 |
if (observers != null) |
| 351 |
observers = observers.Remove(observer); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
class Subscription : IDisposable |
| 356 |
{ |
| 357 |
Subject<T> subject; |
| 358 |
IObserver<T> observer; |
| 359 |
|
| 360 |
public Subscription(Subject<T> subject, IObserver<T> observer) |
| 361 |
{ |
| 362 |
this.subject = subject; |
| 363 |
this.observer = observer; |
| 364 |
} |
| 365 |
|
| 366 |
public void Dispose() |
| 367 |
{ |
| 368 |
var o = Interlocked.Exchange<IObserver<T>>(ref observer, null); |
| 369 |
if (o != null) |
| 370 |
{ |
| 371 |
subject.Unsubscribe(o); |
| 372 |
subject = null; |
| 373 |
} |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
void CheckDisposed() |
| 378 |
{ |
| 379 |
if (isDisposed) |
| 380 |
throw new ObjectDisposedException(string.Empty); |
| 381 |
} |
| 382 |
|
| 383 |
/// <summary> |
| 384 |
/// Unsubscribe all observers and release resources. |
| 385 |
/// </summary> |
| 386 |
public void Dispose() |
| 387 |
{ |
| 388 |
lock (gate) |
| 389 |
{ |
| 390 |
isDisposed = true; |
| 391 |
observers = null; |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
#endif |