1
0

SelectMany.cs 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_PERF
  3. using System.Collections.Generic;
  4. using System.Reactive.Disposables;
  5. #if !NO_TPL
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. #endif
  9. namespace System.Reactive.Linq.Observαble
  10. {
  11. class SelectMany<TSource, TCollection, TResult> : Producer<TResult>
  12. {
  13. private readonly IObservable<TSource> _source;
  14. private readonly Func<TSource, IObservable<TCollection>> _collectionSelector;
  15. private readonly Func<TSource, int, IObservable<TCollection>> _collectionSelectorI;
  16. private readonly Func<TSource, IEnumerable<TCollection>> _collectionSelectorE;
  17. private readonly Func<TSource, int, IEnumerable<TCollection>> _collectionSelectorEI;
  18. private readonly Func<TSource, TCollection, TResult> _resultSelector;
  19. private readonly Func<TSource, int, TCollection, int, TResult> _resultSelectorI;
  20. public SelectMany(IObservable<TSource> source, Func<TSource, IObservable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
  21. {
  22. _source = source;
  23. _collectionSelector = collectionSelector;
  24. _resultSelector = resultSelector;
  25. }
  26. public SelectMany(IObservable<TSource> source, Func<TSource, int, IObservable<TCollection>> collectionSelector, Func<TSource, int, TCollection, int, TResult> resultSelector)
  27. {
  28. _source = source;
  29. _collectionSelectorI = collectionSelector;
  30. _resultSelectorI = resultSelector;
  31. }
  32. public SelectMany(IObservable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
  33. {
  34. _source = source;
  35. _collectionSelectorE = collectionSelector;
  36. _resultSelector = resultSelector;
  37. }
  38. public SelectMany(IObservable<TSource> source, Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, int, TCollection, int, TResult> resultSelector)
  39. {
  40. _source = source;
  41. _collectionSelectorEI = collectionSelector;
  42. _resultSelectorI = resultSelector;
  43. }
  44. #if !NO_TPL
  45. private readonly Func<TSource, CancellationToken, Task<TCollection>> _collectionSelectorT;
  46. private readonly Func<TSource, int, CancellationToken, Task<TCollection>> _collectionSelectorTI;
  47. private readonly Func<TSource, int, TCollection, TResult> _resultSelectorTI;
  48. public SelectMany(IObservable<TSource> source, Func<TSource, CancellationToken, Task<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
  49. {
  50. _source = source;
  51. _collectionSelectorT = collectionSelector;
  52. _resultSelector = resultSelector;
  53. }
  54. public SelectMany(IObservable<TSource> source, Func<TSource, int, CancellationToken, Task<TCollection>> collectionSelector, Func<TSource, int, TCollection, TResult> resultSelector)
  55. {
  56. _source = source;
  57. _collectionSelectorTI = collectionSelector;
  58. _resultSelectorTI = resultSelector;
  59. }
  60. #endif
  61. protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
  62. {
  63. if (_collectionSelector != null)
  64. {
  65. var sink = new _(this, observer, cancel);
  66. setSink(sink);
  67. return sink.Run();
  68. }
  69. else if (_collectionSelectorI != null)
  70. {
  71. var sink = new π(this, observer, cancel);
  72. setSink(sink);
  73. return sink.Run();
  74. }
  75. #if !NO_TPL
  76. else if (_collectionSelectorT != null)
  77. {
  78. var sink = new τ(this, observer, cancel);
  79. setSink(sink);
  80. return sink.Run();
  81. }
  82. else if (_collectionSelectorTI != null)
  83. {
  84. var sink = new σ(this, observer, cancel);
  85. setSink(sink);
  86. return sink.Run();
  87. }
  88. #endif
  89. else if (_collectionSelectorE != null)
  90. {
  91. var sink = new ε(this, observer, cancel);
  92. setSink(sink);
  93. return _source.SubscribeSafe(sink);
  94. }
  95. else
  96. {
  97. var sink = new ω(this, observer, cancel);
  98. setSink(sink);
  99. return _source.SubscribeSafe(sink);
  100. }
  101. }
  102. class _ : Sink<TResult>, IObserver<TSource>
  103. {
  104. private readonly SelectMany<TSource, TCollection, TResult> _parent;
  105. public _(SelectMany<TSource, TCollection, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  106. : base(observer, cancel)
  107. {
  108. _parent = parent;
  109. }
  110. private object _gate;
  111. private bool _isStopped;
  112. private CompositeDisposable _group;
  113. private SingleAssignmentDisposable _sourceSubscription;
  114. public IDisposable Run()
  115. {
  116. _gate = new object();
  117. _isStopped = false;
  118. _group = new CompositeDisposable();
  119. _sourceSubscription = new SingleAssignmentDisposable();
  120. _group.Add(_sourceSubscription);
  121. _sourceSubscription.Disposable = _parent._source.SubscribeSafe(this);
  122. return _group;
  123. }
  124. public void OnNext(TSource value)
  125. {
  126. var collection = default(IObservable<TCollection>);
  127. try
  128. {
  129. collection = _parent._collectionSelector(value);
  130. }
  131. catch (Exception ex)
  132. {
  133. lock (_gate)
  134. {
  135. base._observer.OnError(ex);
  136. base.Dispose();
  137. }
  138. return;
  139. }
  140. var innerSubscription = new SingleAssignmentDisposable();
  141. _group.Add(innerSubscription);
  142. innerSubscription.Disposable = collection.SubscribeSafe(new ι(this, value, innerSubscription));
  143. }
  144. public void OnError(Exception error)
  145. {
  146. lock (_gate)
  147. {
  148. base._observer.OnError(error);
  149. base.Dispose();
  150. }
  151. }
  152. public void OnCompleted()
  153. {
  154. _isStopped = true;
  155. if (_group.Count == 1)
  156. {
  157. //
  158. // Notice there can be a race between OnCompleted of the source and any
  159. // of the inner sequences, where both see _group.Count == 1, and one is
  160. // waiting for the lock. There won't be a double OnCompleted observation
  161. // though, because the call to Dispose silences the observer by swapping
  162. // in a NopObserver<T>.
  163. //
  164. lock (_gate)
  165. {
  166. base._observer.OnCompleted();
  167. base.Dispose();
  168. }
  169. }
  170. else
  171. {
  172. _sourceSubscription.Dispose();
  173. }
  174. }
  175. class ι : IObserver<TCollection>
  176. {
  177. private readonly _ _parent;
  178. private readonly TSource _value;
  179. private readonly IDisposable _self;
  180. public ι(_ parent, TSource value, IDisposable self)
  181. {
  182. _parent = parent;
  183. _value = value;
  184. _self = self;
  185. }
  186. public void OnNext(TCollection value)
  187. {
  188. var res = default(TResult);
  189. try
  190. {
  191. res = _parent._parent._resultSelector(_value, value);
  192. }
  193. catch (Exception ex)
  194. {
  195. lock (_parent._gate)
  196. {
  197. _parent._observer.OnError(ex);
  198. _parent.Dispose();
  199. }
  200. return;
  201. }
  202. lock (_parent._gate)
  203. _parent._observer.OnNext(res);
  204. }
  205. public void OnError(Exception error)
  206. {
  207. lock (_parent._gate)
  208. {
  209. _parent._observer.OnError(error);
  210. _parent.Dispose();
  211. }
  212. }
  213. public void OnCompleted()
  214. {
  215. _parent._group.Remove(_self);
  216. if (_parent._isStopped && _parent._group.Count == 1)
  217. {
  218. //
  219. // Notice there can be a race between OnCompleted of the source and any
  220. // of the inner sequences, where both see _group.Count == 1, and one is
  221. // waiting for the lock. There won't be a double OnCompleted observation
  222. // though, because the call to Dispose silences the observer by swapping
  223. // in a NopObserver<T>.
  224. //
  225. lock (_parent._gate)
  226. {
  227. _parent._observer.OnCompleted();
  228. _parent.Dispose();
  229. }
  230. }
  231. }
  232. }
  233. }
  234. class π : Sink<TResult>, IObserver<TSource>
  235. {
  236. private readonly SelectMany<TSource, TCollection, TResult> _parent;
  237. public π(SelectMany<TSource, TCollection, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  238. : base(observer, cancel)
  239. {
  240. _parent = parent;
  241. }
  242. private object _gate;
  243. private bool _isStopped;
  244. private CompositeDisposable _group;
  245. private SingleAssignmentDisposable _sourceSubscription;
  246. private int _index;
  247. public IDisposable Run()
  248. {
  249. _gate = new object();
  250. _isStopped = false;
  251. _group = new CompositeDisposable();
  252. _sourceSubscription = new SingleAssignmentDisposable();
  253. _group.Add(_sourceSubscription);
  254. _sourceSubscription.Disposable = _parent._source.SubscribeSafe(this);
  255. return _group;
  256. }
  257. public void OnNext(TSource value)
  258. {
  259. var index = checked(_index++);
  260. var collection = default(IObservable<TCollection>);
  261. try
  262. {
  263. collection = _parent._collectionSelectorI(value, index);
  264. }
  265. catch (Exception ex)
  266. {
  267. lock (_gate)
  268. {
  269. base._observer.OnError(ex);
  270. base.Dispose();
  271. }
  272. return;
  273. }
  274. var innerSubscription = new SingleAssignmentDisposable();
  275. _group.Add(innerSubscription);
  276. innerSubscription.Disposable = collection.SubscribeSafe(new ι(this, value, index, innerSubscription));
  277. }
  278. public void OnError(Exception error)
  279. {
  280. lock (_gate)
  281. {
  282. base._observer.OnError(error);
  283. base.Dispose();
  284. }
  285. }
  286. public void OnCompleted()
  287. {
  288. _isStopped = true;
  289. if (_group.Count == 1)
  290. {
  291. //
  292. // Notice there can be a race between OnCompleted of the source and any
  293. // of the inner sequences, where both see _group.Count == 1, and one is
  294. // waiting for the lock. There won't be a double OnCompleted observation
  295. // though, because the call to Dispose silences the observer by swapping
  296. // in a NopObserver<T>.
  297. //
  298. lock (_gate)
  299. {
  300. base._observer.OnCompleted();
  301. base.Dispose();
  302. }
  303. }
  304. else
  305. {
  306. _sourceSubscription.Dispose();
  307. }
  308. }
  309. class ι : IObserver<TCollection>
  310. {
  311. private readonly π _parent;
  312. private readonly TSource _value;
  313. private readonly int _valueIndex;
  314. private readonly IDisposable _self;
  315. public ι(π parent, TSource value, int index, IDisposable self)
  316. {
  317. _parent = parent;
  318. _value = value;
  319. _valueIndex = index;
  320. _self = self;
  321. }
  322. private int _index;
  323. public void OnNext(TCollection value)
  324. {
  325. var res = default(TResult);
  326. try
  327. {
  328. res = _parent._parent._resultSelectorI(_value, _valueIndex, value, checked(_index++));
  329. }
  330. catch (Exception ex)
  331. {
  332. lock (_parent._gate)
  333. {
  334. _parent._observer.OnError(ex);
  335. _parent.Dispose();
  336. }
  337. return;
  338. }
  339. lock (_parent._gate)
  340. _parent._observer.OnNext(res);
  341. }
  342. public void OnError(Exception error)
  343. {
  344. lock (_parent._gate)
  345. {
  346. _parent._observer.OnError(error);
  347. _parent.Dispose();
  348. }
  349. }
  350. public void OnCompleted()
  351. {
  352. _parent._group.Remove(_self);
  353. if (_parent._isStopped && _parent._group.Count == 1)
  354. {
  355. //
  356. // Notice there can be a race between OnCompleted of the source and any
  357. // of the inner sequences, where both see _group.Count == 1, and one is
  358. // waiting for the lock. There won't be a double OnCompleted observation
  359. // though, because the call to Dispose silences the observer by swapping
  360. // in a NopObserver<T>.
  361. //
  362. lock (_parent._gate)
  363. {
  364. _parent._observer.OnCompleted();
  365. _parent.Dispose();
  366. }
  367. }
  368. }
  369. }
  370. }
  371. class ε : Sink<TResult>, IObserver<TSource>
  372. {
  373. private readonly SelectMany<TSource, TCollection, TResult> _parent;
  374. public ε(SelectMany<TSource, TCollection, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  375. : base(observer, cancel)
  376. {
  377. _parent = parent;
  378. }
  379. public void OnNext(TSource value)
  380. {
  381. var xs = default(IEnumerable<TCollection>);
  382. try
  383. {
  384. xs = _parent._collectionSelectorE(value);
  385. }
  386. catch (Exception exception)
  387. {
  388. base._observer.OnError(exception);
  389. base.Dispose();
  390. return;
  391. }
  392. var e = default(IEnumerator<TCollection>);
  393. try
  394. {
  395. e = xs.GetEnumerator();
  396. }
  397. catch (Exception exception)
  398. {
  399. base._observer.OnError(exception);
  400. base.Dispose();
  401. return;
  402. }
  403. try
  404. {
  405. var hasNext = true;
  406. while (hasNext)
  407. {
  408. hasNext = false;
  409. var current = default(TResult);
  410. try
  411. {
  412. hasNext = e.MoveNext();
  413. if (hasNext)
  414. current = _parent._resultSelector(value, e.Current);
  415. }
  416. catch (Exception exception)
  417. {
  418. base._observer.OnError(exception);
  419. base.Dispose();
  420. return;
  421. }
  422. if (hasNext)
  423. base._observer.OnNext(current);
  424. }
  425. }
  426. finally
  427. {
  428. if (e != null)
  429. e.Dispose();
  430. }
  431. }
  432. public void OnError(Exception error)
  433. {
  434. base._observer.OnError(error);
  435. base.Dispose();
  436. }
  437. public void OnCompleted()
  438. {
  439. base._observer.OnCompleted();
  440. base.Dispose();
  441. }
  442. }
  443. class ω : Sink<TResult>, IObserver<TSource>
  444. {
  445. private readonly SelectMany<TSource, TCollection, TResult> _parent;
  446. public ω(SelectMany<TSource, TCollection, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  447. : base(observer, cancel)
  448. {
  449. _parent = parent;
  450. }
  451. private int _index;
  452. public void OnNext(TSource value)
  453. {
  454. var index = checked(_index++);
  455. var xs = default(IEnumerable<TCollection>);
  456. try
  457. {
  458. xs = _parent._collectionSelectorEI(value, index);
  459. }
  460. catch (Exception exception)
  461. {
  462. base._observer.OnError(exception);
  463. base.Dispose();
  464. return;
  465. }
  466. var e = default(IEnumerator<TCollection>);
  467. try
  468. {
  469. e = xs.GetEnumerator();
  470. }
  471. catch (Exception exception)
  472. {
  473. base._observer.OnError(exception);
  474. base.Dispose();
  475. return;
  476. }
  477. try
  478. {
  479. var eIndex = 0;
  480. var hasNext = true;
  481. while (hasNext)
  482. {
  483. hasNext = false;
  484. var current = default(TResult);
  485. try
  486. {
  487. hasNext = e.MoveNext();
  488. if (hasNext)
  489. current = _parent._resultSelectorI(value, index, e.Current, checked(eIndex++));
  490. }
  491. catch (Exception exception)
  492. {
  493. base._observer.OnError(exception);
  494. base.Dispose();
  495. return;
  496. }
  497. if (hasNext)
  498. base._observer.OnNext(current);
  499. }
  500. }
  501. finally
  502. {
  503. if (e != null)
  504. e.Dispose();
  505. }
  506. }
  507. public void OnError(Exception error)
  508. {
  509. base._observer.OnError(error);
  510. base.Dispose();
  511. }
  512. public void OnCompleted()
  513. {
  514. base._observer.OnCompleted();
  515. base.Dispose();
  516. }
  517. }
  518. #if !NO_TPL
  519. #pragma warning disable 0420
  520. class τ : Sink<TResult>, IObserver<TSource>
  521. {
  522. private readonly SelectMany<TSource, TCollection, TResult> _parent;
  523. public τ(SelectMany<TSource, TCollection, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  524. : base(observer, cancel)
  525. {
  526. _parent = parent;
  527. }
  528. private object _gate;
  529. private CancellationDisposable _cancel;
  530. private volatile int _count;
  531. public IDisposable Run()
  532. {
  533. _gate = new object();
  534. _cancel = new CancellationDisposable();
  535. _count = 1;
  536. return new CompositeDisposable(_parent._source.SubscribeSafe(this), _cancel);
  537. }
  538. public void OnNext(TSource value)
  539. {
  540. var task = default(Task<TCollection>);
  541. try
  542. {
  543. Interlocked.Increment(ref _count);
  544. task = _parent._collectionSelectorT(value, _cancel.Token);
  545. }
  546. catch (Exception ex)
  547. {
  548. lock (_gate)
  549. {
  550. base._observer.OnError(ex);
  551. base.Dispose();
  552. }
  553. return;
  554. }
  555. if (task.IsCompleted)
  556. {
  557. OnCompletedTask(value, task);
  558. }
  559. else
  560. {
  561. AttachContinuation(value, task);
  562. }
  563. }
  564. private void AttachContinuation(TSource value, Task<TCollection> task)
  565. {
  566. //
  567. // Separate method to avoid closure in synchronous completion case.
  568. //
  569. task.ContinueWith(t => OnCompletedTask(value, t));
  570. }
  571. private void OnCompletedTask(TSource value, Task<TCollection> task)
  572. {
  573. switch (task.Status)
  574. {
  575. case TaskStatus.RanToCompletion:
  576. {
  577. var res = default(TResult);
  578. try
  579. {
  580. res = _parent._resultSelector(value, task.Result);
  581. }
  582. catch (Exception ex)
  583. {
  584. lock (_gate)
  585. {
  586. base._observer.OnError(ex);
  587. base.Dispose();
  588. }
  589. return;
  590. }
  591. lock (_gate)
  592. base._observer.OnNext(res);
  593. OnCompleted();
  594. }
  595. break;
  596. case TaskStatus.Faulted:
  597. {
  598. lock (_gate)
  599. {
  600. base._observer.OnError(task.Exception.InnerException);
  601. base.Dispose();
  602. }
  603. }
  604. break;
  605. case TaskStatus.Canceled:
  606. {
  607. if (!_cancel.IsDisposed)
  608. {
  609. lock (_gate)
  610. {
  611. base._observer.OnError(new TaskCanceledException(task));
  612. base.Dispose();
  613. }
  614. }
  615. }
  616. break;
  617. }
  618. }
  619. public void OnError(Exception error)
  620. {
  621. lock (_gate)
  622. {
  623. base._observer.OnError(error);
  624. base.Dispose();
  625. }
  626. }
  627. public void OnCompleted()
  628. {
  629. if (Interlocked.Decrement(ref _count) == 0)
  630. {
  631. lock (_gate)
  632. {
  633. base._observer.OnCompleted();
  634. base.Dispose();
  635. }
  636. }
  637. }
  638. }
  639. class σ : Sink<TResult>, IObserver<TSource>
  640. {
  641. private readonly SelectMany<TSource, TCollection, TResult> _parent;
  642. public σ(SelectMany<TSource, TCollection, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  643. : base(observer, cancel)
  644. {
  645. _parent = parent;
  646. }
  647. private object _gate;
  648. private CancellationDisposable _cancel;
  649. private volatile int _count;
  650. private int _index;
  651. public IDisposable Run()
  652. {
  653. _gate = new object();
  654. _cancel = new CancellationDisposable();
  655. _count = 1;
  656. return new CompositeDisposable(_parent._source.SubscribeSafe(this), _cancel);
  657. }
  658. public void OnNext(TSource value)
  659. {
  660. var index = checked(_index++);
  661. var task = default(Task<TCollection>);
  662. try
  663. {
  664. Interlocked.Increment(ref _count);
  665. task = _parent._collectionSelectorTI(value, index, _cancel.Token);
  666. }
  667. catch (Exception ex)
  668. {
  669. lock (_gate)
  670. {
  671. base._observer.OnError(ex);
  672. base.Dispose();
  673. }
  674. return;
  675. }
  676. if (task.IsCompleted)
  677. {
  678. OnCompletedTask(value, index, task);
  679. }
  680. else
  681. {
  682. AttachContinuation(value, index, task);
  683. }
  684. }
  685. private void AttachContinuation(TSource value, int index, Task<TCollection> task)
  686. {
  687. //
  688. // Separate method to avoid closure in synchronous completion case.
  689. //
  690. task.ContinueWith(t => OnCompletedTask(value, index, t));
  691. }
  692. private void OnCompletedTask(TSource value, int index, Task<TCollection> task)
  693. {
  694. switch (task.Status)
  695. {
  696. case TaskStatus.RanToCompletion:
  697. {
  698. var res = default(TResult);
  699. try
  700. {
  701. res = _parent._resultSelectorTI(value, index, task.Result);
  702. }
  703. catch (Exception ex)
  704. {
  705. lock (_gate)
  706. {
  707. base._observer.OnError(ex);
  708. base.Dispose();
  709. }
  710. return;
  711. }
  712. lock (_gate)
  713. base._observer.OnNext(res);
  714. OnCompleted();
  715. }
  716. break;
  717. case TaskStatus.Faulted:
  718. {
  719. lock (_gate)
  720. {
  721. base._observer.OnError(task.Exception.InnerException);
  722. base.Dispose();
  723. }
  724. }
  725. break;
  726. case TaskStatus.Canceled:
  727. {
  728. if (!_cancel.IsDisposed)
  729. {
  730. lock (_gate)
  731. {
  732. base._observer.OnError(new TaskCanceledException(task));
  733. base.Dispose();
  734. }
  735. }
  736. }
  737. break;
  738. }
  739. }
  740. public void OnError(Exception error)
  741. {
  742. lock (_gate)
  743. {
  744. base._observer.OnError(error);
  745. base.Dispose();
  746. }
  747. }
  748. public void OnCompleted()
  749. {
  750. if (Interlocked.Decrement(ref _count) == 0)
  751. {
  752. lock (_gate)
  753. {
  754. base._observer.OnCompleted();
  755. base.Dispose();
  756. }
  757. }
  758. }
  759. }
  760. #pragma warning restore 0420
  761. #endif
  762. }
  763. class SelectMany<TSource, TResult> : Producer<TResult>
  764. {
  765. private readonly IObservable<TSource> _source;
  766. private readonly Func<TSource, IObservable<TResult>> _selector;
  767. private readonly Func<TSource, int, IObservable<TResult>> _selectorI;
  768. private readonly Func<Exception, IObservable<TResult>> _selectorOnError;
  769. private readonly Func<IObservable<TResult>> _selectorOnCompleted;
  770. private readonly Func<TSource, IEnumerable<TResult>> _selectorE;
  771. private readonly Func<TSource, int, IEnumerable<TResult>> _selectorEI;
  772. public SelectMany(IObservable<TSource> source, Func<TSource, IObservable<TResult>> selector)
  773. {
  774. _source = source;
  775. _selector = selector;
  776. }
  777. public SelectMany(IObservable<TSource> source, Func<TSource, int, IObservable<TResult>> selector)
  778. {
  779. _source = source;
  780. _selectorI = selector;
  781. }
  782. public SelectMany(IObservable<TSource> source, Func<TSource, IObservable<TResult>> selector, Func<Exception, IObservable<TResult>> selectorOnError, Func<IObservable<TResult>> selectorOnCompleted)
  783. {
  784. _source = source;
  785. _selector = selector;
  786. _selectorOnError = selectorOnError;
  787. _selectorOnCompleted = selectorOnCompleted;
  788. }
  789. public SelectMany(IObservable<TSource> source, Func<TSource, int, IObservable<TResult>> selector, Func<Exception, IObservable<TResult>> selectorOnError, Func<IObservable<TResult>> selectorOnCompleted)
  790. {
  791. _source = source;
  792. _selectorI = selector;
  793. _selectorOnError = selectorOnError;
  794. _selectorOnCompleted = selectorOnCompleted;
  795. }
  796. public SelectMany(IObservable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)
  797. {
  798. _source = source;
  799. _selectorE = selector;
  800. }
  801. public SelectMany(IObservable<TSource> source, Func<TSource, int, IEnumerable<TResult>> selector)
  802. {
  803. _source = source;
  804. _selectorEI = selector;
  805. }
  806. #if !NO_TPL
  807. private readonly Func<TSource, CancellationToken, Task<TResult>> _selectorT;
  808. private readonly Func<TSource, int, CancellationToken, Task<TResult>> _selectorTI;
  809. public SelectMany(IObservable<TSource> source, Func<TSource, CancellationToken, Task<TResult>> selector)
  810. {
  811. _source = source;
  812. _selectorT = selector;
  813. }
  814. public SelectMany(IObservable<TSource> source, Func<TSource, int, CancellationToken, Task<TResult>> selector)
  815. {
  816. _source = source;
  817. _selectorTI = selector;
  818. }
  819. #endif
  820. protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
  821. {
  822. if (_selector != null)
  823. {
  824. var sink = new _(this, observer, cancel);
  825. setSink(sink);
  826. return sink.Run();
  827. }
  828. else if (_selectorI != null)
  829. {
  830. var sink = new π(this, observer, cancel);
  831. setSink(sink);
  832. return sink.Run();
  833. }
  834. #if !NO_TPL
  835. else if (_selectorT != null)
  836. {
  837. var sink = new τ(this, observer, cancel);
  838. setSink(sink);
  839. return sink.Run();
  840. }
  841. else if (_selectorTI != null)
  842. {
  843. var sink = new σ(this, observer, cancel);
  844. setSink(sink);
  845. return sink.Run();
  846. }
  847. #endif
  848. else if (_selectorE != null)
  849. {
  850. var sink = new ε(this, observer, cancel);
  851. setSink(sink);
  852. return _source.SubscribeSafe(sink);
  853. }
  854. else
  855. {
  856. var sink = new ω(this, observer, cancel);
  857. setSink(sink);
  858. return _source.SubscribeSafe(sink);
  859. }
  860. }
  861. class _ : Sink<TResult>, IObserver<TSource>
  862. {
  863. private readonly SelectMany<TSource, TResult> _parent;
  864. public _(SelectMany<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  865. : base(observer, cancel)
  866. {
  867. _parent = parent;
  868. }
  869. private object _gate;
  870. private bool _isStopped;
  871. private CompositeDisposable _group;
  872. private SingleAssignmentDisposable _sourceSubscription;
  873. public IDisposable Run()
  874. {
  875. _gate = new object();
  876. _isStopped = false;
  877. _group = new CompositeDisposable();
  878. _sourceSubscription = new SingleAssignmentDisposable();
  879. _group.Add(_sourceSubscription);
  880. _sourceSubscription.Disposable = _parent._source.SubscribeSafe(this);
  881. return _group;
  882. }
  883. public void OnNext(TSource value)
  884. {
  885. var inner = default(IObservable<TResult>);
  886. try
  887. {
  888. inner = _parent._selector(value);
  889. }
  890. catch (Exception ex)
  891. {
  892. lock (_gate)
  893. {
  894. base._observer.OnError(ex);
  895. base.Dispose();
  896. }
  897. return;
  898. }
  899. SubscribeInner(inner);
  900. }
  901. public void OnError(Exception error)
  902. {
  903. if (_parent._selectorOnError != null)
  904. {
  905. var inner = default(IObservable<TResult>);
  906. try
  907. {
  908. inner = _parent._selectorOnError(error);
  909. }
  910. catch (Exception ex)
  911. {
  912. lock (_gate)
  913. {
  914. base._observer.OnError(ex);
  915. base.Dispose();
  916. }
  917. return;
  918. }
  919. SubscribeInner(inner);
  920. Final();
  921. }
  922. else
  923. {
  924. lock (_gate)
  925. {
  926. base._observer.OnError(error);
  927. base.Dispose();
  928. }
  929. }
  930. }
  931. public void OnCompleted()
  932. {
  933. if (_parent._selectorOnCompleted != null)
  934. {
  935. var inner = default(IObservable<TResult>);
  936. try
  937. {
  938. inner = _parent._selectorOnCompleted();
  939. }
  940. catch (Exception ex)
  941. {
  942. lock (_gate)
  943. {
  944. base._observer.OnError(ex);
  945. base.Dispose();
  946. }
  947. return;
  948. }
  949. SubscribeInner(inner);
  950. }
  951. Final();
  952. }
  953. private void Final()
  954. {
  955. _isStopped = true;
  956. if (_group.Count == 1)
  957. {
  958. //
  959. // Notice there can be a race between OnCompleted of the source and any
  960. // of the inner sequences, where both see _group.Count == 1, and one is
  961. // waiting for the lock. There won't be a double OnCompleted observation
  962. // though, because the call to Dispose silences the observer by swapping
  963. // in a NopObserver<T>.
  964. //
  965. lock (_gate)
  966. {
  967. base._observer.OnCompleted();
  968. base.Dispose();
  969. }
  970. }
  971. else
  972. {
  973. _sourceSubscription.Dispose();
  974. }
  975. }
  976. private void SubscribeInner(IObservable<TResult> inner)
  977. {
  978. var innerSubscription = new SingleAssignmentDisposable();
  979. _group.Add(innerSubscription);
  980. innerSubscription.Disposable = inner.SubscribeSafe(new ι(this, innerSubscription));
  981. }
  982. class ι : IObserver<TResult>
  983. {
  984. private readonly _ _parent;
  985. private readonly IDisposable _self;
  986. public ι(_ parent, IDisposable self)
  987. {
  988. _parent = parent;
  989. _self = self;
  990. }
  991. public void OnNext(TResult value)
  992. {
  993. lock (_parent._gate)
  994. _parent._observer.OnNext(value);
  995. }
  996. public void OnError(Exception error)
  997. {
  998. lock (_parent._gate)
  999. {
  1000. _parent._observer.OnError(error);
  1001. _parent.Dispose();
  1002. }
  1003. }
  1004. public void OnCompleted()
  1005. {
  1006. _parent._group.Remove(_self);
  1007. if (_parent._isStopped && _parent._group.Count == 1)
  1008. {
  1009. //
  1010. // Notice there can be a race between OnCompleted of the source and any
  1011. // of the inner sequences, where both see _group.Count == 1, and one is
  1012. // waiting for the lock. There won't be a double OnCompleted observation
  1013. // though, because the call to Dispose silences the observer by swapping
  1014. // in a NopObserver<T>.
  1015. //
  1016. lock (_parent._gate)
  1017. {
  1018. _parent._observer.OnCompleted();
  1019. _parent.Dispose();
  1020. }
  1021. }
  1022. }
  1023. }
  1024. }
  1025. class π : Sink<TResult>, IObserver<TSource>
  1026. {
  1027. private readonly SelectMany<TSource, TResult> _parent;
  1028. public π(SelectMany<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  1029. : base(observer, cancel)
  1030. {
  1031. _parent = parent;
  1032. }
  1033. private object _gate;
  1034. private bool _isStopped;
  1035. private CompositeDisposable _group;
  1036. private SingleAssignmentDisposable _sourceSubscription;
  1037. private int _index;
  1038. public IDisposable Run()
  1039. {
  1040. _gate = new object();
  1041. _isStopped = false;
  1042. _group = new CompositeDisposable();
  1043. _sourceSubscription = new SingleAssignmentDisposable();
  1044. _group.Add(_sourceSubscription);
  1045. _sourceSubscription.Disposable = _parent._source.SubscribeSafe(this);
  1046. return _group;
  1047. }
  1048. public void OnNext(TSource value)
  1049. {
  1050. var inner = default(IObservable<TResult>);
  1051. try
  1052. {
  1053. inner = _parent._selectorI(value, checked(_index++));
  1054. }
  1055. catch (Exception ex)
  1056. {
  1057. lock (_gate)
  1058. {
  1059. base._observer.OnError(ex);
  1060. base.Dispose();
  1061. }
  1062. return;
  1063. }
  1064. SubscribeInner(inner);
  1065. }
  1066. public void OnError(Exception error)
  1067. {
  1068. if (_parent._selectorOnError != null)
  1069. {
  1070. var inner = default(IObservable<TResult>);
  1071. try
  1072. {
  1073. inner = _parent._selectorOnError(error);
  1074. }
  1075. catch (Exception ex)
  1076. {
  1077. lock (_gate)
  1078. {
  1079. base._observer.OnError(ex);
  1080. base.Dispose();
  1081. }
  1082. return;
  1083. }
  1084. SubscribeInner(inner);
  1085. Final();
  1086. }
  1087. else
  1088. {
  1089. lock (_gate)
  1090. {
  1091. base._observer.OnError(error);
  1092. base.Dispose();
  1093. }
  1094. }
  1095. }
  1096. public void OnCompleted()
  1097. {
  1098. if (_parent._selectorOnCompleted != null)
  1099. {
  1100. var inner = default(IObservable<TResult>);
  1101. try
  1102. {
  1103. inner = _parent._selectorOnCompleted();
  1104. }
  1105. catch (Exception ex)
  1106. {
  1107. lock (_gate)
  1108. {
  1109. base._observer.OnError(ex);
  1110. base.Dispose();
  1111. }
  1112. return;
  1113. }
  1114. SubscribeInner(inner);
  1115. }
  1116. Final();
  1117. }
  1118. private void Final()
  1119. {
  1120. _isStopped = true;
  1121. if (_group.Count == 1)
  1122. {
  1123. //
  1124. // Notice there can be a race between OnCompleted of the source and any
  1125. // of the inner sequences, where both see _group.Count == 1, and one is
  1126. // waiting for the lock. There won't be a double OnCompleted observation
  1127. // though, because the call to Dispose silences the observer by swapping
  1128. // in a NopObserver<T>.
  1129. //
  1130. lock (_gate)
  1131. {
  1132. base._observer.OnCompleted();
  1133. base.Dispose();
  1134. }
  1135. }
  1136. else
  1137. {
  1138. _sourceSubscription.Dispose();
  1139. }
  1140. }
  1141. private void SubscribeInner(IObservable<TResult> inner)
  1142. {
  1143. var innerSubscription = new SingleAssignmentDisposable();
  1144. _group.Add(innerSubscription);
  1145. innerSubscription.Disposable = inner.SubscribeSafe(new ι(this, innerSubscription));
  1146. }
  1147. class ι : IObserver<TResult>
  1148. {
  1149. private readonly π _parent;
  1150. private readonly IDisposable _self;
  1151. public ι(π parent, IDisposable self)
  1152. {
  1153. _parent = parent;
  1154. _self = self;
  1155. }
  1156. public void OnNext(TResult value)
  1157. {
  1158. lock (_parent._gate)
  1159. _parent._observer.OnNext(value);
  1160. }
  1161. public void OnError(Exception error)
  1162. {
  1163. lock (_parent._gate)
  1164. {
  1165. _parent._observer.OnError(error);
  1166. _parent.Dispose();
  1167. }
  1168. }
  1169. public void OnCompleted()
  1170. {
  1171. _parent._group.Remove(_self);
  1172. if (_parent._isStopped && _parent._group.Count == 1)
  1173. {
  1174. //
  1175. // Notice there can be a race between OnCompleted of the source and any
  1176. // of the inner sequences, where both see _group.Count == 1, and one is
  1177. // waiting for the lock. There won't be a double OnCompleted observation
  1178. // though, because the call to Dispose silences the observer by swapping
  1179. // in a NopObserver<T>.
  1180. //
  1181. lock (_parent._gate)
  1182. {
  1183. _parent._observer.OnCompleted();
  1184. _parent.Dispose();
  1185. }
  1186. }
  1187. }
  1188. }
  1189. }
  1190. class ε : Sink<TResult>, IObserver<TSource>
  1191. {
  1192. private readonly SelectMany<TSource, TResult> _parent;
  1193. public ε(SelectMany<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  1194. : base(observer, cancel)
  1195. {
  1196. _parent = parent;
  1197. }
  1198. public void OnNext(TSource value)
  1199. {
  1200. var xs = default(IEnumerable<TResult>);
  1201. try
  1202. {
  1203. xs = _parent._selectorE(value);
  1204. }
  1205. catch (Exception exception)
  1206. {
  1207. base._observer.OnError(exception);
  1208. base.Dispose();
  1209. return;
  1210. }
  1211. var e = default(IEnumerator<TResult>);
  1212. try
  1213. {
  1214. e = xs.GetEnumerator();
  1215. }
  1216. catch (Exception exception)
  1217. {
  1218. base._observer.OnError(exception);
  1219. base.Dispose();
  1220. return;
  1221. }
  1222. try
  1223. {
  1224. var hasNext = true;
  1225. while (hasNext)
  1226. {
  1227. hasNext = false;
  1228. var current = default(TResult);
  1229. try
  1230. {
  1231. hasNext = e.MoveNext();
  1232. if (hasNext)
  1233. current = e.Current;
  1234. }
  1235. catch (Exception exception)
  1236. {
  1237. base._observer.OnError(exception);
  1238. base.Dispose();
  1239. return;
  1240. }
  1241. if (hasNext)
  1242. base._observer.OnNext(current);
  1243. }
  1244. }
  1245. finally
  1246. {
  1247. if (e != null)
  1248. e.Dispose();
  1249. }
  1250. }
  1251. public void OnError(Exception error)
  1252. {
  1253. base._observer.OnError(error);
  1254. base.Dispose();
  1255. }
  1256. public void OnCompleted()
  1257. {
  1258. base._observer.OnCompleted();
  1259. base.Dispose();
  1260. }
  1261. }
  1262. class ω : Sink<TResult>, IObserver<TSource>
  1263. {
  1264. private readonly SelectMany<TSource, TResult> _parent;
  1265. public ω(SelectMany<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  1266. : base(observer, cancel)
  1267. {
  1268. _parent = parent;
  1269. }
  1270. private int _index;
  1271. public void OnNext(TSource value)
  1272. {
  1273. var xs = default(IEnumerable<TResult>);
  1274. try
  1275. {
  1276. xs = _parent._selectorEI(value, checked(_index++));
  1277. }
  1278. catch (Exception exception)
  1279. {
  1280. base._observer.OnError(exception);
  1281. base.Dispose();
  1282. return;
  1283. }
  1284. var e = default(IEnumerator<TResult>);
  1285. try
  1286. {
  1287. e = xs.GetEnumerator();
  1288. }
  1289. catch (Exception exception)
  1290. {
  1291. base._observer.OnError(exception);
  1292. base.Dispose();
  1293. return;
  1294. }
  1295. try
  1296. {
  1297. var hasNext = true;
  1298. while (hasNext)
  1299. {
  1300. hasNext = false;
  1301. var current = default(TResult);
  1302. try
  1303. {
  1304. hasNext = e.MoveNext();
  1305. if (hasNext)
  1306. current = e.Current;
  1307. }
  1308. catch (Exception exception)
  1309. {
  1310. base._observer.OnError(exception);
  1311. base.Dispose();
  1312. return;
  1313. }
  1314. if (hasNext)
  1315. base._observer.OnNext(current);
  1316. }
  1317. }
  1318. finally
  1319. {
  1320. if (e != null)
  1321. e.Dispose();
  1322. }
  1323. }
  1324. public void OnError(Exception error)
  1325. {
  1326. base._observer.OnError(error);
  1327. base.Dispose();
  1328. }
  1329. public void OnCompleted()
  1330. {
  1331. base._observer.OnCompleted();
  1332. base.Dispose();
  1333. }
  1334. }
  1335. #if !NO_TPL
  1336. #pragma warning disable 0420
  1337. class τ : Sink<TResult>, IObserver<TSource>
  1338. {
  1339. private readonly SelectMany<TSource, TResult> _parent;
  1340. public τ(SelectMany<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  1341. : base(observer, cancel)
  1342. {
  1343. _parent = parent;
  1344. }
  1345. private object _gate;
  1346. private CancellationDisposable _cancel;
  1347. private volatile int _count;
  1348. public IDisposable Run()
  1349. {
  1350. _gate = new object();
  1351. _cancel = new CancellationDisposable();
  1352. _count = 1;
  1353. return new CompositeDisposable(_parent._source.SubscribeSafe(this), _cancel);
  1354. }
  1355. public void OnNext(TSource value)
  1356. {
  1357. var task = default(Task<TResult>);
  1358. try
  1359. {
  1360. Interlocked.Increment(ref _count);
  1361. task = _parent._selectorT(value, _cancel.Token);
  1362. }
  1363. catch (Exception ex)
  1364. {
  1365. lock (_gate)
  1366. {
  1367. base._observer.OnError(ex);
  1368. base.Dispose();
  1369. }
  1370. return;
  1371. }
  1372. if (task.IsCompleted)
  1373. {
  1374. OnCompletedTask(task);
  1375. }
  1376. else
  1377. {
  1378. task.ContinueWith(OnCompletedTask);
  1379. }
  1380. }
  1381. private void OnCompletedTask(Task<TResult> task)
  1382. {
  1383. switch (task.Status)
  1384. {
  1385. case TaskStatus.RanToCompletion:
  1386. {
  1387. lock (_gate)
  1388. base._observer.OnNext(task.Result);
  1389. OnCompleted();
  1390. }
  1391. break;
  1392. case TaskStatus.Faulted:
  1393. {
  1394. lock (_gate)
  1395. {
  1396. base._observer.OnError(task.Exception.InnerException);
  1397. base.Dispose();
  1398. }
  1399. }
  1400. break;
  1401. case TaskStatus.Canceled:
  1402. {
  1403. if (!_cancel.IsDisposed)
  1404. {
  1405. lock (_gate)
  1406. {
  1407. base._observer.OnError(new TaskCanceledException(task));
  1408. base.Dispose();
  1409. }
  1410. }
  1411. }
  1412. break;
  1413. }
  1414. }
  1415. public void OnError(Exception error)
  1416. {
  1417. lock (_gate)
  1418. {
  1419. base._observer.OnError(error);
  1420. base.Dispose();
  1421. }
  1422. }
  1423. public void OnCompleted()
  1424. {
  1425. if (Interlocked.Decrement(ref _count) == 0)
  1426. {
  1427. lock (_gate)
  1428. {
  1429. base._observer.OnCompleted();
  1430. base.Dispose();
  1431. }
  1432. }
  1433. }
  1434. }
  1435. class σ : Sink<TResult>, IObserver<TSource>
  1436. {
  1437. private readonly SelectMany<TSource, TResult> _parent;
  1438. public σ(SelectMany<TSource, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  1439. : base(observer, cancel)
  1440. {
  1441. _parent = parent;
  1442. }
  1443. private object _gate;
  1444. private CancellationDisposable _cancel;
  1445. private volatile int _count;
  1446. private int _index;
  1447. public IDisposable Run()
  1448. {
  1449. _gate = new object();
  1450. _cancel = new CancellationDisposable();
  1451. _count = 1;
  1452. return new CompositeDisposable(_parent._source.SubscribeSafe(this), _cancel);
  1453. }
  1454. public void OnNext(TSource value)
  1455. {
  1456. var task = default(Task<TResult>);
  1457. try
  1458. {
  1459. Interlocked.Increment(ref _count);
  1460. task = _parent._selectorTI(value, checked(_index++), _cancel.Token);
  1461. }
  1462. catch (Exception ex)
  1463. {
  1464. lock (_gate)
  1465. {
  1466. base._observer.OnError(ex);
  1467. base.Dispose();
  1468. }
  1469. return;
  1470. }
  1471. if (task.IsCompleted)
  1472. {
  1473. OnCompletedTask(task);
  1474. }
  1475. else
  1476. {
  1477. task.ContinueWith(OnCompletedTask);
  1478. }
  1479. }
  1480. private void OnCompletedTask(Task<TResult> task)
  1481. {
  1482. switch (task.Status)
  1483. {
  1484. case TaskStatus.RanToCompletion:
  1485. {
  1486. lock (_gate)
  1487. base._observer.OnNext(task.Result);
  1488. OnCompleted();
  1489. }
  1490. break;
  1491. case TaskStatus.Faulted:
  1492. {
  1493. lock (_gate)
  1494. {
  1495. base._observer.OnError(task.Exception.InnerException);
  1496. base.Dispose();
  1497. }
  1498. }
  1499. break;
  1500. case TaskStatus.Canceled:
  1501. {
  1502. if (!_cancel.IsDisposed)
  1503. {
  1504. lock (_gate)
  1505. {
  1506. base._observer.OnError(new TaskCanceledException(task));
  1507. base.Dispose();
  1508. }
  1509. }
  1510. }
  1511. break;
  1512. }
  1513. }
  1514. public void OnError(Exception error)
  1515. {
  1516. lock (_gate)
  1517. {
  1518. base._observer.OnError(error);
  1519. base.Dispose();
  1520. }
  1521. }
  1522. public void OnCompleted()
  1523. {
  1524. if (Interlocked.Decrement(ref _count) == 0)
  1525. {
  1526. lock (_gate)
  1527. {
  1528. base._observer.OnCompleted();
  1529. base.Dispose();
  1530. }
  1531. }
  1532. }
  1533. }
  1534. #pragma warning restore 0420
  1535. #endif
  1536. }
  1537. }
  1538. #endif