ToObservable.cs 5.4 KB

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