SampleFrame.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. #if UniRxLibrary
  3. using UnityObservable = UniRx.ObservableUnity;
  4. #else
  5. using UnityObservable = UniRx.Observable;
  6. #endif
  7. namespace UniRx.Operators
  8. {
  9. internal class SampleFrameObservable<T> : OperatorObservableBase<T>
  10. {
  11. readonly IObservable<T> source;
  12. readonly int frameCount;
  13. readonly FrameCountType frameCountType;
  14. public SampleFrameObservable(IObservable<T> source, int frameCount, FrameCountType frameCountType) : base(source.IsRequiredSubscribeOnCurrentThread())
  15. {
  16. this.source = source;
  17. this.frameCount = frameCount;
  18. this.frameCountType = frameCountType;
  19. }
  20. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  21. {
  22. return new SampleFrame(this, observer, cancel).Run();
  23. }
  24. class SampleFrame : OperatorObserverBase<T, T>
  25. {
  26. readonly SampleFrameObservable<T> parent;
  27. readonly object gate = new object();
  28. T latestValue = default(T);
  29. bool isUpdated = false;
  30. bool isCompleted = false;
  31. SingleAssignmentDisposable sourceSubscription;
  32. public SampleFrame(SampleFrameObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  33. {
  34. this.parent = parent;
  35. }
  36. public IDisposable Run()
  37. {
  38. sourceSubscription = new SingleAssignmentDisposable();
  39. sourceSubscription.Disposable = parent.source.Subscribe(this);
  40. var scheduling = UnityObservable.IntervalFrame(parent.frameCount, parent.frameCountType)
  41. .Subscribe(new SampleFrameTick(this));
  42. return StableCompositeDisposable.Create(sourceSubscription, scheduling);
  43. }
  44. void OnNextTick(long _)
  45. {
  46. lock (gate)
  47. {
  48. if (isUpdated)
  49. {
  50. var value = latestValue;
  51. isUpdated = false;
  52. observer.OnNext(value);
  53. }
  54. if (isCompleted)
  55. {
  56. try { observer.OnCompleted(); } finally { Dispose(); }
  57. }
  58. }
  59. }
  60. public override void OnNext(T value)
  61. {
  62. lock (gate)
  63. {
  64. latestValue = value;
  65. isUpdated = true;
  66. }
  67. }
  68. public override void OnError(Exception error)
  69. {
  70. lock (gate)
  71. {
  72. try { base.observer.OnError(error); } finally { Dispose(); }
  73. }
  74. }
  75. public override void OnCompleted()
  76. {
  77. lock (gate)
  78. {
  79. isCompleted = true;
  80. sourceSubscription.Dispose();
  81. }
  82. }
  83. class SampleFrameTick : IObserver<long>
  84. {
  85. readonly SampleFrame parent;
  86. public SampleFrameTick(SampleFrame parent)
  87. {
  88. this.parent = parent;
  89. }
  90. public void OnCompleted()
  91. {
  92. }
  93. public void OnError(Exception error)
  94. {
  95. }
  96. public void OnNext(long _)
  97. {
  98. lock (parent.gate)
  99. {
  100. if (parent.isUpdated)
  101. {
  102. var value = parent.latestValue;
  103. parent.isUpdated = false;
  104. parent.observer.OnNext(value);
  105. }
  106. if (parent.isCompleted)
  107. {
  108. try { parent.observer.OnCompleted(); } finally { parent.Dispose(); }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }