1
0

DoWhile.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. using System.Collections.Generic;
  7. namespace System.Reactive.Linq.ObservableImpl
  8. {
  9. class DoWhile<TSource> : Producer<TSource>, IConcatenatable<TSource>
  10. {
  11. private readonly IObservable<TSource> _source;
  12. private readonly Func<bool> _condition;
  13. public DoWhile(IObservable<TSource> source, Func<bool> condition)
  14. {
  15. _condition = condition;
  16. _source = source;
  17. }
  18. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  19. {
  20. var sink = new _(observer, cancel);
  21. setSink(sink);
  22. return sink.Run(GetSources());
  23. }
  24. public IEnumerable<IObservable<TSource>> GetSources()
  25. {
  26. yield return _source;
  27. while (_condition())
  28. yield return _source;
  29. }
  30. class _ : ConcatSink<TSource>
  31. {
  32. public _(IObserver<TSource> observer, IDisposable cancel)
  33. : base(observer, cancel)
  34. {
  35. }
  36. public override void OnNext(TSource value)
  37. {
  38. base._observer.OnNext(value);
  39. }
  40. public override void OnError(Exception error)
  41. {
  42. base._observer.OnError(error);
  43. base.Dispose();
  44. }
  45. }
  46. }
  47. }
  48. #endif