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