Browse Source

4.x: Improve MaxBy cleanup/initialization (#679)

* 4.x: Improve MaxBy cleanup/initialization

* Clear the _lastKey as well

* Add cleanup to the keySelector crash
David Karnok 7 years ago
parent
commit
ad85f24aa0
1 changed files with 21 additions and 7 deletions
  1. 21 7
      Rx.NET/Source/src/System.Reactive/Linq/Observable/MaxBy.cs

+ 21 - 7
Rx.NET/Source/src/System.Reactive/Linq/Observable/MaxBy.cs

@@ -25,7 +25,8 @@ namespace System.Reactive.Linq.ObservableImpl
 
         internal sealed class _ : Sink<TSource, IList<TSource>> 
         {
-            private readonly MaxBy<TSource, TKey> _parent;
+            readonly Func<TSource, TKey> _keySelector;
+            readonly IComparer<TKey> _comparer;
             private bool _hasValue;
             private TKey _lastKey;
             private List<TSource> _list;
@@ -33,10 +34,9 @@ namespace System.Reactive.Linq.ObservableImpl
             public _(MaxBy<TSource, TKey> parent, IObserver<IList<TSource>> observer)
                 : base(observer)
             {
-                _parent = parent;
+                _keySelector = parent._keySelector;
+                _comparer = parent._comparer;
 
-                _hasValue = false;
-                _lastKey = default(TKey);
                 _list = new List<TSource>();
             }
 
@@ -45,10 +45,12 @@ namespace System.Reactive.Linq.ObservableImpl
                 var key = default(TKey);
                 try
                 {
-                    key = _parent._keySelector(value);
+                    key = _keySelector(value);
                 }
                 catch (Exception ex)
                 {
+                    _list = null;
+                    _lastKey = default;
                     ForwardOnError(ex);
                     return;
                 }
@@ -64,10 +66,12 @@ namespace System.Reactive.Linq.ObservableImpl
                 {
                     try
                     {
-                        comparison = _parent._comparer.Compare(key, _lastKey);
+                        comparison = _comparer.Compare(key, _lastKey);
                     }
                     catch (Exception ex)
                     {
+                        _list = null;
+                        _lastKey = default;
                         ForwardOnError(ex);
                         return;
                     }
@@ -85,9 +89,19 @@ namespace System.Reactive.Linq.ObservableImpl
                 }
             }
 
+            public override void OnError(Exception error)
+            {
+                _lastKey = default;
+                _list = null;
+                base.OnError(error);
+            }
+
             public override void OnCompleted()
             {
-                ForwardOnNext(_list);
+                var list = _list;
+                _list = null;
+                _lastKey = default;
+                ForwardOnNext(list);
                 ForwardOnCompleted();
             }
         }