Do.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace UniRx.Operators
  6. {
  7. // Do, DoOnError, DoOnCompleted, DoOnTerminate, DoOnSubscribe, DoOnCancel
  8. internal class DoObservable<T> : OperatorObservableBase<T>
  9. {
  10. readonly IObservable<T> source;
  11. readonly Action<T> onNext;
  12. readonly Action<Exception> onError;
  13. readonly Action onCompleted;
  14. public DoObservable(IObservable<T> source, Action<T> onNext, Action<Exception> onError, Action onCompleted)
  15. : base(source.IsRequiredSubscribeOnCurrentThread())
  16. {
  17. this.source = source;
  18. this.onNext = onNext;
  19. this.onError = onError;
  20. this.onCompleted = onCompleted;
  21. }
  22. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  23. {
  24. return new Do(this, observer, cancel).Run();
  25. }
  26. class Do : OperatorObserverBase<T, T>
  27. {
  28. readonly DoObservable<T> parent;
  29. public Do(DoObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  30. {
  31. this.parent = parent;
  32. }
  33. public IDisposable Run()
  34. {
  35. return parent.source.Subscribe(this);
  36. }
  37. public override void OnNext(T value)
  38. {
  39. try
  40. {
  41. parent.onNext(value);
  42. }
  43. catch (Exception ex)
  44. {
  45. try { observer.OnError(ex); } finally { Dispose(); };
  46. return;
  47. }
  48. base.observer.OnNext(value);
  49. }
  50. public override void OnError(Exception error)
  51. {
  52. try
  53. {
  54. parent.onError(error);
  55. }
  56. catch (Exception ex)
  57. {
  58. try { observer.OnError(ex); } finally { Dispose(); };
  59. return;
  60. }
  61. try { observer.OnError(error); } finally { Dispose(); };
  62. }
  63. public override void OnCompleted()
  64. {
  65. try
  66. {
  67. parent.onCompleted();
  68. }
  69. catch (Exception ex)
  70. {
  71. base.observer.OnError(ex);
  72. Dispose();
  73. return;
  74. }
  75. try { observer.OnCompleted(); } finally { Dispose(); };
  76. }
  77. }
  78. }
  79. internal class DoObserverObservable<T> : OperatorObservableBase<T>
  80. {
  81. readonly IObservable<T> source;
  82. readonly IObserver<T> observer;
  83. public DoObserverObservable(IObservable<T> source, IObserver<T> observer)
  84. : base(source.IsRequiredSubscribeOnCurrentThread())
  85. {
  86. this.source = source;
  87. this.observer = observer;
  88. }
  89. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  90. {
  91. return new Do(this, observer, cancel).Run();
  92. }
  93. class Do : OperatorObserverBase<T, T>
  94. {
  95. readonly DoObserverObservable<T> parent;
  96. public Do(DoObserverObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  97. {
  98. this.parent = parent;
  99. }
  100. public IDisposable Run()
  101. {
  102. return parent.source.Subscribe(this);
  103. }
  104. public override void OnNext(T value)
  105. {
  106. try
  107. {
  108. parent.observer.OnNext(value);
  109. }
  110. catch (Exception ex)
  111. {
  112. try { observer.OnError(ex); }
  113. finally { Dispose(); }
  114. return;
  115. }
  116. base.observer.OnNext(value);
  117. }
  118. public override void OnError(Exception error)
  119. {
  120. try
  121. {
  122. parent.observer.OnError(error);
  123. }
  124. catch (Exception ex)
  125. {
  126. try { observer.OnError(ex); }
  127. finally { Dispose(); }
  128. return;
  129. }
  130. try { observer.OnError(error); }
  131. finally { Dispose(); }
  132. }
  133. public override void OnCompleted()
  134. {
  135. try
  136. {
  137. parent.observer.OnCompleted();
  138. }
  139. catch (Exception ex)
  140. {
  141. try { observer.OnError(ex); }
  142. finally { Dispose(); }
  143. return;
  144. }
  145. try { observer.OnCompleted(); }
  146. finally { Dispose(); }
  147. }
  148. }
  149. }
  150. internal class DoOnErrorObservable<T> : OperatorObservableBase<T>
  151. {
  152. readonly IObservable<T> source;
  153. readonly Action<Exception> onError;
  154. public DoOnErrorObservable(IObservable<T> source, Action<Exception> onError)
  155. : base(source.IsRequiredSubscribeOnCurrentThread())
  156. {
  157. this.source = source;
  158. this.onError = onError;
  159. }
  160. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  161. {
  162. return new DoOnError(this, observer, cancel).Run();
  163. }
  164. class DoOnError : OperatorObserverBase<T, T>
  165. {
  166. readonly DoOnErrorObservable<T> parent;
  167. public DoOnError(DoOnErrorObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  168. {
  169. this.parent = parent;
  170. }
  171. public IDisposable Run()
  172. {
  173. return parent.source.Subscribe(this);
  174. }
  175. public override void OnNext(T value)
  176. {
  177. base.observer.OnNext(value);
  178. }
  179. public override void OnError(Exception error)
  180. {
  181. try
  182. {
  183. parent.onError(error);
  184. }
  185. catch (Exception ex)
  186. {
  187. try { observer.OnError(ex); }
  188. finally { Dispose(); }
  189. return;
  190. }
  191. try { observer.OnError(error); }
  192. finally { Dispose(); }
  193. }
  194. public override void OnCompleted()
  195. {
  196. try { observer.OnCompleted(); }
  197. finally { Dispose(); }
  198. }
  199. }
  200. }
  201. internal class DoOnCompletedObservable<T> : OperatorObservableBase<T>
  202. {
  203. readonly IObservable<T> source;
  204. readonly Action onCompleted;
  205. public DoOnCompletedObservable(IObservable<T> source, Action onCompleted)
  206. : base(source.IsRequiredSubscribeOnCurrentThread())
  207. {
  208. this.source = source;
  209. this.onCompleted = onCompleted;
  210. }
  211. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  212. {
  213. return new DoOnCompleted(this, observer, cancel).Run();
  214. }
  215. class DoOnCompleted : OperatorObserverBase<T, T>
  216. {
  217. readonly DoOnCompletedObservable<T> parent;
  218. public DoOnCompleted(DoOnCompletedObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  219. {
  220. this.parent = parent;
  221. }
  222. public IDisposable Run()
  223. {
  224. return parent.source.Subscribe(this);
  225. }
  226. public override void OnNext(T value)
  227. {
  228. base.observer.OnNext(value);
  229. }
  230. public override void OnError(Exception error)
  231. {
  232. try { observer.OnError(error); }
  233. finally { Dispose(); }
  234. }
  235. public override void OnCompleted()
  236. {
  237. try
  238. {
  239. parent.onCompleted();
  240. }
  241. catch (Exception ex)
  242. {
  243. base.observer.OnError(ex);
  244. Dispose();
  245. return;
  246. }
  247. try { observer.OnCompleted(); } finally { Dispose(); };
  248. }
  249. }
  250. }
  251. internal class DoOnTerminateObservable<T> : OperatorObservableBase<T>
  252. {
  253. readonly IObservable<T> source;
  254. readonly Action onTerminate;
  255. public DoOnTerminateObservable(IObservable<T> source, Action onTerminate)
  256. : base(source.IsRequiredSubscribeOnCurrentThread())
  257. {
  258. this.source = source;
  259. this.onTerminate = onTerminate;
  260. }
  261. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  262. {
  263. return new DoOnTerminate(this, observer, cancel).Run();
  264. }
  265. class DoOnTerminate : OperatorObserverBase<T, T>
  266. {
  267. readonly DoOnTerminateObservable<T> parent;
  268. public DoOnTerminate(DoOnTerminateObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  269. {
  270. this.parent = parent;
  271. }
  272. public IDisposable Run()
  273. {
  274. return parent.source.Subscribe(this);
  275. }
  276. public override void OnNext(T value)
  277. {
  278. base.observer.OnNext(value);
  279. }
  280. public override void OnError(Exception error)
  281. {
  282. try
  283. {
  284. parent.onTerminate();
  285. }
  286. catch (Exception ex)
  287. {
  288. try { observer.OnError(ex); }
  289. finally { Dispose(); }
  290. return;
  291. }
  292. try { observer.OnError(error); } finally { Dispose(); };
  293. }
  294. public override void OnCompleted()
  295. {
  296. try
  297. {
  298. parent.onTerminate();
  299. }
  300. catch (Exception ex)
  301. {
  302. base.observer.OnError(ex);
  303. Dispose();
  304. return;
  305. }
  306. try { observer.OnCompleted(); } finally { Dispose(); };
  307. }
  308. }
  309. }
  310. internal class DoOnSubscribeObservable<T> : OperatorObservableBase<T>
  311. {
  312. readonly IObservable<T> source;
  313. readonly Action onSubscribe;
  314. public DoOnSubscribeObservable(IObservable<T> source, Action onSubscribe)
  315. : base(source.IsRequiredSubscribeOnCurrentThread())
  316. {
  317. this.source = source;
  318. this.onSubscribe = onSubscribe;
  319. }
  320. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  321. {
  322. return new DoOnSubscribe(this, observer, cancel).Run();
  323. }
  324. class DoOnSubscribe : OperatorObserverBase<T, T>
  325. {
  326. readonly DoOnSubscribeObservable<T> parent;
  327. public DoOnSubscribe(DoOnSubscribeObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
  328. {
  329. this.parent = parent;
  330. }
  331. public IDisposable Run()
  332. {
  333. try
  334. {
  335. parent.onSubscribe();
  336. }
  337. catch (Exception ex)
  338. {
  339. try { observer.OnError(ex); }
  340. finally { Dispose(); }
  341. return Disposable.Empty;
  342. }
  343. return parent.source.Subscribe(this);
  344. }
  345. public override void OnNext(T value)
  346. {
  347. base.observer.OnNext(value);
  348. }
  349. public override void OnError(Exception error)
  350. {
  351. try { observer.OnError(error); } finally { Dispose(); }
  352. }
  353. public override void OnCompleted()
  354. {
  355. try { observer.OnCompleted(); } finally { Dispose(); }
  356. }
  357. }
  358. }
  359. internal class DoOnCancelObservable<T> : OperatorObservableBase<T>
  360. {
  361. readonly IObservable<T> source;
  362. readonly Action onCancel;
  363. public DoOnCancelObservable(IObservable<T> source, Action onCancel)
  364. : base(source.IsRequiredSubscribeOnCurrentThread())
  365. {
  366. this.source = source;
  367. this.onCancel = onCancel;
  368. }
  369. protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
  370. {
  371. return new DoOnCancel(this, observer, cancel).Run();
  372. }
  373. class DoOnCancel : OperatorObserverBase<T, T>
  374. {
  375. readonly DoOnCancelObservable<T> parent;
  376. bool isCompletedCall = false;
  377. public DoOnCancel(DoOnCancelObservable<T> parent, IObserver<T> observer, IDisposable cancel)
  378. : base(observer, cancel)
  379. {
  380. this.parent = parent;
  381. }
  382. public IDisposable Run()
  383. {
  384. return StableCompositeDisposable.Create(parent.source.Subscribe(this), Disposable.Create(() =>
  385. {
  386. if (!isCompletedCall)
  387. {
  388. parent.onCancel();
  389. }
  390. }));
  391. }
  392. public override void OnNext(T value)
  393. {
  394. base.observer.OnNext(value);
  395. }
  396. public override void OnError(Exception error)
  397. {
  398. isCompletedCall = true;
  399. try { observer.OnError(error); } finally { Dispose(); };
  400. }
  401. public override void OnCompleted()
  402. {
  403. isCompletedCall = true;
  404. try { observer.OnCompleted(); } finally { Dispose(); };
  405. }
  406. }
  407. }
  408. }