1
0

DoWhile.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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.Observαble
  6. {
  7. class DoWhile<TSource> : Producer<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 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. yield return _source;
  25. while (_condition())
  26. yield return _source;
  27. }
  28. class _ : ConcatSink<TSource>
  29. {
  30. public _(IObserver<TSource> observer, IDisposable cancel)
  31. : base(observer, cancel)
  32. {
  33. }
  34. public override void OnNext(TSource value)
  35. {
  36. base._observer.OnNext(value);
  37. }
  38. public override void OnError(Exception error)
  39. {
  40. base._observer.OnError(error);
  41. base.Dispose();
  42. }
  43. }
  44. }
  45. }
  46. #endif