While.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 While<TSource> : Producer<TSource, While<TSource>._>, IConcatenatable<TSource>
  8. {
  9. private readonly Func<bool> _condition;
  10. private readonly IObservable<TSource> _source;
  11. public While(Func<bool> condition, IObservable<TSource> source)
  12. {
  13. _condition = condition;
  14. _source = source;
  15. }
  16. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
  17. protected override void Run(_ sink) => sink.Run(GetSources());
  18. public IEnumerable<IObservable<TSource>> GetSources()
  19. {
  20. while (_condition())
  21. {
  22. yield return _source;
  23. }
  24. }
  25. internal sealed class _ : ConcatSink<TSource>
  26. {
  27. public _(IObserver<TSource> observer)
  28. : base(observer)
  29. {
  30. }
  31. }
  32. }
  33. }