ToObservable.cs 5.5 KB

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