Range.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 Range : Producer<int>
  11. {
  12. private readonly int _start;
  13. private readonly int _count;
  14. private readonly IScheduler _scheduler;
  15. public Range(int start, int count, IScheduler scheduler)
  16. {
  17. _start = start;
  18. _count = count;
  19. _scheduler = scheduler;
  20. }
  21. protected override IDisposable Run(IObserver<int> 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<int>
  28. {
  29. private readonly Range _parent;
  30. public _(Range parent, IObserver<int> 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 longRunning.ScheduleLongRunning(0, Loop);
  41. }
  42. else
  43. {
  44. return _parent._scheduler.Schedule(0, LoopRec);
  45. }
  46. }
  47. private void Loop(int i, ICancelable cancel)
  48. {
  49. while (!cancel.IsDisposed && i < _parent._count)
  50. {
  51. base._observer.OnNext(_parent._start + i);
  52. i++;
  53. }
  54. if (!cancel.IsDisposed)
  55. base._observer.OnCompleted();
  56. base.Dispose();
  57. }
  58. private void LoopRec(int i, Action<int> recurse)
  59. {
  60. if (i < _parent._count)
  61. {
  62. base._observer.OnNext(_parent._start + i);
  63. recurse(i + 1);
  64. }
  65. else
  66. {
  67. base._observer.OnCompleted();
  68. base.Dispose();
  69. }
  70. }
  71. }
  72. }
  73. }
  74. #endif