123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- using System;
- using System.Collections.Generic;
- namespace UniRx.Operators
- {
- internal class DistinctUntilChangedObservable<T> : OperatorObservableBase<T>
- {
- readonly IObservable<T> source;
- readonly IEqualityComparer<T> comparer;
- public DistinctUntilChangedObservable(IObservable<T> source, IEqualityComparer<T> comparer)
- : base(source.IsRequiredSubscribeOnCurrentThread())
- {
- this.source = source;
- this.comparer = comparer;
- }
- protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
- {
- return source.Subscribe(new DistinctUntilChanged(this, observer, cancel));
- }
- class DistinctUntilChanged : OperatorObserverBase<T, T>
- {
- readonly DistinctUntilChangedObservable<T> parent;
- bool isFirst = true;
- T prevKey = default(T);
- public DistinctUntilChanged(DistinctUntilChangedObservable<T> parent, IObserver<T> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- this.parent = parent;
- }
- public override void OnNext(T value)
- {
- T currentKey;
- try
- {
- currentKey = value;
- }
- catch (Exception exception)
- {
- try { observer.OnError(exception); } finally { Dispose(); }
- return;
- }
- var sameKey = false;
- if (isFirst)
- {
- isFirst = false;
- }
- else
- {
- try
- {
- sameKey = parent.comparer.Equals(currentKey, prevKey);
- }
- catch (Exception ex)
- {
- try { observer.OnError(ex); } finally { Dispose(); }
- return;
- }
- }
- if (!sameKey)
- {
- prevKey = currentKey;
- observer.OnNext(value);
- }
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); } finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); } finally { Dispose(); }
- }
- }
- }
- internal class DistinctUntilChangedObservable<T, TKey> : OperatorObservableBase<T>
- {
- readonly IObservable<T> source;
- readonly IEqualityComparer<TKey> comparer;
- readonly Func<T, TKey> keySelector;
- public DistinctUntilChangedObservable(IObservable<T> source, Func<T, TKey> keySelector, IEqualityComparer<TKey> comparer)
- : base(source.IsRequiredSubscribeOnCurrentThread())
- {
- this.source = source;
- this.comparer = comparer;
- this.keySelector = keySelector;
- }
- protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
- {
- return source.Subscribe(new DistinctUntilChanged(this, observer, cancel));
- }
- class DistinctUntilChanged : OperatorObserverBase<T, T>
- {
- readonly DistinctUntilChangedObservable<T, TKey> parent;
- bool isFirst = true;
- TKey prevKey = default(TKey);
- public DistinctUntilChanged(DistinctUntilChangedObservable<T, TKey> parent, IObserver<T> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- this.parent = parent;
- }
- public override void OnNext(T value)
- {
- TKey currentKey;
- try
- {
- currentKey = parent.keySelector(value);
- }
- catch (Exception exception)
- {
- try { observer.OnError(exception); } finally { Dispose(); }
- return;
- }
- var sameKey = false;
- if (isFirst)
- {
- isFirst = false;
- }
- else
- {
- try
- {
- sameKey = parent.comparer.Equals(currentKey, prevKey);
- }
- catch (Exception ex)
- {
- try { observer.OnError(ex); } finally { Dispose(); }
- return;
- }
- }
- if (!sameKey)
- {
- prevKey = currentKey;
- observer.OnNext(value);
- }
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); } finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); } finally { Dispose(); }
- }
- }
- }
- }
|