Repeat.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.Reactive.Concurrency;
  7. using System.Reactive.Disposables;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. class Repeat<TResult> : Producer<TResult>
  11. {
  12. private readonly TResult _value;
  13. private readonly int? _repeatCount;
  14. private readonly IScheduler _scheduler;
  15. public Repeat(TResult value, int? repeatCount, IScheduler scheduler)
  16. {
  17. _value = value;
  18. _repeatCount = repeatCount;
  19. _scheduler = scheduler;
  20. }
  21. protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
  22. {
  23. var sink = new _(this, observer, cancel);
  24. setSink(sink);
  25. return sink.Run();
  26. }
  27. class _ : Sink<TResult>
  28. {
  29. private readonly Repeat<TResult> _parent;
  30. public _(Repeat<TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  31. : base(observer, cancel)
  32. {
  33. _parent = parent;
  34. }
  35. public IDisposable Run()
  36. {
  37. var longRunning = _parent._scheduler.AsLongRunning();
  38. if (longRunning != null)
  39. {
  40. return Run(longRunning);
  41. }
  42. else
  43. {
  44. return Run(_parent._scheduler);
  45. }
  46. }
  47. private IDisposable Run(IScheduler scheduler)
  48. {
  49. if (_parent._repeatCount == null)
  50. {
  51. return scheduler.Schedule(LoopRecInf);
  52. }
  53. else
  54. {
  55. return scheduler.Schedule(_parent._repeatCount.Value, LoopRec);
  56. }
  57. }
  58. private void LoopRecInf(Action recurse)
  59. {
  60. base._observer.OnNext(_parent._value);
  61. recurse();
  62. }
  63. private void LoopRec(int n, Action<int> recurse)
  64. {
  65. if (n > 0)
  66. {
  67. base._observer.OnNext(_parent._value);
  68. n--;
  69. }
  70. if (n == 0)
  71. {
  72. base._observer.OnCompleted();
  73. base.Dispose();
  74. return;
  75. }
  76. recurse(n);
  77. }
  78. private IDisposable Run(ISchedulerLongRunning scheduler)
  79. {
  80. if (_parent._repeatCount == null)
  81. {
  82. return scheduler.ScheduleLongRunning(LoopInf);
  83. }
  84. else
  85. {
  86. return scheduler.ScheduleLongRunning(_parent._repeatCount.Value, Loop);
  87. }
  88. }
  89. private void LoopInf(ICancelable cancel)
  90. {
  91. var value = _parent._value;
  92. while (!cancel.IsDisposed)
  93. base._observer.OnNext(value);
  94. base.Dispose();
  95. }
  96. private void Loop(int n, ICancelable cancel)
  97. {
  98. var value = _parent._value;
  99. while (n > 0 && !cancel.IsDisposed)
  100. {
  101. base._observer.OnNext(value);
  102. n--;
  103. }
  104. if (!cancel.IsDisposed)
  105. base._observer.OnCompleted();
  106. base.Dispose();
  107. }
  108. }
  109. }
  110. }
  111. #endif