BehaviorSubject.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using UniRx.InternalUtil;
  3. namespace UniRx
  4. {
  5. public sealed class BehaviorSubject<T> : ISubject<T>, IDisposable, IOptimizedObservable<T>
  6. {
  7. object observerLock = new object();
  8. bool isStopped;
  9. bool isDisposed;
  10. T lastValue;
  11. Exception lastError;
  12. IObserver<T> outObserver = EmptyObserver<T>.Instance;
  13. public BehaviorSubject(T defaultValue)
  14. {
  15. lastValue = defaultValue;
  16. }
  17. public T Value
  18. {
  19. get
  20. {
  21. ThrowIfDisposed();
  22. if (lastError != null) lastError.Throw();
  23. return lastValue;
  24. }
  25. }
  26. public bool HasObservers
  27. {
  28. get
  29. {
  30. return !(outObserver is EmptyObserver<T>) && !isStopped && !isDisposed;
  31. }
  32. }
  33. public void OnCompleted()
  34. {
  35. IObserver<T> old;
  36. lock (observerLock)
  37. {
  38. ThrowIfDisposed();
  39. if (isStopped) return;
  40. old = outObserver;
  41. outObserver = EmptyObserver<T>.Instance;
  42. isStopped = true;
  43. }
  44. old.OnCompleted();
  45. }
  46. public void OnError(Exception error)
  47. {
  48. if (error == null) throw new ArgumentNullException("error");
  49. IObserver<T> old;
  50. lock (observerLock)
  51. {
  52. ThrowIfDisposed();
  53. if (isStopped) return;
  54. old = outObserver;
  55. outObserver = EmptyObserver<T>.Instance;
  56. isStopped = true;
  57. lastError = error;
  58. }
  59. old.OnError(error);
  60. }
  61. public void OnNext(T value)
  62. {
  63. IObserver<T> current;
  64. lock (observerLock)
  65. {
  66. if (isStopped) return;
  67. lastValue = value;
  68. current = outObserver;
  69. }
  70. current.OnNext(value);
  71. }
  72. public IDisposable Subscribe(IObserver<T> observer)
  73. {
  74. if (observer == null) throw new ArgumentNullException("observer");
  75. var ex = default(Exception);
  76. var v = default(T);
  77. var subscription = default(Subscription);
  78. lock (observerLock)
  79. {
  80. ThrowIfDisposed();
  81. if (!isStopped)
  82. {
  83. var listObserver = outObserver as ListObserver<T>;
  84. if (listObserver != null)
  85. {
  86. outObserver = listObserver.Add(observer);
  87. }
  88. else
  89. {
  90. var current = outObserver;
  91. if (current is EmptyObserver<T>)
  92. {
  93. outObserver = observer;
  94. }
  95. else
  96. {
  97. outObserver = new ListObserver<T>(new ImmutableList<IObserver<T>>(new[] { current, observer }));
  98. }
  99. }
  100. v = lastValue;
  101. subscription = new Subscription(this, observer);
  102. }
  103. else
  104. {
  105. ex = lastError;
  106. }
  107. }
  108. if (subscription != null)
  109. {
  110. observer.OnNext(v);
  111. return subscription;
  112. }
  113. else if (ex != null)
  114. {
  115. observer.OnError(ex);
  116. }
  117. else
  118. {
  119. observer.OnCompleted();
  120. }
  121. return Disposable.Empty;
  122. }
  123. public void Dispose()
  124. {
  125. lock (observerLock)
  126. {
  127. isDisposed = true;
  128. outObserver = DisposedObserver<T>.Instance;
  129. lastError = null;
  130. lastValue = default(T);
  131. }
  132. }
  133. void ThrowIfDisposed()
  134. {
  135. if (isDisposed) throw new ObjectDisposedException("");
  136. }
  137. public bool IsRequiredSubscribeOnCurrentThread()
  138. {
  139. return false;
  140. }
  141. class Subscription : IDisposable
  142. {
  143. readonly object gate = new object();
  144. BehaviorSubject<T> parent;
  145. IObserver<T> unsubscribeTarget;
  146. public Subscription(BehaviorSubject<T> parent, IObserver<T> unsubscribeTarget)
  147. {
  148. this.parent = parent;
  149. this.unsubscribeTarget = unsubscribeTarget;
  150. }
  151. public void Dispose()
  152. {
  153. lock (gate)
  154. {
  155. if (parent != null)
  156. {
  157. lock (parent.observerLock)
  158. {
  159. var listObserver = parent.outObserver as ListObserver<T>;
  160. if (listObserver != null)
  161. {
  162. parent.outObserver = listObserver.Remove(unsubscribeTarget);
  163. }
  164. else
  165. {
  166. parent.outObserver = EmptyObserver<T>.Instance;
  167. }
  168. unsubscribeTarget = null;
  169. parent = null;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }