Timestamp.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. #if !NO_PERF
  3. using System;
  4. using System.Reactive.Concurrency;
  5. namespace System.Reactive.Linq.Observαble
  6. {
  7. 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 _(this, observer, cancel);
  19. setSink(sink);
  20. return _source.SubscribeSafe(sink);
  21. }
  22. class _ : Sink<Timestamped<TSource>>, IObserver<TSource>
  23. {
  24. private readonly Timestamp<TSource> _parent;
  25. public _(Timestamp<TSource> parent, IObserver<Timestamped<TSource>> observer, IDisposable cancel)
  26. : base(observer, cancel)
  27. {
  28. _parent = parent;
  29. }
  30. public void OnNext(TSource value)
  31. {
  32. base._observer.OnNext(new Timestamped<TSource>(value, _parent._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. }
  47. #endif