Scan.cs 4.6 KB

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