1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- namespace UniRx.Operators
- {
- internal class TimeIntervalObservable<T> : OperatorObservableBase<UniRx.TimeInterval<T>>
- {
- readonly IObservable<T> source;
- readonly IScheduler scheduler;
- public TimeIntervalObservable(IObservable<T> source, IScheduler scheduler)
- : base(scheduler == Scheduler.CurrentThread || source.IsRequiredSubscribeOnCurrentThread())
- {
- this.source = source;
- this.scheduler = scheduler;
- }
- protected override IDisposable SubscribeCore(IObserver<UniRx.TimeInterval<T>> observer, IDisposable cancel)
- {
- return source.Subscribe(new TimeInterval(this, observer, cancel));
- }
- class TimeInterval : OperatorObserverBase<T, UniRx.TimeInterval<T>>
- {
- readonly TimeIntervalObservable<T> parent;
- DateTimeOffset lastTime;
- public TimeInterval(TimeIntervalObservable<T> parent, IObserver<UniRx.TimeInterval<T>> observer, IDisposable cancel)
- : base(observer, cancel)
- {
- this.parent = parent;
- this.lastTime = parent.scheduler.Now;
- }
- public override void OnNext(T value)
- {
- var now = parent.scheduler.Now;
- var span = now.Subtract(lastTime);
- lastTime = now;
- base.observer.OnNext(new UniRx.TimeInterval<T>(value, span));
- }
- public override void OnError(Exception error)
- {
- try { observer.OnError(error); }
- finally { Dispose(); }
- }
- public override void OnCompleted()
- {
- try { observer.OnCompleted(); }
- finally { Dispose(); }
- }
- }
- }
- }
|