Observer.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. using System;
  2. using System.Threading;
  3. using UniRx.InternalUtil;
  4. namespace UniRx
  5. {
  6. public static class Observer
  7. {
  8. internal static IObserver<T> CreateSubscribeObserver<T>(Action<T> onNext, Action<Exception> onError, Action onCompleted)
  9. {
  10. // need compare for avoid iOS AOT
  11. if (onNext == Stubs<T>.Ignore)
  12. {
  13. return new Subscribe_<T>(onError, onCompleted);
  14. }
  15. else
  16. {
  17. return new Subscribe<T>(onNext, onError, onCompleted);
  18. }
  19. }
  20. internal static IObserver<T> CreateSubscribeWithStateObserver<T, TState>(TState state, Action<T, TState> onNext, Action<Exception, TState> onError, Action<TState> onCompleted)
  21. {
  22. return new Subscribe<T, TState>(state, onNext, onError, onCompleted);
  23. }
  24. internal static IObserver<T> CreateSubscribeWithState2Observer<T, TState1, TState2>(TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<Exception, TState1, TState2> onError, Action<TState1, TState2> onCompleted)
  25. {
  26. return new Subscribe<T, TState1, TState2>(state1, state2, onNext, onError, onCompleted);
  27. }
  28. internal static IObserver<T> CreateSubscribeWithState3Observer<T, TState1, TState2, TState3>(TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<Exception, TState1, TState2, TState3> onError, Action<TState1, TState2, TState3> onCompleted)
  29. {
  30. return new Subscribe<T, TState1, TState2, TState3>(state1, state2, state3, onNext, onError, onCompleted);
  31. }
  32. public static IObserver<T> Create<T>(Action<T> onNext)
  33. {
  34. return Create<T>(onNext, UniRx.Stubs.Throw, UniRx.Stubs.Nop);
  35. }
  36. public static IObserver<T> Create<T>(Action<T> onNext, Action<Exception> onError)
  37. {
  38. return Create<T>(onNext, onError, UniRx.Stubs.Nop);
  39. }
  40. public static IObserver<T> Create<T>(Action<T> onNext, Action onCompleted)
  41. {
  42. return Create<T>(onNext, UniRx.Stubs.Throw, onCompleted);
  43. }
  44. public static IObserver<T> Create<T>(Action<T> onNext, Action<Exception> onError, Action onCompleted)
  45. {
  46. // need compare for avoid iOS AOT
  47. if (onNext == Stubs<T>.Ignore)
  48. {
  49. return new EmptyOnNextAnonymousObserver<T>(onError, onCompleted);
  50. }
  51. else
  52. {
  53. return new AnonymousObserver<T>(onNext, onError, onCompleted);
  54. }
  55. }
  56. public static IObserver<T> CreateAutoDetachObserver<T>(IObserver<T> observer, IDisposable disposable)
  57. {
  58. return new AutoDetachObserver<T>(observer, disposable);
  59. }
  60. class AnonymousObserver<T> : IObserver<T>
  61. {
  62. readonly Action<T> onNext;
  63. readonly Action<Exception> onError;
  64. readonly Action onCompleted;
  65. int isStopped = 0;
  66. public AnonymousObserver(Action<T> onNext, Action<Exception> onError, Action onCompleted)
  67. {
  68. this.onNext = onNext;
  69. this.onError = onError;
  70. this.onCompleted = onCompleted;
  71. }
  72. public void OnNext(T value)
  73. {
  74. if (isStopped == 0)
  75. {
  76. onNext(value);
  77. }
  78. }
  79. public void OnError(Exception error)
  80. {
  81. if (Interlocked.Increment(ref isStopped) == 1)
  82. {
  83. onError(error);
  84. }
  85. }
  86. public void OnCompleted()
  87. {
  88. if (Interlocked.Increment(ref isStopped) == 1)
  89. {
  90. onCompleted();
  91. }
  92. }
  93. }
  94. class EmptyOnNextAnonymousObserver<T> : IObserver<T>
  95. {
  96. readonly Action<Exception> onError;
  97. readonly Action onCompleted;
  98. int isStopped = 0;
  99. public EmptyOnNextAnonymousObserver(Action<Exception> onError, Action onCompleted)
  100. {
  101. this.onError = onError;
  102. this.onCompleted = onCompleted;
  103. }
  104. public void OnNext(T value)
  105. {
  106. }
  107. public void OnError(Exception error)
  108. {
  109. if (Interlocked.Increment(ref isStopped) == 1)
  110. {
  111. onError(error);
  112. }
  113. }
  114. public void OnCompleted()
  115. {
  116. if (Interlocked.Increment(ref isStopped) == 1)
  117. {
  118. onCompleted();
  119. }
  120. }
  121. }
  122. // same as AnonymousObserver...
  123. class Subscribe<T> : IObserver<T>
  124. {
  125. readonly Action<T> onNext;
  126. readonly Action<Exception> onError;
  127. readonly Action onCompleted;
  128. int isStopped = 0;
  129. public Subscribe(Action<T> onNext, Action<Exception> onError, Action onCompleted)
  130. {
  131. this.onNext = onNext;
  132. this.onError = onError;
  133. this.onCompleted = onCompleted;
  134. }
  135. public void OnNext(T value)
  136. {
  137. if (isStopped == 0)
  138. {
  139. onNext(value);
  140. }
  141. }
  142. public void OnError(Exception error)
  143. {
  144. if (Interlocked.Increment(ref isStopped) == 1)
  145. {
  146. onError(error);
  147. }
  148. }
  149. public void OnCompleted()
  150. {
  151. if (Interlocked.Increment(ref isStopped) == 1)
  152. {
  153. onCompleted();
  154. }
  155. }
  156. }
  157. // same as EmptyOnNextAnonymousObserver...
  158. class Subscribe_<T> : IObserver<T>
  159. {
  160. readonly Action<Exception> onError;
  161. readonly Action onCompleted;
  162. int isStopped = 0;
  163. public Subscribe_(Action<Exception> onError, Action onCompleted)
  164. {
  165. this.onError = onError;
  166. this.onCompleted = onCompleted;
  167. }
  168. public void OnNext(T value)
  169. {
  170. }
  171. public void OnError(Exception error)
  172. {
  173. if (Interlocked.Increment(ref isStopped) == 1)
  174. {
  175. onError(error);
  176. }
  177. }
  178. public void OnCompleted()
  179. {
  180. if (Interlocked.Increment(ref isStopped) == 1)
  181. {
  182. onCompleted();
  183. }
  184. }
  185. }
  186. // with state
  187. class Subscribe<T, TState> : IObserver<T>
  188. {
  189. readonly TState state;
  190. readonly Action<T, TState> onNext;
  191. readonly Action<Exception, TState> onError;
  192. readonly Action<TState> onCompleted;
  193. int isStopped = 0;
  194. public Subscribe(TState state, Action<T, TState> onNext, Action<Exception, TState> onError, Action<TState> onCompleted)
  195. {
  196. this.state = state;
  197. this.onNext = onNext;
  198. this.onError = onError;
  199. this.onCompleted = onCompleted;
  200. }
  201. public void OnNext(T value)
  202. {
  203. if (isStopped == 0)
  204. {
  205. onNext(value, state);
  206. }
  207. }
  208. public void OnError(Exception error)
  209. {
  210. if (Interlocked.Increment(ref isStopped) == 1)
  211. {
  212. onError(error, state);
  213. }
  214. }
  215. public void OnCompleted()
  216. {
  217. if (Interlocked.Increment(ref isStopped) == 1)
  218. {
  219. onCompleted(state);
  220. }
  221. }
  222. }
  223. class Subscribe<T, TState1, TState2> : IObserver<T>
  224. {
  225. readonly TState1 state1;
  226. readonly TState2 state2;
  227. readonly Action<T, TState1, TState2> onNext;
  228. readonly Action<Exception, TState1, TState2> onError;
  229. readonly Action<TState1, TState2> onCompleted;
  230. int isStopped = 0;
  231. public Subscribe(TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<Exception, TState1, TState2> onError, Action<TState1, TState2> onCompleted)
  232. {
  233. this.state1 = state1;
  234. this.state2 = state2;
  235. this.onNext = onNext;
  236. this.onError = onError;
  237. this.onCompleted = onCompleted;
  238. }
  239. public void OnNext(T value)
  240. {
  241. if (isStopped == 0)
  242. {
  243. onNext(value, state1, state2);
  244. }
  245. }
  246. public void OnError(Exception error)
  247. {
  248. if (Interlocked.Increment(ref isStopped) == 1)
  249. {
  250. onError(error, state1, state2);
  251. }
  252. }
  253. public void OnCompleted()
  254. {
  255. if (Interlocked.Increment(ref isStopped) == 1)
  256. {
  257. onCompleted(state1, state2);
  258. }
  259. }
  260. }
  261. class Subscribe<T, TState1, TState2, TState3> : IObserver<T>
  262. {
  263. readonly TState1 state1;
  264. readonly TState2 state2;
  265. readonly TState3 state3;
  266. readonly Action<T, TState1, TState2, TState3> onNext;
  267. readonly Action<Exception, TState1, TState2, TState3> onError;
  268. readonly Action<TState1, TState2, TState3> onCompleted;
  269. int isStopped = 0;
  270. public Subscribe(TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<Exception, TState1, TState2, TState3> onError, Action<TState1, TState2, TState3> onCompleted)
  271. {
  272. this.state1 = state1;
  273. this.state2 = state2;
  274. this.state3 = state3;
  275. this.onNext = onNext;
  276. this.onError = onError;
  277. this.onCompleted = onCompleted;
  278. }
  279. public void OnNext(T value)
  280. {
  281. if (isStopped == 0)
  282. {
  283. onNext(value, state1, state2, state3);
  284. }
  285. }
  286. public void OnError(Exception error)
  287. {
  288. if (Interlocked.Increment(ref isStopped) == 1)
  289. {
  290. onError(error, state1, state2, state3);
  291. }
  292. }
  293. public void OnCompleted()
  294. {
  295. if (Interlocked.Increment(ref isStopped) == 1)
  296. {
  297. onCompleted(state1, state2, state3);
  298. }
  299. }
  300. }
  301. class AutoDetachObserver<T> : UniRx.Operators.OperatorObserverBase<T, T>
  302. {
  303. public AutoDetachObserver(IObserver<T> observer, IDisposable cancel)
  304. : base(observer, cancel)
  305. {
  306. }
  307. public override void OnNext(T value)
  308. {
  309. try
  310. {
  311. base.observer.OnNext(value);
  312. }
  313. catch
  314. {
  315. Dispose();
  316. throw;
  317. }
  318. }
  319. public override void OnError(Exception error)
  320. {
  321. try { observer.OnError(error); }
  322. finally { Dispose(); }
  323. }
  324. public override void OnCompleted()
  325. {
  326. try { observer.OnCompleted(); }
  327. finally { Dispose(); }
  328. }
  329. }
  330. }
  331. public static partial class ObserverExtensions
  332. {
  333. public static IObserver<T> Synchronize<T>(this IObserver<T> observer)
  334. {
  335. return new UniRx.Operators.SynchronizedObserver<T>(observer, new object());
  336. }
  337. public static IObserver<T> Synchronize<T>(this IObserver<T> observer, object gate)
  338. {
  339. return new UniRx.Operators.SynchronizedObserver<T>(observer, gate);
  340. }
  341. }
  342. public static partial class ObservableExtensions
  343. {
  344. public static IDisposable Subscribe<T>(this IObservable<T> source)
  345. {
  346. return source.Subscribe(UniRx.InternalUtil.ThrowObserver<T>.Instance);
  347. }
  348. public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext)
  349. {
  350. return source.Subscribe(Observer.CreateSubscribeObserver(onNext, Stubs.Throw, Stubs.Nop));
  351. }
  352. public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action<Exception> onError)
  353. {
  354. return source.Subscribe(Observer.CreateSubscribeObserver(onNext, onError, Stubs.Nop));
  355. }
  356. public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action onCompleted)
  357. {
  358. return source.Subscribe(Observer.CreateSubscribeObserver(onNext, Stubs.Throw, onCompleted));
  359. }
  360. public static IDisposable Subscribe<T>(this IObservable<T> source, Action<T> onNext, Action<Exception> onError, Action onCompleted)
  361. {
  362. return source.Subscribe(Observer.CreateSubscribeObserver(onNext, onError, onCompleted));
  363. }
  364. public static IDisposable SubscribeWithState<T, TState>(this IObservable<T> source, TState state, Action<T, TState> onNext)
  365. {
  366. return source.Subscribe(Observer.CreateSubscribeWithStateObserver(state, onNext, Stubs<TState>.Throw, Stubs<TState>.Ignore));
  367. }
  368. public static IDisposable SubscribeWithState<T, TState>(this IObservable<T> source, TState state, Action<T, TState> onNext, Action<Exception, TState> onError)
  369. {
  370. return source.Subscribe(Observer.CreateSubscribeWithStateObserver(state, onNext, onError, Stubs<TState>.Ignore));
  371. }
  372. public static IDisposable SubscribeWithState<T, TState>(this IObservable<T> source, TState state, Action<T, TState> onNext, Action<TState> onCompleted)
  373. {
  374. return source.Subscribe(Observer.CreateSubscribeWithStateObserver(state, onNext, Stubs<TState>.Throw, onCompleted));
  375. }
  376. public static IDisposable SubscribeWithState<T, TState>(this IObservable<T> source, TState state, Action<T, TState> onNext, Action<Exception, TState> onError, Action<TState> onCompleted)
  377. {
  378. return source.Subscribe(Observer.CreateSubscribeWithStateObserver(state, onNext, onError, onCompleted));
  379. }
  380. public static IDisposable SubscribeWithState2<T, TState1, TState2>(this IObservable<T> source, TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext)
  381. {
  382. return source.Subscribe(Observer.CreateSubscribeWithState2Observer(state1, state2, onNext, Stubs<TState1, TState2>.Throw, Stubs<TState1, TState2>.Ignore));
  383. }
  384. public static IDisposable SubscribeWithState2<T, TState1, TState2>(this IObservable<T> source, TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<Exception, TState1, TState2> onError)
  385. {
  386. return source.Subscribe(Observer.CreateSubscribeWithState2Observer(state1, state2, onNext, onError, Stubs<TState1, TState2>.Ignore));
  387. }
  388. public static IDisposable SubscribeWithState2<T, TState1, TState2>(this IObservable<T> source, TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<TState1, TState2> onCompleted)
  389. {
  390. return source.Subscribe(Observer.CreateSubscribeWithState2Observer(state1, state2, onNext, Stubs<TState1, TState2>.Throw, onCompleted));
  391. }
  392. public static IDisposable SubscribeWithState2<T, TState1, TState2>(this IObservable<T> source, TState1 state1, TState2 state2, Action<T, TState1, TState2> onNext, Action<Exception, TState1, TState2> onError, Action<TState1, TState2> onCompleted)
  393. {
  394. return source.Subscribe(Observer.CreateSubscribeWithState2Observer(state1, state2, onNext, onError, onCompleted));
  395. }
  396. public static IDisposable SubscribeWithState3<T, TState1, TState2, TState3>(this IObservable<T> source, TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext)
  397. {
  398. return source.Subscribe(Observer.CreateSubscribeWithState3Observer(state1, state2, state3, onNext, Stubs<TState1, TState2, TState3>.Throw, Stubs<TState1, TState2, TState3>.Ignore));
  399. }
  400. public static IDisposable SubscribeWithState3<T, TState1, TState2, TState3>(this IObservable<T> source, TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<Exception, TState1, TState2, TState3> onError)
  401. {
  402. return source.Subscribe(Observer.CreateSubscribeWithState3Observer(state1, state2, state3, onNext, onError, Stubs<TState1, TState2, TState3>.Ignore));
  403. }
  404. public static IDisposable SubscribeWithState3<T, TState1, TState2, TState3>(this IObservable<T> source, TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<TState1, TState2, TState3> onCompleted)
  405. {
  406. return source.Subscribe(Observer.CreateSubscribeWithState3Observer(state1, state2, state3, onNext, Stubs<TState1, TState2, TState3>.Throw, onCompleted));
  407. }
  408. public static IDisposable SubscribeWithState3<T, TState1, TState2, TState3>(this IObservable<T> source, TState1 state1, TState2 state2, TState3 state3, Action<T, TState1, TState2, TState3> onNext, Action<Exception, TState1, TState2, TState3> onError, Action<TState1, TState2, TState3> onCompleted)
  409. {
  410. return source.Subscribe(Observer.CreateSubscribeWithState3Observer(state1, state2, state3, onNext, onError, onCompleted));
  411. }
  412. }
  413. internal static class Stubs
  414. {
  415. public static readonly Action Nop = () => { };
  416. public static readonly Action<Exception> Throw = ex => { ex.Throw(); };
  417. // marker for CatchIgnore and Catch avoid iOS AOT problem.
  418. public static IObservable<TSource> CatchIgnore<TSource>(Exception ex)
  419. {
  420. return Observable.Empty<TSource>();
  421. }
  422. }
  423. internal static class Stubs<T>
  424. {
  425. public static readonly Action<T> Ignore = (T t) => { };
  426. public static readonly Func<T, T> Identity = (T t) => t;
  427. public static readonly Action<Exception, T> Throw = (ex, _) => { ex.Throw(); };
  428. }
  429. internal static class Stubs<T1, T2>
  430. {
  431. public static readonly Action<T1, T2> Ignore = (x, y) => { };
  432. public static readonly Action<Exception, T1, T2> Throw = (ex, _, __) => { ex.Throw(); };
  433. }
  434. internal static class Stubs<T1, T2, T3>
  435. {
  436. public static readonly Action<T1, T2, T3> Ignore = (x, y, z) => { };
  437. public static readonly Action<Exception, T1, T2, T3> Throw = (ex, _, __, ___) => { ex.Throw(); };
  438. }
  439. }