Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/SkipLast.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.Collections.Generic;
6 using System.Reactive.Concurrency;
7
8 namespace System.Reactive.Linq.ObservableImpl
9 {
10 class SkipLast<TSource> : Producer<TSource>
11 {
12 private readonly IObservable<TSource> _source;
13 private readonly int _count;
14 private readonly TimeSpan _duration;
15 private readonly IScheduler _scheduler;
16
17 public SkipLast(IObservable<TSource> source, int count)
18 {
19 _source = source;
20 _count = count;
21 }
22
23 public SkipLast(IObservable<TSource> source, TimeSpan duration, IScheduler scheduler)
24 {
25 _source = source;
26 _duration = duration;
27 _scheduler = scheduler;
28 }
29
30 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
31 {
32 if (_scheduler == null)
33 {
34 var sink = new _(this, observer, cancel);
35 setSink(sink);
36 return _source.SubscribeSafe(sink);
37 }
38 else
39 {
40 var sink = new SkipLastImpl(this, observer, cancel);
41 setSink(sink);
42 return sink.Run();
43 }
44 }
45
46 class _ : Sink<TSource>, IObserver<TSource>
47 {
48 private readonly SkipLast<TSource> _parent;
49 private Queue<TSource> _queue;
50
51 public _(SkipLast<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
52 : base(observer, cancel)
53 {
54 _parent = parent;
55 _queue = new Queue<TSource>();
56 }
57
58 public void OnNext(TSource value)
59 {
60 _queue.Enqueue(value);
61 if (_queue.Count > _parent._count)
62 base._observer.OnNext(_queue.Dequeue());
63 }
64
65 public void OnError(Exception error)
66 {
67 base._observer.OnError(error);
68 base.Dispose();
69 }
70
71 public void OnCompleted()
72 {
73 base._observer.OnCompleted();
74 base.Dispose();
75 }
76 }
77
78 class SkipLastImpl : Sink<TSource>, IObserver<TSource>
79 {
80 private readonly SkipLast<TSource> _parent;
81 private Queue<System.Reactive.TimeInterval<TSource>> _queue;
82
83 public SkipLastImpl(SkipLast<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
84 : base(observer, cancel)
85 {
86 _parent = parent;
87 _queue = new Queue<System.Reactive.TimeInterval<TSource>>();
88 }
89
90 private IStopwatch _watch;
91
92 public IDisposable Run()
93 {
94 _watch = _parent._scheduler.StartStopwatch();
95
96 return _parent._source.SubscribeSafe(this);
97 }
98
99 public void OnNext(TSource value)
100 {
101 var now = _watch.Elapsed;
102 _queue.Enqueue(new System.Reactive.TimeInterval<TSource>(value, now));
103 while (_queue.Count > 0 && now - _queue.Peek().Interval >= _parent._duration)
104 base._observer.OnNext(_queue.Dequeue().Value);
105 }
106
107 public void OnError(Exception error)
108 {
109 base._observer.OnError(error);
110 base.Dispose();
111 }
112
113 public void OnCompleted()
114 {
115 var now = _watch.Elapsed;
116 while (_queue.Count > 0 && now - _queue.Peek().Interval >= _parent._duration)
117 base._observer.OnNext(_queue.Dequeue().Value);
118
119 base._observer.OnCompleted();
120 base.Dispose();
121 }
122 }
123 }
124 }
125 #endif