While.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_PERF
  3. using System;
  4. using System.Collections.Generic;
  5. namespace System.Reactive.Linq.ObservableImpl
  6. {
  7. class While<TSource> : Producer<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 IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  17. {
  18. var sink = new _(observer, cancel);
  19. setSink(sink);
  20. return sink.Run(GetSources());
  21. }
  22. public IEnumerable<IObservable<TSource>> GetSources()
  23. {
  24. while (_condition())
  25. yield return _source;
  26. }
  27. class _ : ConcatSink<TSource>
  28. {
  29. public _(IObserver<TSource> observer, IDisposable cancel)
  30. : base(observer, cancel)
  31. {
  32. }
  33. public override void OnNext(TSource value)
  34. {
  35. base._observer.OnNext(value);
  36. }
  37. public override void OnError(Exception error)
  38. {
  39. base._observer.OnError(error);
  40. base.Dispose();
  41. }
  42. }
  43. }
  44. }
  45. #endif