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