Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/TimeInterval.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if !NO_PERF
4 using System;
5 using System.Diagnostics;
6 using System.Reactive.Concurrency;
7 using System.Reactive.Disposables;
8 using System.Threading;
9
10 namespace System.Reactive.Linq.ObservableImpl
11 {
12 class TimeInterval<TSource> : Producer<System.Reactive.TimeInterval<TSource>>
13 {
14 private readonly IObservable<TSource> _source;
15 private readonly IScheduler _scheduler;
16
17 public TimeInterval(IObservable<TSource> source, IScheduler scheduler)
18 {
19 _source = source;
20 _scheduler = scheduler;
21 }
22
23 protected override IDisposable Run(IObserver<System.Reactive.TimeInterval<TSource>> observer, IDisposable cancel, Action<IDisposable> setSink)
24 {
25 var sink = new _(this, observer, cancel);
26 setSink(sink);
27 return sink.Run();
28 }
29
30 class _ : Sink<System.Reactive.TimeInterval<TSource>>, IObserver<TSource>
31 {
32 private readonly TimeInterval<TSource> _parent;
33
34 public _(TimeInterval<TSource> parent, IObserver<System.Reactive.TimeInterval<TSource>> observer, IDisposable cancel)
35 : base(observer, cancel)
36 {
37 _parent = parent;
38 }
39
40 private IStopwatch _watch;
41 private TimeSpan _last;
42
43 public IDisposable Run()
44 {
45 _watch = _parent._scheduler.StartStopwatch();
46 _last = TimeSpan.Zero;
47
48 return _parent._source.Subscribe(this);
49 }
50
51 public void OnNext(TSource value)
52 {
53 var now = _watch.Elapsed;
54 var span = now.Subtract(_last);
55 _last = now;
56 base._observer.OnNext(new System.Reactive.TimeInterval<TSource>(value, span));
57 }
58
59 public void OnError(Exception error)
60 {
61 base._observer.OnError(error);
62 Dispose();
63 }
64
65 public void OnCompleted()
66 {
67 base._observer.OnCompleted();
68 Dispose();
69 }
70 }
71 }
72 }
73 #endif