RepeatUntil.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace UniRx.Operators
  6. {
  7. internal class RepeatUntilObservable<T> : OperatorObservableBase<T>
  8. {
  9. readonly IEnumerable<IObservable<T>> sources;
  10. readonly IObservable<Unit> trigger;
  11. readonly GameObject lifeTimeChecker;
  12. public RepeatUntilObservable(IEnumerable<IObservable<T>> sources, IObservable<Unit> trigger, GameObject lifeTimeChecker)
  13. : base(true)
  14. {
  15. this.sources = sources;
  16. this.trigger = trigger;
  17. this.lifeTimeChecker = lifeTimeChecker;
  18. }
  19. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  20. {
  21. return new RepeatUntil(this, observer, cancel).Run();
  22. }
  23. class RepeatUntil : OperatorObserverBase<T, T>
  24. {
  25. readonly RepeatUntilObservable<T> parent;
  26. readonly object gate = new object();
  27. IEnumerator<IObservable<T>> e;
  28. SerialDisposable subscription;
  29. SingleAssignmentDisposable schedule;
  30. Action nextSelf;
  31. bool isStopped;
  32. bool isDisposed;
  33. bool isFirstSubscribe;
  34. IDisposable stopper;
  35. public RepeatUntil(RepeatUntilObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  36. {
  37. this.parent = parent;
  38. }
  39. public IDisposable Run()
  40. {
  41. isFirstSubscribe = true;
  42. isDisposed = false;
  43. isStopped = false;
  44. e = parent.sources.GetEnumerator();
  45. subscription = new SerialDisposable();
  46. schedule = new SingleAssignmentDisposable();
  47. stopper = parent.trigger.Subscribe(_ =>
  48. {
  49. lock (gate)
  50. {
  51. isStopped = true;
  52. e.Dispose();
  53. subscription.Dispose();
  54. schedule.Dispose();
  55. observer.OnCompleted();
  56. }
  57. }, observer.OnError);
  58. schedule.Disposable = Scheduler.CurrentThread.Schedule(RecursiveRun);
  59. return new CompositeDisposable(schedule, subscription, stopper, Disposable.Create(() =>
  60. {
  61. lock (gate)
  62. {
  63. isDisposed = true;
  64. e.Dispose();
  65. }
  66. }));
  67. }
  68. void RecursiveRun(Action self)
  69. {
  70. lock (gate)
  71. {
  72. this.nextSelf = self;
  73. if (isDisposed) return;
  74. if (isStopped) return;
  75. var current = default(IObservable<T>);
  76. var hasNext = false;
  77. var ex = default(Exception);
  78. try
  79. {
  80. hasNext = e.MoveNext();
  81. if (hasNext)
  82. {
  83. current = e.Current;
  84. if (current == null) throw new InvalidOperationException("sequence is null.");
  85. }
  86. else
  87. {
  88. e.Dispose();
  89. }
  90. }
  91. catch (Exception exception)
  92. {
  93. ex = exception;
  94. e.Dispose();
  95. }
  96. if (ex != null)
  97. {
  98. stopper.Dispose();
  99. observer.OnError(ex);
  100. return;
  101. }
  102. if (!hasNext)
  103. {
  104. stopper.Dispose();
  105. observer.OnCompleted();
  106. return;
  107. }
  108. var source = e.Current;
  109. var d = new SingleAssignmentDisposable();
  110. subscription.Disposable = d;
  111. if (isFirstSubscribe)
  112. {
  113. isFirstSubscribe = false;
  114. d.Disposable = source.Subscribe(this);
  115. }
  116. else
  117. {
  118. MainThreadDispatcher.SendStartCoroutine(SubscribeAfterEndOfFrame(d, source, this, parent.lifeTimeChecker));
  119. }
  120. }
  121. }
  122. static IEnumerator SubscribeAfterEndOfFrame(SingleAssignmentDisposable d, IObservable<T> source, IObserver<T> observer, GameObject lifeTimeChecker)
  123. {
  124. yield return YieldInstructionCache.WaitForEndOfFrame;
  125. if (!d.IsDisposed && lifeTimeChecker != null)
  126. {
  127. d.Disposable = source.Subscribe(observer);
  128. }
  129. }
  130. public override void OnNext(T value)
  131. {
  132. base.observer.OnNext(value);
  133. }
  134. public override void OnError(Exception error)
  135. {
  136. try { observer.OnError(error); }
  137. finally { Dispose(); }
  138. }
  139. public override void OnCompleted()
  140. {
  141. if (!isDisposed)
  142. {
  143. this.nextSelf();
  144. }
  145. else
  146. {
  147. e.Dispose();
  148. if (!isDisposed)
  149. {
  150. try { observer.OnCompleted(); }
  151. finally { Dispose(); }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }