| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Reactive.Concurrency;
- using System.Reactive.Disposables;
- namespace System.Reactive.Linq.ObservableImpl
- {
- internal sealed class ToObservableRecursive<TSource> : Producer<TSource, ToObservableRecursive<TSource>._>
- {
- private readonly IEnumerable<TSource> _source;
- private readonly IScheduler _scheduler;
- public ToObservableRecursive(IEnumerable<TSource> source, IScheduler scheduler)
- {
- _source = source;
- _scheduler = scheduler;
- }
- protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
- protected override void Run(_ sink) => sink.Run(_source, _scheduler);
- internal sealed class _ : IdentitySink<TSource>
- {
- private IEnumerator<TSource> _enumerator;
- private volatile bool _disposed;
- public _(IObserver<TSource> observer)
- : base(observer)
- {
- }
- public void Run(IEnumerable<TSource> source, IScheduler scheduler)
- {
- try
- {
- _enumerator = source.GetEnumerator();
- }
- catch (Exception exception)
- {
- ForwardOnError(exception);
- return;
- }
- //
- // We never allow the scheduled work to be cancelled. Instead, the _disposed flag
- // is used to have LoopRec bail out and perform proper clean-up of the
- // enumerator.
- //
- scheduler.Schedule(this, (innerScheduler, @this) => @this.LoopRec(innerScheduler));
- }
- protected override void Dispose(bool disposing)
- {
- base.Dispose(disposing);
- if (disposing)
- {
- _disposed = true;
- }
- }
- private IDisposable LoopRec(IScheduler scheduler)
- {
- var hasNext = false;
- var ex = default(Exception);
- var current = default(TSource);
- var enumerator = _enumerator;
- if (_disposed)
- {
- _enumerator.Dispose();
- _enumerator = null;
- return Disposable.Empty;
- }
- try
- {
- hasNext = enumerator.MoveNext();
- if (hasNext)
- {
- current = enumerator.Current;
- }
- }
- catch (Exception exception)
- {
- ex = exception;
- }
- if (ex != null)
- {
- enumerator.Dispose();
- _enumerator = null;
- ForwardOnError(ex);
- return Disposable.Empty;
- }
- if (!hasNext)
- {
- enumerator.Dispose();
- _enumerator = null;
- ForwardOnCompleted();
- return Disposable.Empty;
- }
- ForwardOnNext(current);
- //
- // We never allow the scheduled work to be cancelled. Instead, the _disposed flag
- // is used to have LoopRec bail out and perform proper clean-up of the
- // enumerator.
- //
- scheduler.Schedule(this, (innerScheduler, @this) => @this.LoopRec(innerScheduler));
- return Disposable.Empty;
- }
- }
- }
- internal sealed class ToObservableLongRunning<TSource> : Producer<TSource, ToObservableLongRunning<TSource>._>
- {
- private readonly IEnumerable<TSource> _source;
- private readonly ISchedulerLongRunning _scheduler;
- public ToObservableLongRunning(IEnumerable<TSource> source, ISchedulerLongRunning scheduler)
- {
- _source = source;
- _scheduler = scheduler;
- }
- protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
- protected override void Run(_ sink) => sink.Run(_source, _scheduler);
- internal sealed class _ : IdentitySink<TSource>
- {
- public _(IObserver<TSource> observer)
- : base(observer)
- {
- }
- public void Run(IEnumerable<TSource> source, ISchedulerLongRunning scheduler)
- {
- IEnumerator<TSource> e;
- try
- {
- e = source.GetEnumerator();
- }
- catch (Exception exception)
- {
- ForwardOnError(exception);
- return;
- }
- SetUpstream(scheduler.ScheduleLongRunning((@this: this, e), (tuple, cancelable) => [email protected](tuple.e, cancelable)));
- }
- private void Loop(IEnumerator<TSource> enumerator, ICancelable cancel)
- {
- while (!cancel.IsDisposed)
- {
- var hasNext = false;
- var ex = default(Exception);
- var current = default(TSource);
- try
- {
- hasNext = enumerator.MoveNext();
- if (hasNext)
- {
- current = enumerator.Current;
- }
- }
- catch (Exception exception)
- {
- ex = exception;
- }
- if (ex != null)
- {
- ForwardOnError(ex);
- break;
- }
- if (!hasNext)
- {
- ForwardOnCompleted();
- break;
- }
- ForwardOnNext(current);
- }
- enumerator.Dispose();
- Dispose();
- }
- }
- }
- }
|