Timer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. namespace UniRx.Operators
  3. {
  4. internal class TimerObservable : OperatorObservableBase<long>
  5. {
  6. readonly DateTimeOffset? dueTimeA;
  7. readonly TimeSpan? dueTimeB;
  8. readonly TimeSpan? period;
  9. readonly IScheduler scheduler;
  10. public TimerObservable(DateTimeOffset dueTime, TimeSpan? period, IScheduler scheduler)
  11. : base(scheduler == Scheduler.CurrentThread)
  12. {
  13. this.dueTimeA = dueTime;
  14. this.period = period;
  15. this.scheduler = scheduler;
  16. }
  17. public TimerObservable(TimeSpan dueTime, TimeSpan? period, IScheduler scheduler)
  18. : base(scheduler == Scheduler.CurrentThread)
  19. {
  20. this.dueTimeB = dueTime;
  21. this.period = period;
  22. this.scheduler = scheduler;
  23. }
  24. protected override IDisposable SubscribeCore(IObserver<long> observer, IDisposable cancel)
  25. {
  26. var timerObserver = new Timer(observer, cancel);
  27. var dueTime = (dueTimeA != null)
  28. ? dueTimeA.Value - scheduler.Now
  29. : dueTimeB.Value;
  30. // one-shot
  31. if (period == null)
  32. {
  33. return scheduler.Schedule(Scheduler.Normalize(dueTime), () =>
  34. {
  35. timerObserver.OnNext();
  36. timerObserver.OnCompleted();
  37. });
  38. }
  39. else
  40. {
  41. var periodicScheduler = scheduler as ISchedulerPeriodic;
  42. if (periodicScheduler != null)
  43. {
  44. if (dueTime == period.Value)
  45. {
  46. // same(Observable.Interval), run periodic
  47. return periodicScheduler.SchedulePeriodic(Scheduler.Normalize(dueTime), timerObserver.OnNext);
  48. }
  49. else
  50. {
  51. // Schedule Once + Scheudle Periodic
  52. var disposable = new SerialDisposable();
  53. disposable.Disposable = scheduler.Schedule(Scheduler.Normalize(dueTime), () =>
  54. {
  55. timerObserver.OnNext(); // run first
  56. var timeP = Scheduler.Normalize(period.Value);
  57. disposable.Disposable = periodicScheduler.SchedulePeriodic(timeP, timerObserver.OnNext); // run periodic
  58. });
  59. return disposable;
  60. }
  61. }
  62. else
  63. {
  64. var timeP = Scheduler.Normalize(period.Value);
  65. return scheduler.Schedule(Scheduler.Normalize(dueTime), self =>
  66. {
  67. timerObserver.OnNext();
  68. self(timeP);
  69. });
  70. }
  71. }
  72. }
  73. class Timer : OperatorObserverBase<long, long>
  74. {
  75. long index = 0;
  76. public Timer(IObserver<long> observer, IDisposable cancel)
  77. : base(observer, cancel)
  78. {
  79. }
  80. public void OnNext()
  81. {
  82. try
  83. {
  84. base.observer.OnNext(index++);
  85. }
  86. catch
  87. {
  88. Dispose();
  89. throw;
  90. }
  91. }
  92. public override void OnNext(long value)
  93. {
  94. // no use.
  95. }
  96. public override void OnError(Exception error)
  97. {
  98. try { observer.OnError(error); }
  99. finally { Dispose(); }
  100. }
  101. public override void OnCompleted()
  102. {
  103. try { observer.OnCompleted(); }
  104. finally { Dispose(); }
  105. }
  106. }
  107. }
  108. }