123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System;
- namespace UniRx.Operators
- {
- internal class TakeUntilObservable<T, TOther> : OperatorObservableBase<T>
- {
- readonly IObservable<T> source;
- readonly IObservable<TOther> other;
- public TakeUntilObservable(IObservable<T> source, IObservable<TOther> other)
- : base(source.IsRequiredSubscribeOnCurrentThread() || other.IsRequiredSubscribeOnCurrentThread())
- {
- this.source = source;
- this.other = other;
- }
- protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
- {
- return new TakeUntil(this, observer, cancel).Run();
- }
- class TakeUntil : OperatorObserverBase<T, T>
- {
- readonly TakeUntilObservable<T, TOther> parent;
- object gate = new object();
- public TakeUntil(TakeUntilObservable<T, TOther> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
- {
- this.parent = parent;
- }
- public IDisposable Run()
- {
- var otherSubscription = new SingleAssignmentDisposable();
- var otherObserver = new TakeUntilOther(this, otherSubscription);
- otherSubscription.Disposable = parent.other.Subscribe(otherObserver);
- var sourceSubscription = parent.source.Subscribe(this);
- return StableCompositeDisposable.Create(otherSubscription, sourceSubscription);
- }
- public override void OnNext(T value)
- {
- lock (gate)
- {
- observer.OnNext(value);
- }
- }
- public override void OnError(Exception error)
- {
- lock (gate)
- {
- try { observer.OnError(error); } finally { Dispose(); }
- }
- }
- public override void OnCompleted()
- {
- lock (gate)
- {
- try { observer.OnCompleted(); } finally { Dispose(); }
- }
- }
- class TakeUntilOther : IObserver<TOther>
- {
- readonly TakeUntil sourceObserver;
- readonly IDisposable subscription;
- public TakeUntilOther(TakeUntil sourceObserver, IDisposable subscription)
- {
- this.sourceObserver = sourceObserver;
- this.subscription = subscription;
- }
- public void OnNext(TOther value)
- {
- lock (sourceObserver.gate)
- {
- try
- {
- sourceObserver.observer.OnCompleted();
- }
- finally
- {
- sourceObserver.Dispose();
- subscription.Dispose();
- }
- }
- }
- public void OnError(Exception error)
- {
- lock (sourceObserver.gate)
- {
- try
- {
- sourceObserver.observer.OnError(error);
- }
- finally
- {
- sourceObserver.Dispose();
- subscription.Dispose();
- }
- }
- }
- public void OnCompleted()
- {
- lock (sourceObserver.gate)
- {
- try
- {
- sourceObserver.observer.OnCompleted();
- }
- finally
- {
- sourceObserver.Dispose();
- subscription.Dispose();
- }
- }
- }
- }
- }
- }
- }
|