IsEmpty.cs 1.5 KB

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