Where.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 Where<TSource> : Producer<TSource>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. private readonly Func<TSource, bool> _predicate;
  12. private readonly Func<TSource, int, bool> _predicateI;
  13. public Where(IObservable<TSource> source, Func<TSource, bool> predicate)
  14. {
  15. _source = source;
  16. _predicate = predicate;
  17. }
  18. public Where(IObservable<TSource> source, Func<TSource, int, bool> predicate)
  19. {
  20. _source = source;
  21. _predicateI = predicate;
  22. }
  23. public IObservable<TSource> Omega(Func<TSource, bool> predicate)
  24. {
  25. if (_predicate != null)
  26. return new Where<TSource>(_source, x => _predicate(x) && predicate(x));
  27. else
  28. return new Where<TSource>(this, predicate);
  29. }
  30. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  31. {
  32. if (_predicate != null)
  33. {
  34. var sink = new _(this, observer, cancel);
  35. setSink(sink);
  36. return _source.SubscribeSafe(sink);
  37. }
  38. else
  39. {
  40. var sink = new WhereImpl(this, observer, cancel);
  41. setSink(sink);
  42. return _source.SubscribeSafe(sink);
  43. }
  44. }
  45. class _ : Sink<TSource>, IObserver<TSource>
  46. {
  47. private readonly Where<TSource> _parent;
  48. public _(Where<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  49. : base(observer, cancel)
  50. {
  51. _parent = parent;
  52. }
  53. public void OnNext(TSource value)
  54. {
  55. var shouldRun = default(bool);
  56. try
  57. {
  58. shouldRun = _parent._predicate(value);
  59. }
  60. catch (Exception exception)
  61. {
  62. base._observer.OnError(exception);
  63. base.Dispose();
  64. return;
  65. }
  66. if (shouldRun)
  67. base._observer.OnNext(value);
  68. }
  69. public void OnError(Exception error)
  70. {
  71. base._observer.OnError(error);
  72. base.Dispose();
  73. }
  74. public void OnCompleted()
  75. {
  76. base._observer.OnCompleted();
  77. base.Dispose();
  78. }
  79. }
  80. class WhereImpl : Sink<TSource>, IObserver<TSource>
  81. {
  82. private readonly Where<TSource> _parent;
  83. private int _index;
  84. public WhereImpl(Where<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  85. : base(observer, cancel)
  86. {
  87. _parent = parent;
  88. _index = 0;
  89. }
  90. public void OnNext(TSource value)
  91. {
  92. var shouldRun = default(bool);
  93. try
  94. {
  95. shouldRun = _parent._predicateI(value, checked(_index++));
  96. }
  97. catch (Exception exception)
  98. {
  99. base._observer.OnError(exception);
  100. base.Dispose();
  101. return;
  102. }
  103. if (shouldRun)
  104. base._observer.OnNext(value);
  105. }
  106. public void OnError(Exception error)
  107. {
  108. base._observer.OnError(error);
  109. base.Dispose();
  110. }
  111. public void OnCompleted()
  112. {
  113. base._observer.OnCompleted();
  114. base.Dispose();
  115. }
  116. }
  117. }
  118. }
  119. #endif