ObserveOn.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UniRx.Operators
  4. {
  5. internal class ObserveOnObservable<T> : OperatorObservableBase<T>
  6. {
  7. readonly IObservable<T> source;
  8. readonly IScheduler scheduler;
  9. public ObserveOnObservable(IObservable<T> source, IScheduler scheduler)
  10. : base(source.IsRequiredSubscribeOnCurrentThread())
  11. {
  12. this.source = source;
  13. this.scheduler = scheduler;
  14. }
  15. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  16. {
  17. var queueing = scheduler as ISchedulerQueueing;
  18. if (queueing == null)
  19. {
  20. return new ObserveOn(this, observer, cancel).Run();
  21. }
  22. else
  23. {
  24. return new ObserveOn_(this, queueing, observer, cancel).Run();
  25. }
  26. }
  27. class ObserveOn : OperatorObserverBase<T, T>
  28. {
  29. class SchedulableAction : IDisposable
  30. {
  31. public Notification<T> data;
  32. public LinkedListNode<SchedulableAction> node;
  33. public IDisposable schedule;
  34. public void Dispose()
  35. {
  36. if (schedule != null)
  37. schedule.Dispose();
  38. schedule = null;
  39. if (node.List != null)
  40. {
  41. node.List.Remove(node);
  42. }
  43. }
  44. public bool IsScheduled { get { return schedule != null; } }
  45. }
  46. readonly ObserveOnObservable<T> parent;
  47. readonly LinkedList<SchedulableAction> actions = new LinkedList<SchedulableAction>();
  48. bool isDisposed;
  49. public ObserveOn(ObserveOnObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  50. {
  51. this.parent = parent;
  52. }
  53. public IDisposable Run()
  54. {
  55. isDisposed = false;
  56. var sourceDisposable = parent.source.Subscribe(this);
  57. return StableCompositeDisposable.Create(sourceDisposable, Disposable.Create(() =>
  58. {
  59. lock (actions)
  60. {
  61. isDisposed = true;
  62. while (actions.Count > 0)
  63. {
  64. // Dispose will both cancel the action (if not already running)
  65. // and remove it from 'actions'
  66. actions.First.Value.Dispose();
  67. }
  68. }
  69. }));
  70. }
  71. public override void OnNext(T value)
  72. {
  73. QueueAction(new Notification<T>.OnNextNotification(value));
  74. }
  75. public override void OnError(Exception error)
  76. {
  77. QueueAction(new Notification<T>.OnErrorNotification(error));
  78. }
  79. public override void OnCompleted()
  80. {
  81. QueueAction(new Notification<T>.OnCompletedNotification());
  82. }
  83. private void QueueAction(Notification<T> data)
  84. {
  85. var action = new SchedulableAction { data = data };
  86. lock (actions)
  87. {
  88. if (isDisposed) return;
  89. action.node = actions.AddLast(action);
  90. ProcessNext();
  91. }
  92. }
  93. private void ProcessNext()
  94. {
  95. lock (actions)
  96. {
  97. if (actions.Count == 0 || isDisposed)
  98. return;
  99. var action = actions.First.Value;
  100. if (action.IsScheduled)
  101. return;
  102. action.schedule = parent.scheduler.Schedule(() =>
  103. {
  104. try
  105. {
  106. switch (action.data.Kind)
  107. {
  108. case NotificationKind.OnNext:
  109. observer.OnNext(action.data.Value);
  110. break;
  111. case NotificationKind.OnError:
  112. observer.OnError(action.data.Exception);
  113. break;
  114. case NotificationKind.OnCompleted:
  115. observer.OnCompleted();
  116. break;
  117. }
  118. }
  119. finally
  120. {
  121. lock (actions)
  122. {
  123. action.Dispose();
  124. }
  125. if (action.data.Kind == NotificationKind.OnNext)
  126. ProcessNext();
  127. else
  128. Dispose();
  129. }
  130. });
  131. }
  132. }
  133. }
  134. class ObserveOn_ : OperatorObserverBase<T, T>
  135. {
  136. readonly ObserveOnObservable<T> parent;
  137. readonly ISchedulerQueueing scheduler;
  138. readonly BooleanDisposable isDisposed;
  139. readonly Action<T> onNext;
  140. public ObserveOn_(ObserveOnObservable<T> parent, ISchedulerQueueing scheduler, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  141. {
  142. this.parent = parent;
  143. this.scheduler = scheduler;
  144. this.isDisposed = new BooleanDisposable();
  145. this.onNext = new Action<T>(OnNext_); // cache delegate
  146. }
  147. public IDisposable Run()
  148. {
  149. var sourceDisposable = parent.source.Subscribe(this);
  150. return StableCompositeDisposable.Create(sourceDisposable, isDisposed);
  151. }
  152. void OnNext_(T value)
  153. {
  154. base.observer.OnNext(value);
  155. }
  156. void OnError_(Exception error)
  157. {
  158. try { observer.OnError(error); } finally { Dispose(); };
  159. }
  160. void OnCompleted_(Unit _)
  161. {
  162. try { observer.OnCompleted(); } finally { Dispose(); };
  163. }
  164. public override void OnNext(T value)
  165. {
  166. scheduler.ScheduleQueueing(isDisposed, value, onNext);
  167. }
  168. public override void OnError(Exception error)
  169. {
  170. scheduler.ScheduleQueueing(isDisposed, error, OnError_);
  171. }
  172. public override void OnCompleted()
  173. {
  174. scheduler.ScheduleQueueing(isDisposed, Unit.Default, OnCompleted_);
  175. }
  176. }
  177. }
  178. }