ToObservable.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if !NO_PERF
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reactive.Concurrency;
  8. using System.Reactive.Disposables;
  9. namespace System.Reactive.Linq.ObservableImpl
  10. {
  11. class ToObservable<TSource> : Producer<TSource>
  12. {
  13. private readonly IEnumerable<TSource> _source;
  14. private readonly IScheduler _scheduler;
  15. public ToObservable(IEnumerable<TSource> source, IScheduler scheduler)
  16. {
  17. _source = source;
  18. _scheduler = scheduler;
  19. }
  20. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  21. {
  22. var sink = new _(this, observer, cancel);
  23. setSink(sink);
  24. return sink.Run();
  25. }
  26. class _ : Sink<TSource>
  27. {
  28. private readonly ToObservable<TSource> _parent;
  29. public _(ToObservable<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  30. : base(observer, cancel)
  31. {
  32. _parent = parent;
  33. }
  34. public IDisposable Run()
  35. {
  36. var e = default(IEnumerator<TSource>);
  37. try
  38. {
  39. e = _parent._source.GetEnumerator();
  40. }
  41. catch (Exception exception)
  42. {
  43. base._observer.OnError(exception);
  44. base.Dispose();
  45. return Disposable.Empty;
  46. }
  47. var longRunning = _parent._scheduler.AsLongRunning();
  48. if (longRunning != null)
  49. {
  50. //
  51. // Long-running schedulers have the contract they should *never* prevent
  52. // the work from starting, such that the scheduled work has the chance
  53. // to observe the cancellation and perform proper clean-up. In this case,
  54. // we're sure Loop will be entered, allowing us to dispose the enumerator.
  55. //
  56. return longRunning.ScheduleLongRunning(e, Loop);
  57. }
  58. else
  59. {
  60. //
  61. // We never allow the scheduled work to be cancelled. Instead, the flag
  62. // is used to have LoopRec bail out and perform proper clean-up of the
  63. // enumerator.
  64. //
  65. var flag = new BooleanDisposable();
  66. _parent._scheduler.Schedule(new State(flag, e), LoopRec);
  67. return flag;
  68. }
  69. }
  70. class State
  71. {
  72. public readonly ICancelable flag;
  73. public readonly IEnumerator<TSource> enumerator;
  74. public State(ICancelable flag, IEnumerator<TSource> enumerator)
  75. {
  76. this.flag = flag;
  77. this.enumerator = enumerator;
  78. }
  79. }
  80. private void LoopRec(State state, Action<State> recurse)
  81. {
  82. var hasNext = false;
  83. var ex = default(Exception);
  84. var current = default(TSource);
  85. if (state.flag.IsDisposed)
  86. {
  87. state.enumerator.Dispose();
  88. return;
  89. }
  90. try
  91. {
  92. hasNext = state.enumerator.MoveNext();
  93. if (hasNext)
  94. current = state.enumerator.Current;
  95. }
  96. catch (Exception exception)
  97. {
  98. ex = exception;
  99. }
  100. if (ex != null)
  101. {
  102. state.enumerator.Dispose();
  103. base._observer.OnError(ex);
  104. base.Dispose();
  105. return;
  106. }
  107. if (!hasNext)
  108. {
  109. state.enumerator.Dispose();
  110. base._observer.OnCompleted();
  111. base.Dispose();
  112. return;
  113. }
  114. base._observer.OnNext(current);
  115. recurse(state);
  116. }
  117. private void Loop(IEnumerator<TSource> enumerator, ICancelable cancel)
  118. {
  119. while (!cancel.IsDisposed)
  120. {
  121. var hasNext = false;
  122. var ex = default(Exception);
  123. var current = default(TSource);
  124. try
  125. {
  126. hasNext = enumerator.MoveNext();
  127. if (hasNext)
  128. current = enumerator.Current;
  129. }
  130. catch (Exception exception)
  131. {
  132. ex = exception;
  133. }
  134. if (ex != null)
  135. {
  136. base._observer.OnError(ex);
  137. break;
  138. }
  139. if (!hasNext)
  140. {
  141. base._observer.OnCompleted();
  142. break;
  143. }
  144. base._observer.OnNext(current);
  145. }
  146. enumerator.Dispose();
  147. base.Dispose();
  148. }
  149. }
  150. }
  151. }
  152. #endif