123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- namespace UniRx.Operators
- {
- internal class TakeWhileObservable<T> : OperatorObservableBase<T>
- {
- readonly IObservable<T> source;
- readonly Func<T, bool> predicate;
- readonly Func<T, int, bool> predicateWithIndex;
- public TakeWhileObservable(IObservable<T> source, Func<T, bool> predicate)
- : base(source.IsRequiredSubscribeOnCurrentThread())
- {
- this.source = source;
- this.predicate = predicate;
- }
- public TakeWhileObservable(IObservable<T> source, Func<T, int, bool> predicateWithIndex)
- : base(source.IsRequiredSubscribeOnCurrentThread())
- {
- this.source = source;
- this.predicateWithIndex = predicateWithIndex;
- }
- protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
- {
- if (predicate != null)
- {
- return new TakeWhile(this, observer, cancel).Run();
- }
- else
- {
- return new TakeWhile_(this, observer, cancel).Run();
- }
- }
- class TakeWhile : OperatorObserverBase<T, T>
- {
- readonly TakeWhileObservable<T> parent;
- public TakeWhile(TakeWhileObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
- {
- this.parent = parent;
- }
- public IDisposable Run()
- {
- return parent.source.Subscribe(this);
- }
- public override void OnNext(T value)
- {
- bool isPassed;
- try
- {
- isPassed = parent.predicate(value);
- }
- catch (Exception ex)
- {
- try { observer.OnError(ex); } finally { Dispose(); }
- return;
- }
- if (isPassed)
- {
- observer.OnNext(value);
- }
- else
- {
- try { observer.OnCompleted(); } finally { Dispose(); }
- }
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); } finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); } finally { Dispose(); }
- }
- }
- class TakeWhile_ : OperatorObserverBase<T, T>
- {
- readonly TakeWhileObservable<T> parent;
- int index = 0;
- public TakeWhile_(TakeWhileObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
- {
- this.parent = parent;
- }
- public IDisposable Run()
- {
- return parent.source.Subscribe(this);
- }
- public override void OnNext(T value)
- {
- bool isPassed;
- try
- {
- isPassed = parent.predicateWithIndex(value, index++);
- }
- catch (Exception ex)
- {
- try { observer.OnError(ex); } finally { Dispose(); }
- return;
- }
- if (isPassed)
- {
- observer.OnNext(value);
- }
- else
- {
- try { observer.OnCompleted(); } finally { Dispose(); }
- }
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); } finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); } finally { Dispose(); }
- }
- }
- }
- }
|