Explorar el Código

4.x: Make plain Empty<T>() a singleton. (#544)

David Karnok hace 7 años
padre
commit
6c456b743e

+ 18 - 5
Rx.NET/Source/src/System.Reactive/Linq/Observable/Empty.cs

@@ -3,6 +3,7 @@
 // See the LICENSE file in the project root for more information. 
 
 using System.Reactive.Concurrency;
+using System.Reactive.Disposables;
 
 namespace System.Reactive.Linq.ObservableImpl
 {
@@ -28,13 +29,25 @@ namespace System.Reactive.Linq.ObservableImpl
 
             public IDisposable Run(IScheduler scheduler)
             {
-                return scheduler.Schedule(Invoke);
+                return scheduler.Schedule(this, (s, target) => 
+                {
+                    target.OnCompleted();
+                    return Disposable.Empty;
+                });
             }
+        }
+    }
 
-            private void Invoke()
-            {
-                ForwardOnCompleted();
-            }
+    internal sealed class EmptyDirect<TResult> : BasicProducer<TResult>
+    {
+        internal static readonly IObservable<TResult> Instance = new EmptyDirect<TResult>();
+
+        private EmptyDirect() { }
+
+        protected override IDisposable Run(IObserver<TResult> observer)
+        {
+            observer.OnCompleted();
+            return Disposable.Empty;
         }
     }
 }

+ 1 - 1
Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Creation.cs

@@ -134,7 +134,7 @@ namespace System.Reactive.Linq
 
         public virtual IObservable<TResult> Empty<TResult>()
         {
-            return new Empty<TResult>(SchedulerDefaults.ConstantTimeOperations);
+            return EmptyDirect<TResult>.Instance;
         }
 
         public virtual IObservable<TResult> Empty<TResult>(IScheduler scheduler)