All.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 All<TSource> : Producer<bool>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. private readonly Func<TSource, bool> _predicate;
  12. public All(IObservable<TSource> source, Func<TSource, bool> predicate)
  13. {
  14. _source = source;
  15. _predicate = predicate;
  16. }
  17. protected override IDisposable Run(IObserver<bool> observer, IDisposable cancel, Action<IDisposable> setSink)
  18. {
  19. var sink = new _(this, observer, cancel);
  20. setSink(sink);
  21. return _source.SubscribeSafe(sink);
  22. }
  23. class _ : Sink<bool>, IObserver<TSource>
  24. {
  25. private readonly All<TSource> _parent;
  26. public _(All<TSource> parent, IObserver<bool> observer, IDisposable cancel)
  27. : base(observer, cancel)
  28. {
  29. _parent = parent;
  30. }
  31. public void OnNext(TSource value)
  32. {
  33. var res = false;
  34. try
  35. {
  36. res = _parent._predicate(value);
  37. }
  38. catch (Exception ex)
  39. {
  40. base._observer.OnError(ex);
  41. base.Dispose();
  42. return;
  43. }
  44. if (!res)
  45. {
  46. base._observer.OnNext(false);
  47. base._observer.OnCompleted();
  48. base.Dispose();
  49. }
  50. }
  51. public void OnError(Exception error)
  52. {
  53. base._observer.OnError(error);
  54. base.Dispose();
  55. }
  56. public void OnCompleted()
  57. {
  58. base._observer.OnNext(true);
  59. base._observer.OnCompleted();
  60. base.Dispose();
  61. }
  62. }
  63. }
  64. }
  65. #endif