2
0

Skip.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using UniRx.Operators;
  3. namespace UniRx.Operators
  4. {
  5. internal class SkipObservable<T> : OperatorObservableBase<T>
  6. {
  7. readonly IObservable<T> source;
  8. readonly int count;
  9. readonly TimeSpan duration;
  10. internal readonly IScheduler scheduler; // public for optimization check
  11. public SkipObservable(IObservable<T> source, int count)
  12. : base(source.IsRequiredSubscribeOnCurrentThread())
  13. {
  14. this.source = source;
  15. this.count = count;
  16. }
  17. public SkipObservable(IObservable<T> source, TimeSpan duration, IScheduler scheduler)
  18. : base(scheduler == Scheduler.CurrentThread || source.IsRequiredSubscribeOnCurrentThread())
  19. {
  20. this.source = source;
  21. this.duration = duration;
  22. this.scheduler = scheduler;
  23. }
  24. // optimize combiner
  25. public IObservable<T> Combine(int count)
  26. {
  27. // use sum
  28. // xs = 6
  29. // xs.Skip(2) = 4
  30. // xs.Skip(2).Skip(3) = 1
  31. return new SkipObservable<T>(source, this.count + count);
  32. }
  33. public IObservable<T> Combine(TimeSpan duration)
  34. {
  35. // use max
  36. // xs = 6s
  37. // xs.Skip(2s) = 2s
  38. // xs.Skip(2s).Skip(3s) = 3s
  39. return (duration <= this.duration)
  40. ? this
  41. : new SkipObservable<T>(source, duration, scheduler);
  42. }
  43. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  44. {
  45. if (scheduler == null)
  46. {
  47. return source.Subscribe(new Skip(this, observer, cancel));
  48. }
  49. else
  50. {
  51. return new Skip_(this, observer, cancel).Run();
  52. }
  53. }
  54. class Skip : OperatorObserverBase<T, T>
  55. {
  56. int remaining;
  57. public Skip(SkipObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  58. {
  59. this.remaining = parent.count;
  60. }
  61. public override void OnNext(T value)
  62. {
  63. if (remaining <= 0)
  64. {
  65. base.observer.OnNext(value);
  66. }
  67. else
  68. {
  69. remaining--;
  70. }
  71. }
  72. public override void OnError(Exception error)
  73. {
  74. try { observer.OnError(error); } finally { Dispose(); }
  75. }
  76. public override void OnCompleted()
  77. {
  78. try { observer.OnCompleted(); } finally { Dispose(); }
  79. }
  80. }
  81. class Skip_ : OperatorObserverBase<T, T>
  82. {
  83. readonly SkipObservable<T> parent;
  84. volatile bool open;
  85. public Skip_(SkipObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  86. {
  87. this.parent = parent;
  88. }
  89. public IDisposable Run()
  90. {
  91. var d1 = parent.scheduler.Schedule(parent.duration, Tick);
  92. var d2 = parent.source.Subscribe(this);
  93. return StableCompositeDisposable.Create(d1, d2);
  94. }
  95. void Tick()
  96. {
  97. open = true;
  98. }
  99. public override void OnNext(T value)
  100. {
  101. if (open)
  102. {
  103. base.observer.OnNext(value);
  104. }
  105. }
  106. public override void OnError(Exception error)
  107. {
  108. try { observer.OnError(error); } finally { Dispose(); };
  109. }
  110. public override void OnCompleted()
  111. {
  112. try { observer.OnCompleted(); } finally { Dispose(); };
  113. }
  114. }
  115. }
  116. }