ToObservable.cs 5.4 KB

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