| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Collections.Generic; |
| 4 |
using System.Reactive.Concurrency; |
| 5 |
|
| 6 |
namespace System.Reactive.Subjects |
| 7 |
{ |
| 8 |
/// <summary> |
| 9 |
/// Represents an object that is both an observable sequence as well as an observer. |
| 10 |
/// Each notification is broadcasted to all subscribed and future observers, subject to buffer trimming policies. |
| 11 |
/// </summary> |
| 12 |
/// <typeparam name="T">The type of the elements processed by the subject.</typeparam> |
| 13 |
public sealed class ReplaySubject<T> : ISubject<T>, IDisposable |
| 14 |
{ |
| 15 |
private const int InfiniteBufferSize = int.MaxValue; |
| 16 |
|
| 17 |
private readonly int _bufferSize; |
| 18 |
private readonly TimeSpan _window; |
| 19 |
private readonly IScheduler _scheduler; |
| 20 |
private readonly IStopwatch _stopwatch; |
| 21 |
|
| 22 |
private readonly Queue<TimeInterval<T>> _queue; |
| 23 |
private bool _isStopped; |
| 24 |
private Exception _error; |
| 25 |
|
| 26 |
private ImmutableList<ScheduledObserver<T>> _observers; |
| 27 |
private bool _isDisposed; |
| 28 |
|
| 29 |
private readonly object _gate = new object(); |
| 30 |
|
| 31 |
/// <summary> |
| 32 |
/// Initializes a new instance of the <see cref="System.Reactive.Subjects.ReplaySubject<T>" /> class with the specified buffer size, window and scheduler. |
| 33 |
/// </summary> |
| 34 |
/// <param name="bufferSize">Maximum element count of the replay buffer.</param> |
| 35 |
/// <param name="window">Maximum time length of the replay buffer.</param> |
| 36 |
/// <param name="scheduler">Scheduler the observers are invoked on.</param> |
| 37 |
/// <exception cref="ArgumentOutOfRangeException"><paramref name="bufferSize"/> is less than zero. -or- <paramref name="window"/> is less than TimeSpan.Zero.</exception> |
| 38 |
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception> |
| 39 |
public ReplaySubject(int bufferSize, TimeSpan window, IScheduler scheduler) |
| 40 |
{ |
| 41 |
if (bufferSize < 0) |
| 42 |
throw new ArgumentOutOfRangeException("bufferSize"); |
| 43 |
if (window < TimeSpan.Zero) |
| 44 |
throw new ArgumentOutOfRangeException("window"); |
| 45 |
if (scheduler == null) |
| 46 |
throw new ArgumentNullException("scheduler"); |
| 47 |
|
| 48 |
_bufferSize = bufferSize; |
| 49 |
_window = window; |
| 50 |
_scheduler = scheduler; |
| 51 |
|
| 52 |
_stopwatch = _scheduler.StartStopwatch(); |
| 53 |
_queue = new Queue<TimeInterval<T>>(); |
| 54 |
_isStopped = false; |
| 55 |
_error = null; |
| 56 |
|
| 57 |
_observers = new ImmutableList<ScheduledObserver<T>>(); |
| 58 |
} |
| 59 |
|
| 60 |
/// <summary> |
| 61 |
/// Initializes a new instance of the <see cref="System.Reactive.Subjects.ReplaySubject<T>" /> class with the specified buffer size and window. |
| 62 |
/// </summary> |
| 63 |
/// <param name="bufferSize">Maximum element count of the replay buffer.</param> |
| 64 |
/// <param name="window">Maximum time length of the replay buffer.</param> |
| 65 |
/// <exception cref="ArgumentOutOfRangeException"><paramref name="bufferSize"/> is less than zero. -or- <paramref name="window"/> is less than TimeSpan.Zero.</exception> |
| 66 |
public ReplaySubject(int bufferSize, TimeSpan window) |
| 67 |
: this(bufferSize, window, SchedulerDefaults.Iteration) |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
/// <summary> |
| 72 |
/// Initializes a new instance of the <see cref="System.Reactive.Subjects.ReplaySubject<T>" /> class. |
| 73 |
/// </summary> |
| 74 |
public ReplaySubject() |
| 75 |
: this(InfiniteBufferSize, TimeSpan.MaxValue, SchedulerDefaults.Iteration) |
| 76 |
{ |
| 77 |
} |
| 78 |
|
| 79 |
/// <summary> |
| 80 |
/// Initializes a new instance of the <see cref="System.Reactive.Subjects.ReplaySubject<T>" /> class with the specified scheduler. |
| 81 |
/// </summary> |
| 82 |
/// <param name="scheduler">Scheduler the observers are invoked on.</param> |
| 83 |
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception> |
| 84 |
public ReplaySubject(IScheduler scheduler) |
| 85 |
: this(InfiniteBufferSize, TimeSpan.MaxValue, scheduler) |
| 86 |
{ |
| 87 |
} |
| 88 |
|
| 89 |
/// <summary> |
| 90 |
/// Initializes a new instance of the <see cref="System.Reactive.Subjects.ReplaySubject<T>" /> class with the specified buffer size and scheduler. |
| 91 |
/// </summary> |
| 92 |
/// <param name="bufferSize">Maximum element count of the replay buffer.</param> |
| 93 |
/// <param name="scheduler">Scheduler the observers are invoked on.</param> |
| 94 |
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception> |
| 95 |
/// <exception cref="ArgumentOutOfRangeException"><paramref name="bufferSize"/> is less than zero.</exception> |
| 96 |
public ReplaySubject(int bufferSize, IScheduler scheduler) |
| 97 |
: this(bufferSize, TimeSpan.MaxValue, scheduler) |
| 98 |
{ |
| 99 |
} |
| 100 |
|
| 101 |
/// <summary> |
| 102 |
/// Initializes a new instance of the <see cref="System.Reactive.Subjects.ReplaySubject<T>" /> class with the specified buffer size. |
| 103 |
/// </summary> |
| 104 |
/// <param name="bufferSize">Maximum element count of the replay buffer.</param> |
| 105 |
/// <exception cref="ArgumentOutOfRangeException"><paramref name="bufferSize"/> is less than zero.</exception> |
| 106 |
public ReplaySubject(int bufferSize) |
| 107 |
: this(bufferSize, TimeSpan.MaxValue, SchedulerDefaults.Iteration) |
| 108 |
{ |
| 109 |
} |
| 110 |
|
| 111 |
/// <summary> |
| 112 |
/// Initializes a new instance of the <see cref="System.Reactive.Subjects.ReplaySubject<T>" /> class with the specified window and scheduler. |
| 113 |
/// </summary> |
| 114 |
/// <param name="window">Maximum time length of the replay buffer.</param> |
| 115 |
/// <param name="scheduler">Scheduler the observers are invoked on.</param> |
| 116 |
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> is null.</exception> |
| 117 |
/// <exception cref="ArgumentOutOfRangeException"><paramref name="window"/> is less than TimeSpan.Zero.</exception> |
| 118 |
public ReplaySubject(TimeSpan window, IScheduler scheduler) |
| 119 |
: this(InfiniteBufferSize, window, scheduler) |
| 120 |
{ |
| 121 |
} |
| 122 |
|
| 123 |
/// <summary> |
| 124 |
/// Initializes a new instance of the <see cref="System.Reactive.Subjects.ReplaySubject<T>" /> class with the specified window. |
| 125 |
/// </summary> |
| 126 |
/// <param name="window">Maximum time length of the replay buffer.</param> |
| 127 |
/// <exception cref="ArgumentOutOfRangeException"><paramref name="window"/> is less than TimeSpan.Zero.</exception> |
| 128 |
public ReplaySubject(TimeSpan window) |
| 129 |
: this(InfiniteBufferSize, window, SchedulerDefaults.Iteration) |
| 130 |
{ |
| 131 |
} |
| 132 |
|
| 133 |
/// <summary> |
| 134 |
/// Indicates whether the subject has observers subscribed to it. |
| 135 |
/// </summary> |
| 136 |
public bool HasObservers |
| 137 |
{ |
| 138 |
get |
| 139 |
{ |
| 140 |
var observers = _observers; |
| 141 |
return observers != null && observers.Data.Length > 0; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
void Trim(TimeSpan now) |
| 146 |
{ |
| 147 |
while (_queue.Count > _bufferSize) |
| 148 |
_queue.Dequeue(); |
| 149 |
while (_queue.Count > 0 && now.Subtract(_queue.Peek().Interval).CompareTo(_window) > 0) |
| 150 |
_queue.Dequeue(); |
| 151 |
} |
| 152 |
|
| 153 |
/// <summary> |
| 154 |
/// Notifies all subscribed and future observers about the arrival of the specified element in the sequence. |
| 155 |
/// </summary> |
| 156 |
/// <param name="value">The value to send to all observers.</param> |
| 157 |
public void OnNext(T value) |
| 158 |
{ |
| 159 |
var o = default(ScheduledObserver<T>[]); |
| 160 |
lock (_gate) |
| 161 |
{ |
| 162 |
CheckDisposed(); |
| 163 |
|
| 164 |
if (!_isStopped) |
| 165 |
{ |
| 166 |
var now = _stopwatch.Elapsed; |
| 167 |
_queue.Enqueue(new TimeInterval<T>(value, now)); |
| 168 |
Trim(now); |
| 169 |
|
| 170 |
o = _observers.Data; |
| 171 |
foreach (var observer in o) |
| 172 |
observer.OnNext(value); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if (o != null) |
| 177 |
foreach (var observer in o) |
| 178 |
observer.EnsureActive(); |
| 179 |
} |
| 180 |
|
| 181 |
/// <summary> |
| 182 |
/// Notifies all subscribed and future observers about the specified exception. |
| 183 |
/// </summary> |
| 184 |
/// <param name="error">The exception to send to all observers.</param> |
| 185 |
/// <exception cref="ArgumentNullException"><paramref name="error"/> is null.</exception> |
| 186 |
public void OnError(Exception error) |
| 187 |
{ |
| 188 |
if (error == null) |
| 189 |
throw new ArgumentNullException("error"); |
| 190 |
|
| 191 |
var o = default(ScheduledObserver<T>[]); |
| 192 |
lock (_gate) |
| 193 |
{ |
| 194 |
CheckDisposed(); |
| 195 |
|
| 196 |
if (!_isStopped) |
| 197 |
{ |
| 198 |
var now = _stopwatch.Elapsed; |
| 199 |
_isStopped = true; |
| 200 |
_error = error; |
| 201 |
Trim(now); |
| 202 |
|
| 203 |
o = _observers.Data; |
| 204 |
foreach (var observer in o) |
| 205 |
observer.OnError(error); |
| 206 |
|
| 207 |
_observers = new ImmutableList<ScheduledObserver<T>>(); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
if (o != null) |
| 212 |
foreach (var observer in o) |
| 213 |
observer.EnsureActive(); |
| 214 |
} |
| 215 |
|
| 216 |
/// <summary> |
| 217 |
/// Notifies all subscribed and future observers about the end of the sequence. |
| 218 |
/// </summary> |
| 219 |
public void OnCompleted() |
| 220 |
{ |
| 221 |
var o = default(ScheduledObserver<T>[]); |
| 222 |
lock (_gate) |
| 223 |
{ |
| 224 |
CheckDisposed(); |
| 225 |
|
| 226 |
if (!_isStopped) |
| 227 |
{ |
| 228 |
var now = _stopwatch.Elapsed; |
| 229 |
_isStopped = true; |
| 230 |
Trim(now); |
| 231 |
|
| 232 |
o = _observers.Data; |
| 233 |
foreach (var observer in o) |
| 234 |
observer.OnCompleted(); |
| 235 |
|
| 236 |
_observers = new ImmutableList<ScheduledObserver<T>>(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if (o != null) |
| 241 |
foreach (var observer in o) |
| 242 |
observer.EnsureActive(); |
| 243 |
} |
| 244 |
|
| 245 |
/// <summary> |
| 246 |
/// Subscribes an observer to the subject. |
| 247 |
/// </summary> |
| 248 |
/// <param name="observer">Observer to subscribe to the subject.</param> |
| 249 |
/// <returns>Disposable object that can be used to unsubscribe the observer from the subject.</returns> |
| 250 |
/// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception> |
| 251 |
public IDisposable Subscribe(IObserver<T> observer) |
| 252 |
{ |
| 253 |
if (observer == null) |
| 254 |
throw new ArgumentNullException("observer"); |
| 255 |
|
| 256 |
var so = new ScheduledObserver<T>(_scheduler, observer); |
| 257 |
|
| 258 |
var n = 0; |
| 259 |
|
| 260 |
var subscription = new RemovableDisposable(this, so); |
| 261 |
lock (_gate) |
| 262 |
{ |
| 263 |
CheckDisposed(); |
| 264 |
|
| 265 |
// |
| 266 |
// Notice the v1.x behavior of always calling Trim is preserved here. |
| 267 |
// |
| 268 |
// This may be subject (pun intended) of debate: should this policy |
| 269 |
// only be applied while the sequence is active? With the current |
| 270 |
// behavior, a sequence will "die out" after it has terminated by |
| 271 |
// continuing to drop OnNext notifications from the queue. |
| 272 |
// |
| 273 |
// In v1.x, this behavior was due to trimming based on the clock value |
| 274 |
// returned by scheduler.Now, applied to all but the terminal message |
| 275 |
// in the queue. Using the IStopwatch has the same effect. Either way, |
| 276 |
// we guarantee the final notification will be observed, but there's |
| 277 |
// no way to retain the buffer directly. One approach is to use the |
| 278 |
// time-based TakeLast operator and apply an unbounded ReplaySubject |
| 279 |
// to it. |
| 280 |
// |
| 281 |
// To conclude, we're keeping the behavior as-is for compatibility |
| 282 |
// reasons with v1.x. |
| 283 |
// |
| 284 |
Trim(_stopwatch.Elapsed); |
| 285 |
_observers = _observers.Add(so); |
| 286 |
|
| 287 |
n = _queue.Count; |
| 288 |
foreach (var item in _queue) |
| 289 |
so.OnNext(item.Value); |
| 290 |
|
| 291 |
if (_error != null) |
| 292 |
{ |
| 293 |
n++; |
| 294 |
so.OnError(_error); |
| 295 |
} |
| 296 |
else if (_isStopped) |
| 297 |
{ |
| 298 |
n++; |
| 299 |
so.OnCompleted(); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
so.EnsureActive(n); |
| 304 |
|
| 305 |
return subscription; |
| 306 |
} |
| 307 |
|
| 308 |
void Unsubscribe(ScheduledObserver<T> observer) |
| 309 |
{ |
| 310 |
lock (_gate) |
| 311 |
{ |
| 312 |
if (!_isDisposed) |
| 313 |
_observers = _observers.Remove(observer); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
sealed class RemovableDisposable : IDisposable |
| 318 |
{ |
| 319 |
private readonly ReplaySubject<T> _subject; |
| 320 |
private readonly ScheduledObserver<T> _observer; |
| 321 |
|
| 322 |
public RemovableDisposable(ReplaySubject<T> subject, ScheduledObserver<T> observer) |
| 323 |
{ |
| 324 |
_subject = subject; |
| 325 |
_observer = observer; |
| 326 |
} |
| 327 |
|
| 328 |
public void Dispose() |
| 329 |
{ |
| 330 |
_observer.Dispose(); |
| 331 |
_subject.Unsubscribe(_observer); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
void CheckDisposed() |
| 336 |
{ |
| 337 |
if (_isDisposed) |
| 338 |
throw new ObjectDisposedException(string.Empty); |
| 339 |
} |
| 340 |
|
| 341 |
/// <summary> |
| 342 |
/// Releases all resources used by the current instance of the <see cref="System.Reactive.Subjects.ReplaySubject<T>"/> class and unsubscribe all observers. |
| 343 |
/// </summary> |
| 344 |
public void Dispose() |
| 345 |
{ |
| 346 |
lock (_gate) |
| 347 |
{ |
| 348 |
_isDisposed = true; |
| 349 |
_observers = null; |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
} |