Timestamp.cs 1.7 KB

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