DistinctUntilChanged.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Reactive.Linq.ObservableImpl
  6. {
  7. internal sealed class DistinctUntilChanged<TSource, TKey> : Producer<TSource>
  8. {
  9. private readonly IObservable<TSource> _source;
  10. private readonly Func<TSource, TKey> _keySelector;
  11. private readonly IEqualityComparer<TKey> _comparer;
  12. public DistinctUntilChanged(IObservable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  13. {
  14. _source = source;
  15. _keySelector = keySelector;
  16. _comparer = comparer;
  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 _source.SubscribeSafe(sink);
  23. }
  24. private sealed class _ : Sink<TSource>, IObserver<TSource>
  25. {
  26. // CONSIDER: This sink has a parent reference that can be considered for removal.
  27. private readonly DistinctUntilChanged<TSource, TKey> _parent;
  28. private TKey _currentKey;
  29. private bool _hasCurrentKey;
  30. public _(DistinctUntilChanged<TSource, TKey> parent, IObserver<TSource> observer, IDisposable cancel)
  31. : base(observer, cancel)
  32. {
  33. _parent = parent;
  34. _currentKey = default(TKey);
  35. _hasCurrentKey = false;
  36. }
  37. public void OnNext(TSource value)
  38. {
  39. var key = default(TKey);
  40. try
  41. {
  42. key = _parent._keySelector(value);
  43. }
  44. catch (Exception exception)
  45. {
  46. base._observer.OnError(exception);
  47. base.Dispose();
  48. return;
  49. }
  50. var comparerEquals = false;
  51. if (_hasCurrentKey)
  52. {
  53. try
  54. {
  55. comparerEquals = _parent._comparer.Equals(_currentKey, key);
  56. }
  57. catch (Exception exception)
  58. {
  59. base._observer.OnError(exception);
  60. base.Dispose();
  61. return;
  62. }
  63. }
  64. if (!_hasCurrentKey || !comparerEquals)
  65. {
  66. _hasCurrentKey = true;
  67. _currentKey = key;
  68. base._observer.OnNext(value);
  69. }
  70. }
  71. public void OnError(Exception error)
  72. {
  73. base._observer.OnError(error);
  74. base.Dispose();
  75. }
  76. public void OnCompleted()
  77. {
  78. base._observer.OnCompleted();
  79. base.Dispose();
  80. }
  81. }
  82. }
  83. }