Timestamp.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. using System.Reactive.Concurrency;
  5. namespace System.Reactive.Linq.ObservableImpl
  6. {
  7. internal sealed class Timestamp<TSource> : Producer<Timestamped<TSource>>
  8. {
  9. private readonly IObservable<TSource> _source;
  10. private readonly IScheduler _scheduler;
  11. public Timestamp(IObservable<TSource> source, IScheduler scheduler)
  12. {
  13. _source = source;
  14. _scheduler = scheduler;
  15. }
  16. protected override IDisposable Run(IObserver<Timestamped<TSource>> observer, IDisposable cancel, Action<IDisposable> setSink)
  17. {
  18. var sink = new _(_scheduler, observer, cancel);
  19. setSink(sink);
  20. return _source.SubscribeSafe(sink);
  21. }
  22. private sealed class _ : Sink<Timestamped<TSource>>, IObserver<TSource>
  23. {
  24. private readonly IScheduler _scheduler;
  25. public _(IScheduler scheduler, IObserver<Timestamped<TSource>> observer, IDisposable cancel)
  26. : base(observer, cancel)
  27. {
  28. _scheduler = scheduler;
  29. }
  30. public void OnNext(TSource value)
  31. {
  32. base._observer.OnNext(new Timestamped<TSource>(value, _scheduler.Now));
  33. }
  34. public void OnError(Exception error)
  35. {
  36. base._observer.OnError(error);
  37. base.Dispose();
  38. }
  39. public void OnCompleted()
  40. {
  41. base._observer.OnCompleted();
  42. base.Dispose();
  43. }
  44. }
  45. }
  46. }