Observer.cs 19 KB

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