DoWhile.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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.Collections.Generic;
  5. namespace System.Reactive.Linq.ObservableImpl
  6. {
  7. internal sealed class DoWhile<TSource> : Producer<TSource, DoWhile<TSource>._>, IConcatenatable<TSource>
  8. {
  9. private readonly IObservable<TSource> _source;
  10. private readonly Func<bool> _condition;
  11. public DoWhile(IObservable<TSource> source, Func<bool> condition)
  12. {
  13. _condition = condition;
  14. _source = source;
  15. }
  16. protected override _ CreateSink(IObserver<TSource> observer, IDisposable cancel) => new _(observer, cancel);
  17. protected override IDisposable Run(_ sink) => sink.Run(GetSources());
  18. public IEnumerable<IObservable<TSource>> GetSources()
  19. {
  20. yield return _source;
  21. while (_condition())
  22. yield return _source;
  23. }
  24. internal sealed class _ : ConcatSink<TSource>
  25. {
  26. public _(IObserver<TSource> observer, IDisposable cancel)
  27. : base(observer, cancel)
  28. {
  29. }
  30. }
  31. }
  32. }