using System; namespace UniRx.Operators { internal class TimestampObservable : OperatorObservableBase> { readonly IObservable source; readonly IScheduler scheduler; public TimestampObservable(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 Timestamp(this, observer, cancel)); } class Timestamp : OperatorObserverBase> { readonly TimestampObservable parent; public Timestamp(TimestampObservable parent, IObserver> observer, IDisposable cancel) : base(observer, cancel) { this.parent = parent; } public override void OnNext(T value) { base.observer.OnNext(new Timestamped(value, parent.scheduler.Now)); } public override void OnError(Exception error) { try { observer.OnError(error); } finally { Dispose(); } } public override void OnCompleted() { try { observer.OnCompleted(); } finally { Dispose(); } } } } }