IsEmpty.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #if !NO_PERF
  5. using System;
  6. namespace System.Reactive.Linq.ObservableImpl
  7. {
  8. class IsEmpty<TSource> : Producer<bool>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. public IsEmpty(IObservable<TSource> source)
  12. {
  13. _source = source;
  14. }
  15. protected override IDisposable Run(IObserver<bool> observer, IDisposable cancel, Action<IDisposable> setSink)
  16. {
  17. var sink = new _(observer, cancel);
  18. setSink(sink);
  19. return _source.SubscribeSafe(sink);
  20. }
  21. class _ : Sink<bool>, IObserver<TSource>
  22. {
  23. public _(IObserver<bool> observer, IDisposable cancel)
  24. : base(observer, cancel)
  25. {
  26. }
  27. public void OnNext(TSource value)
  28. {
  29. base._observer.OnNext(false);
  30. base._observer.OnCompleted();
  31. base.Dispose();
  32. }
  33. public void OnError(Exception error)
  34. {
  35. base._observer.OnError(error);
  36. base.Dispose();
  37. }
  38. public void OnCompleted()
  39. {
  40. base._observer.OnNext(true);
  41. base._observer.OnCompleted();
  42. base.Dispose();
  43. }
  44. }
  45. }
  46. }
  47. #endif