Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions Rx.NET/Source/src/System.Reactive/Linq/Observable/Empty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -28,13 +29,25 @@ public _(IObserver<TResult> observer, IDisposable cancel)

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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public virtual IObservable<TValue> Defer<TValue>(Func<CancellationToken, Task<IO

public virtual IObservable<TResult> Empty<TResult>()
{
return new Empty<TResult>(SchedulerDefaults.ConstantTimeOperations);
return EmptyDirect<TResult>.Instance;
}

public virtual IObservable<TResult> Empty<TResult>(IScheduler scheduler)
Expand Down