| 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; |
| 5 |
using System.Collections.Generic; |
| 6 |
using System.Diagnostics; |
| 7 |
using System.Reactive.Concurrency; |
| 8 |
using System.Reactive.Disposables; |
| 9 |
using System.Reactive.Subjects; |
| 10 |
using System.Threading; |
| 11 |
|
| 12 |
namespace System.Reactive.Linq.ObservableImpl |
| 13 |
{ |
| 14 |
class Window<TSource> : Producer<IObservable<TSource>> |
| 15 |
{ |
| 16 |
private readonly IObservable<TSource> _source; |
| 17 |
private readonly int _count; |
| 18 |
private readonly int _skip; |
| 19 |
|
| 20 |
private readonly TimeSpan _timeSpan; |
| 21 |
private readonly TimeSpan _timeShift; |
| 22 |
private readonly IScheduler _scheduler; |
| 23 |
|
| 24 |
public Window(IObservable<TSource> source, int count, int skip) |
| 25 |
{ |
| 26 |
_source = source; |
| 27 |
_count = count; |
| 28 |
_skip = skip; |
| 29 |
} |
| 30 |
|
| 31 |
public Window(IObservable<TSource> source, TimeSpan timeSpan, TimeSpan timeShift, IScheduler scheduler) |
| 32 |
{ |
| 33 |
_source = source; |
| 34 |
_timeSpan = timeSpan; |
| 35 |
_timeShift = timeShift; |
| 36 |
_scheduler = scheduler; |
| 37 |
} |
| 38 |
|
| 39 |
public Window(IObservable<TSource> source, TimeSpan timeSpan, int count, IScheduler scheduler) |
| 40 |
{ |
| 41 |
_source = source; |
| 42 |
_timeSpan = timeSpan; |
| 43 |
_count = count; |
| 44 |
_scheduler = scheduler; |
| 45 |
} |
| 46 |
|
| 47 |
protected override IDisposable Run(IObserver<IObservable<TSource>> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 48 |
{ |
| 49 |
if (_scheduler == null) |
| 50 |
{ |
| 51 |
var sink = new _(this, observer, cancel); |
| 52 |
setSink(sink); |
| 53 |
return sink.Run(); |
| 54 |
} |
| 55 |
else if (_count > 0) |
| 56 |
{ |
| 57 |
var sink = new BoundedWindowImpl(this, observer, cancel); |
| 58 |
setSink(sink); |
| 59 |
return sink.Run(); |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
if (_timeSpan == _timeShift) |
| 64 |
{ |
| 65 |
var sink = new TimeShiftImpl(this, observer, cancel); |
| 66 |
setSink(sink); |
| 67 |
return sink.Run(); |
| 68 |
} |
| 69 |
else |
| 70 |
{ |
| 71 |
var sink = new WindowImpl(this, observer, cancel); |
| 72 |
setSink(sink); |
| 73 |
return sink.Run(); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
class _ : Sink<IObservable<TSource>>, IObserver<TSource> |
| 79 |
{ |
| 80 |
private readonly Window<TSource> _parent; |
| 81 |
|
| 82 |
public _(Window<TSource> parent, IObserver<IObservable<TSource>> observer, IDisposable cancel) |
| 83 |
: base(observer, cancel) |
| 84 |
{ |
| 85 |
_parent = parent; |
| 86 |
} |
| 87 |
|
| 88 |
private Queue<ISubject<TSource>> _queue; |
| 89 |
private int _n; |
| 90 |
private SingleAssignmentDisposable _m; |
| 91 |
private RefCountDisposable _refCountDisposable; |
| 92 |
|
| 93 |
public IDisposable Run() |
| 94 |
{ |
| 95 |
_queue = new Queue<ISubject<TSource>>(); |
| 96 |
_n = 0; |
| 97 |
_m = new SingleAssignmentDisposable(); |
| 98 |
_refCountDisposable = new RefCountDisposable(_m); |
| 99 |
|
| 100 |
var firstWindow = CreateWindow(); |
| 101 |
base._observer.OnNext(firstWindow); |
| 102 |
|
| 103 |
_m.Disposable = _parent._source.SubscribeSafe(this); |
| 104 |
|
| 105 |
return _refCountDisposable; |
| 106 |
} |
| 107 |
|
| 108 |
private IObservable<TSource> CreateWindow() |
| 109 |
{ |
| 110 |
var s = new Subject<TSource>(); |
| 111 |
_queue.Enqueue(s); |
| 112 |
return new WindowObservable<TSource>(s, _refCountDisposable); |
| 113 |
} |
| 114 |
|
| 115 |
public void OnNext(TSource value) |
| 116 |
{ |
| 117 |
foreach (var s in _queue) |
| 118 |
s.OnNext(value); |
| 119 |
|
| 120 |
var c = _n - _parent._count + 1; |
| 121 |
if (c >= 0 && c % _parent._skip == 0) |
| 122 |
{ |
| 123 |
var s = _queue.Dequeue(); |
| 124 |
s.OnCompleted(); |
| 125 |
} |
| 126 |
|
| 127 |
_n++; |
| 128 |
if (_n % _parent._skip == 0) |
| 129 |
{ |
| 130 |
var newWindow = CreateWindow(); |
| 131 |
base._observer.OnNext(newWindow); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
public void OnError(Exception error) |
| 136 |
{ |
| 137 |
while (_queue.Count > 0) |
| 138 |
_queue.Dequeue().OnError(error); |
| 139 |
|
| 140 |
base._observer.OnError(error); |
| 141 |
base.Dispose(); |
| 142 |
} |
| 143 |
|
| 144 |
public void OnCompleted() |
| 145 |
{ |
| 146 |
while (_queue.Count > 0) |
| 147 |
_queue.Dequeue().OnCompleted(); |
| 148 |
|
| 149 |
base._observer.OnCompleted(); |
| 150 |
base.Dispose(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
class WindowImpl : Sink<IObservable<TSource>>, IObserver<TSource> |
| 155 |
{ |
| 156 |
private readonly Window<TSource> _parent; |
| 157 |
|
| 158 |
public WindowImpl(Window<TSource> parent, IObserver<IObservable<TSource>> observer, IDisposable cancel) |
| 159 |
: base(observer, cancel) |
| 160 |
{ |
| 161 |
_parent = parent; |
| 162 |
} |
| 163 |
|
| 164 |
private TimeSpan _totalTime; |
| 165 |
private TimeSpan _nextShift; |
| 166 |
private TimeSpan _nextSpan; |
| 167 |
|
| 168 |
private object _gate; |
| 169 |
private Queue<ISubject<TSource>> _q; |
| 170 |
|
| 171 |
private SerialDisposable _timerD; |
| 172 |
private RefCountDisposable _refCountDisposable; |
| 173 |
|
| 174 |
public IDisposable Run() |
| 175 |
{ |
| 176 |
_totalTime = TimeSpan.Zero; |
| 177 |
_nextShift = _parent._timeShift; |
| 178 |
_nextSpan = _parent._timeSpan; |
| 179 |
|
| 180 |
_gate = new object(); |
| 181 |
_q = new Queue<ISubject<TSource>>(); |
| 182 |
|
| 183 |
_timerD = new SerialDisposable(); |
| 184 |
|
| 185 |
var groupDisposable = new CompositeDisposable(2) { _timerD }; |
| 186 |
_refCountDisposable = new RefCountDisposable(groupDisposable); |
| 187 |
|
| 188 |
CreateWindow(); |
| 189 |
CreateTimer(); |
| 190 |
|
| 191 |
groupDisposable.Add(_parent._source.SubscribeSafe(this)); |
| 192 |
|
| 193 |
return _refCountDisposable; |
| 194 |
} |
| 195 |
|
| 196 |
private void CreateWindow() |
| 197 |
{ |
| 198 |
var s = new Subject<TSource>(); |
| 199 |
_q.Enqueue(s); |
| 200 |
base._observer.OnNext(new WindowObservable<TSource>(s, _refCountDisposable)); |
| 201 |
} |
| 202 |
|
| 203 |
private void CreateTimer() |
| 204 |
{ |
| 205 |
var m = new SingleAssignmentDisposable(); |
| 206 |
_timerD.Disposable = m; |
| 207 |
|
| 208 |
var isSpan = false; |
| 209 |
var isShift = false; |
| 210 |
if (_nextSpan == _nextShift) |
| 211 |
{ |
| 212 |
isSpan = true; |
| 213 |
isShift = true; |
| 214 |
} |
| 215 |
else if (_nextSpan < _nextShift) |
| 216 |
isSpan = true; |
| 217 |
else |
| 218 |
isShift = true; |
| 219 |
|
| 220 |
var newTotalTime = isSpan ? _nextSpan : _nextShift; |
| 221 |
var ts = newTotalTime - _totalTime; |
| 222 |
_totalTime = newTotalTime; |
| 223 |
|
| 224 |
if (isSpan) |
| 225 |
_nextSpan += _parent._timeShift; |
| 226 |
if (isShift) |
| 227 |
_nextShift += _parent._timeShift; |
| 228 |
|
| 229 |
m.Disposable = _parent._scheduler.Schedule(new State { isSpan = isSpan, isShift = isShift }, ts, Tick); |
| 230 |
} |
| 231 |
|
| 232 |
struct State |
| 233 |
{ |
| 234 |
public bool isSpan; |
| 235 |
public bool isShift; |
| 236 |
} |
| 237 |
|
| 238 |
private IDisposable Tick(IScheduler self, State state) |
| 239 |
{ |
| 240 |
lock (_gate) |
| 241 |
{ |
| 242 |
// |
| 243 |
// BREAKING CHANGE v2 > v1.x - Making behavior of sending OnCompleted to the window |
| 244 |
// before sending out a new window consistent across all |
| 245 |
// overloads of Window and Buffer. Before v2, the two |
| 246 |
// operations below were reversed. |
| 247 |
// |
| 248 |
if (state.isSpan) |
| 249 |
{ |
| 250 |
var s = _q.Dequeue(); |
| 251 |
s.OnCompleted(); |
| 252 |
} |
| 253 |
|
| 254 |
if (state.isShift) |
| 255 |
{ |
| 256 |
CreateWindow(); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
CreateTimer(); |
| 261 |
|
| 262 |
return Disposable.Empty; |
| 263 |
} |
| 264 |
|
| 265 |
public void OnNext(TSource value) |
| 266 |
{ |
| 267 |
lock (_gate) |
| 268 |
{ |
| 269 |
foreach (var s in _q) |
| 270 |
s.OnNext(value); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
public void OnError(Exception error) |
| 275 |
{ |
| 276 |
lock (_gate) |
| 277 |
{ |
| 278 |
foreach (var s in _q) |
| 279 |
s.OnError(error); |
| 280 |
|
| 281 |
base._observer.OnError(error); |
| 282 |
base.Dispose(); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
public void OnCompleted() |
| 287 |
{ |
| 288 |
lock (_gate) |
| 289 |
{ |
| 290 |
foreach (var s in _q) |
| 291 |
s.OnCompleted(); |
| 292 |
|
| 293 |
base._observer.OnCompleted(); |
| 294 |
base.Dispose(); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
class TimeShiftImpl : Sink<IObservable<TSource>>, IObserver<TSource> |
| 300 |
{ |
| 301 |
private readonly Window<TSource> _parent; |
| 302 |
|
| 303 |
public TimeShiftImpl(Window<TSource> parent, IObserver<IObservable<TSource>> observer, IDisposable cancel) |
| 304 |
: base(observer, cancel) |
| 305 |
{ |
| 306 |
_parent = parent; |
| 307 |
} |
| 308 |
|
| 309 |
private object _gate; |
| 310 |
private Subject<TSource> _subject; |
| 311 |
private RefCountDisposable _refCountDisposable; |
| 312 |
|
| 313 |
public IDisposable Run() |
| 314 |
{ |
| 315 |
_gate = new object(); |
| 316 |
|
| 317 |
var groupDisposable = new CompositeDisposable(2); |
| 318 |
_refCountDisposable = new RefCountDisposable(groupDisposable); |
| 319 |
|
| 320 |
CreateWindow(); |
| 321 |
|
| 322 |
groupDisposable.Add(_parent._scheduler.SchedulePeriodic(_parent._timeSpan, Tick)); |
| 323 |
groupDisposable.Add(_parent._source.SubscribeSafe(this)); |
| 324 |
|
| 325 |
return _refCountDisposable; |
| 326 |
} |
| 327 |
|
| 328 |
private void Tick() |
| 329 |
{ |
| 330 |
lock (_gate) |
| 331 |
{ |
| 332 |
_subject.OnCompleted(); |
| 333 |
CreateWindow(); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
private void CreateWindow() |
| 338 |
{ |
| 339 |
_subject = new Subject<TSource>(); |
| 340 |
base._observer.OnNext(new WindowObservable<TSource>(_subject, _refCountDisposable)); |
| 341 |
} |
| 342 |
|
| 343 |
public void OnNext(TSource value) |
| 344 |
{ |
| 345 |
lock (_gate) |
| 346 |
{ |
| 347 |
_subject.OnNext(value); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
public void OnError(Exception error) |
| 352 |
{ |
| 353 |
lock (_gate) |
| 354 |
{ |
| 355 |
_subject.OnError(error); |
| 356 |
|
| 357 |
base._observer.OnError(error); |
| 358 |
base.Dispose(); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
public void OnCompleted() |
| 363 |
{ |
| 364 |
lock (_gate) |
| 365 |
{ |
| 366 |
_subject.OnCompleted(); |
| 367 |
|
| 368 |
base._observer.OnCompleted(); |
| 369 |
base.Dispose(); |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
class BoundedWindowImpl : Sink<IObservable<TSource>>, IObserver<TSource> |
| 375 |
{ |
| 376 |
private readonly Window<TSource> _parent; |
| 377 |
|
| 378 |
public BoundedWindowImpl(Window<TSource> parent, IObserver<IObservable<TSource>> observer, IDisposable cancel) |
| 379 |
: base(observer, cancel) |
| 380 |
{ |
| 381 |
_parent = parent; |
| 382 |
} |
| 383 |
|
| 384 |
private object _gate; |
| 385 |
private ISubject<TSource> _s; |
| 386 |
private int _n; |
| 387 |
private int _windowId; |
| 388 |
|
| 389 |
private SerialDisposable _timerD; |
| 390 |
private RefCountDisposable _refCountDisposable; |
| 391 |
|
| 392 |
public IDisposable Run() |
| 393 |
{ |
| 394 |
_gate = new object(); |
| 395 |
_s = default(ISubject<TSource>); |
| 396 |
_n = 0; |
| 397 |
_windowId = 0; |
| 398 |
|
| 399 |
_timerD = new SerialDisposable(); |
| 400 |
var groupDisposable = new CompositeDisposable(2) { _timerD }; |
| 401 |
_refCountDisposable = new RefCountDisposable(groupDisposable); |
| 402 |
|
| 403 |
_s = new Subject<TSource>(); |
| 404 |
base._observer.OnNext(new WindowObservable<TSource>(_s, _refCountDisposable)); |
| 405 |
CreateTimer(0); |
| 406 |
|
| 407 |
groupDisposable.Add(_parent._source.SubscribeSafe(this)); |
| 408 |
|
| 409 |
return _refCountDisposable; |
| 410 |
} |
| 411 |
|
| 412 |
private void CreateTimer(int id) |
| 413 |
{ |
| 414 |
var m = new SingleAssignmentDisposable(); |
| 415 |
_timerD.Disposable = m; |
| 416 |
|
| 417 |
m.Disposable = _parent._scheduler.Schedule(id, _parent._timeSpan, Tick); |
| 418 |
} |
| 419 |
|
| 420 |
private IDisposable Tick(IScheduler self, int id) |
| 421 |
{ |
| 422 |
var d = Disposable.Empty; |
| 423 |
|
| 424 |
var newId = 0; |
| 425 |
lock (_gate) |
| 426 |
{ |
| 427 |
if (id != _windowId) |
| 428 |
return d; |
| 429 |
|
| 430 |
_n = 0; |
| 431 |
newId = ++_windowId; |
| 432 |
|
| 433 |
_s.OnCompleted(); |
| 434 |
_s = new Subject<TSource>(); |
| 435 |
base._observer.OnNext(new WindowObservable<TSource>(_s, _refCountDisposable)); |
| 436 |
} |
| 437 |
|
| 438 |
CreateTimer(newId); |
| 439 |
|
| 440 |
return d; |
| 441 |
} |
| 442 |
|
| 443 |
public void OnNext(TSource value) |
| 444 |
{ |
| 445 |
var newWindow = false; |
| 446 |
var newId = 0; |
| 447 |
|
| 448 |
lock (_gate) |
| 449 |
{ |
| 450 |
_s.OnNext(value); |
| 451 |
|
| 452 |
_n++; |
| 453 |
if (_n == _parent._count) |
| 454 |
{ |
| 455 |
newWindow = true; |
| 456 |
_n = 0; |
| 457 |
newId = ++_windowId; |
| 458 |
|
| 459 |
_s.OnCompleted(); |
| 460 |
_s = new Subject<TSource>(); |
| 461 |
base._observer.OnNext(new WindowObservable<TSource>(_s, _refCountDisposable)); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
if (newWindow) |
| 466 |
CreateTimer(newId); |
| 467 |
} |
| 468 |
|
| 469 |
public void OnError(Exception error) |
| 470 |
{ |
| 471 |
lock (_gate) |
| 472 |
{ |
| 473 |
_s.OnError(error); |
| 474 |
base._observer.OnError(error); |
| 475 |
base.Dispose(); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
public void OnCompleted() |
| 480 |
{ |
| 481 |
lock (_gate) |
| 482 |
{ |
| 483 |
_s.OnCompleted(); |
| 484 |
base._observer.OnCompleted(); |
| 485 |
base.Dispose(); |
| 486 |
} |
| 487 |
} |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
class Window<TSource, TWindowClosing> : Producer<IObservable<TSource>> |
| 492 |
{ |
| 493 |
private readonly IObservable<TSource> _source; |
| 494 |
private readonly Func<IObservable<TWindowClosing>> _windowClosingSelector; |
| 495 |
private readonly IObservable<TWindowClosing> _windowBoundaries; |
| 496 |
|
| 497 |
public Window(IObservable<TSource> source, Func<IObservable<TWindowClosing>> windowClosingSelector) |
| 498 |
{ |
| 499 |
_source = source; |
| 500 |
_windowClosingSelector = windowClosingSelector; |
| 501 |
} |
| 502 |
|
| 503 |
public Window(IObservable<TSource> source, IObservable<TWindowClosing> windowBoundaries) |
| 504 |
{ |
| 505 |
_source = source; |
| 506 |
_windowBoundaries = windowBoundaries; |
| 507 |
} |
| 508 |
|
| 509 |
protected override IDisposable Run(IObserver<IObservable<TSource>> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 510 |
{ |
| 511 |
if (_windowClosingSelector != null) |
| 512 |
{ |
| 513 |
var sink = new _(this, observer, cancel); |
| 514 |
setSink(sink); |
| 515 |
return sink.Run(); |
| 516 |
} |
| 517 |
else |
| 518 |
{ |
| 519 |
var sink = new Beta(this, observer, cancel); |
| 520 |
setSink(sink); |
| 521 |
return sink.Run(); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
class _ : Sink<IObservable<TSource>>, IObserver<TSource> |
| 526 |
{ |
| 527 |
private readonly Window<TSource, TWindowClosing> _parent; |
| 528 |
|
| 529 |
public _(Window<TSource, TWindowClosing> parent, IObserver<IObservable<TSource>> observer, IDisposable cancel) |
| 530 |
: base(observer, cancel) |
| 531 |
{ |
| 532 |
_parent = parent; |
| 533 |
} |
| 534 |
|
| 535 |
private ISubject<TSource> _window; |
| 536 |
private object _gate; |
| 537 |
private AsyncLock _windowGate; |
| 538 |
|
| 539 |
private SerialDisposable _m; |
| 540 |
private RefCountDisposable _refCountDisposable; |
| 541 |
|
| 542 |
public IDisposable Run() |
| 543 |
{ |
| 544 |
_window = new Subject<TSource>(); |
| 545 |
_gate = new object(); |
| 546 |
_windowGate = new AsyncLock(); |
| 547 |
|
| 548 |
_m = new SerialDisposable(); |
| 549 |
var groupDisposable = new CompositeDisposable(2) { _m }; |
| 550 |
_refCountDisposable = new RefCountDisposable(groupDisposable); |
| 551 |
|
| 552 |
var window = new WindowObservable<TSource>(_window, _refCountDisposable); |
| 553 |
base._observer.OnNext(window); |
| 554 |
|
| 555 |
groupDisposable.Add(_parent._source.SubscribeSafe(this)); |
| 556 |
|
| 557 |
_windowGate.Wait(CreateWindowClose); |
| 558 |
|
| 559 |
return _refCountDisposable; |
| 560 |
} |
| 561 |
|
| 562 |
private void CreateWindowClose() |
| 563 |
{ |
| 564 |
var windowClose = default(IObservable<TWindowClosing>); |
| 565 |
try |
| 566 |
{ |
| 567 |
windowClose = _parent._windowClosingSelector(); |
| 568 |
} |
| 569 |
catch (Exception exception) |
| 570 |
{ |
| 571 |
lock (_gate) |
| 572 |
{ |
| 573 |
base._observer.OnError(exception); |
| 574 |
base.Dispose(); |
| 575 |
} |
| 576 |
return; |
| 577 |
} |
| 578 |
|
| 579 |
var closingSubscription = new SingleAssignmentDisposable(); |
| 580 |
_m.Disposable = closingSubscription; |
| 581 |
closingSubscription.Disposable = windowClose.SubscribeSafe(new Omega(this, closingSubscription)); |
| 582 |
} |
| 583 |
|
| 584 |
private void CloseWindow(IDisposable closingSubscription) |
| 585 |
{ |
| 586 |
closingSubscription.Dispose(); |
| 587 |
|
| 588 |
lock (_gate) |
| 589 |
{ |
| 590 |
_window.OnCompleted(); |
| 591 |
_window = new Subject<TSource>(); |
| 592 |
|
| 593 |
var window = new WindowObservable<TSource>(_window, _refCountDisposable); |
| 594 |
base._observer.OnNext(window); |
| 595 |
} |
| 596 |
|
| 597 |
_windowGate.Wait(CreateWindowClose); |
| 598 |
} |
| 599 |
|
| 600 |
class Omega : IObserver<TWindowClosing> |
| 601 |
{ |
| 602 |
private readonly _ _parent; |
| 603 |
private readonly IDisposable _self; |
| 604 |
|
| 605 |
public Omega(_ parent, IDisposable self) |
| 606 |
{ |
| 607 |
_parent = parent; |
| 608 |
_self = self; |
| 609 |
} |
| 610 |
|
| 611 |
public void OnNext(TWindowClosing value) |
| 612 |
{ |
| 613 |
_parent.CloseWindow(_self); |
| 614 |
} |
| 615 |
|
| 616 |
public void OnError(Exception error) |
| 617 |
{ |
| 618 |
_parent.OnError(error); |
| 619 |
} |
| 620 |
|
| 621 |
public void OnCompleted() |
| 622 |
{ |
| 623 |
_parent.CloseWindow(_self); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
public void OnNext(TSource value) |
| 628 |
{ |
| 629 |
lock (_gate) |
| 630 |
{ |
| 631 |
_window.OnNext(value); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
public void OnError(Exception error) |
| 636 |
{ |
| 637 |
lock (_gate) |
| 638 |
{ |
| 639 |
_window.OnError(error); |
| 640 |
base._observer.OnError(error); |
| 641 |
base.Dispose(); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
public void OnCompleted() |
| 646 |
{ |
| 647 |
lock (_gate) |
| 648 |
{ |
| 649 |
_window.OnCompleted(); |
| 650 |
base._observer.OnCompleted(); |
| 651 |
base.Dispose(); |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
class Beta : Sink<IObservable<TSource>>, IObserver<TSource> |
| 657 |
{ |
| 658 |
private readonly Window<TSource, TWindowClosing> _parent; |
| 659 |
|
| 660 |
public Beta(Window<TSource, TWindowClosing> parent, IObserver<IObservable<TSource>> observer, IDisposable cancel) |
| 661 |
: base(observer, cancel) |
| 662 |
{ |
| 663 |
_parent = parent; |
| 664 |
} |
| 665 |
|
| 666 |
private ISubject<TSource> _window; |
| 667 |
private object _gate; |
| 668 |
|
| 669 |
private RefCountDisposable _refCountDisposable; |
| 670 |
|
| 671 |
public IDisposable Run() |
| 672 |
{ |
| 673 |
_window = new Subject<TSource>(); |
| 674 |
_gate = new object(); |
| 675 |
|
| 676 |
var d = new CompositeDisposable(2); |
| 677 |
_refCountDisposable = new RefCountDisposable(d); |
| 678 |
|
| 679 |
var window = new WindowObservable<TSource>(_window, _refCountDisposable); |
| 680 |
base._observer.OnNext(window); |
| 681 |
|
| 682 |
d.Add(_parent._source.SubscribeSafe(this)); |
| 683 |
d.Add(_parent._windowBoundaries.SubscribeSafe(new Omega(this))); |
| 684 |
|
| 685 |
return _refCountDisposable; |
| 686 |
} |
| 687 |
|
| 688 |
class Omega : IObserver<TWindowClosing> |
| 689 |
{ |
| 690 |
private readonly Beta _parent; |
| 691 |
|
| 692 |
public Omega(Beta parent) |
| 693 |
{ |
| 694 |
_parent = parent; |
| 695 |
} |
| 696 |
|
| 697 |
public void OnNext(TWindowClosing value) |
| 698 |
{ |
| 699 |
lock (_parent._gate) |
| 700 |
{ |
| 701 |
_parent._window.OnCompleted(); |
| 702 |
_parent._window = new Subject<TSource>(); |
| 703 |
|
| 704 |
var window = new WindowObservable<TSource>(_parent._window, _parent._refCountDisposable); |
| 705 |
_parent._observer.OnNext(window); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
public void OnError(Exception error) |
| 710 |
{ |
| 711 |
_parent.OnError(error); |
| 712 |
} |
| 713 |
|
| 714 |
public void OnCompleted() |
| 715 |
{ |
| 716 |
_parent.OnCompleted(); |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
public void OnNext(TSource value) |
| 721 |
{ |
| 722 |
lock (_gate) |
| 723 |
{ |
| 724 |
_window.OnNext(value); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
public void OnError(Exception error) |
| 729 |
{ |
| 730 |
lock (_gate) |
| 731 |
{ |
| 732 |
_window.OnError(error); |
| 733 |
base._observer.OnError(error); |
| 734 |
base.Dispose(); |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
public void OnCompleted() |
| 739 |
{ |
| 740 |
lock (_gate) |
| 741 |
{ |
| 742 |
_window.OnCompleted(); |
| 743 |
base._observer.OnCompleted(); |
| 744 |
base.Dispose(); |
| 745 |
} |
| 746 |
} |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
class WindowObservable<TSource> : AddRef<TSource> |
| 751 |
{ |
| 752 |
public WindowObservable(IObservable<TSource> source, RefCountDisposable refCount) |
| 753 |
: base(source, refCount) |
| 754 |
{ |
| 755 |
} |
| 756 |
} |
| 757 |
} |
| 758 |
#endif |