Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/SkipWhile.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
6 namespace System.Reactive.Linq.ObservableImpl
7 {
8 class SkipWhile<TSource> : Producer<TSource>
9 {
10 private readonly IObservable<TSource> _source;
11 private readonly Func<TSource, bool> _predicate;
12 private readonly Func<TSource, int, bool> _predicateI;
13
14 public SkipWhile(IObservable<TSource> source, Func<TSource, bool> predicate)
15 {
16 _source = source;
17 _predicate = predicate;
18 }
19
20 public SkipWhile(IObservable<TSource> source, Func<TSource, int, bool> predicate)
21 {
22 _source = source;
23 _predicateI = predicate;
24 }
25
26 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
27 {
28 if (_predicate != null)
29 {
30 var sink = new _(this, observer, cancel);
31 setSink(sink);
32 return _source.SubscribeSafe(sink);
33 }
34 else
35 {
36 var sink = new SkipWhileImpl(this, observer, cancel);
37 setSink(sink);
38 return _source.SubscribeSafe(sink);
39 }
40 }
41
42 class _ : Sink<TSource>, IObserver<TSource>
43 {
44 private readonly SkipWhile<TSource> _parent;
45 private bool _running;
46
47 public _(SkipWhile<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
48 : base(observer, cancel)
49 {
50 _parent = parent;
51 _running = false;
52 }
53
54 public void OnNext(TSource value)
55 {
56 if (!_running)
57 {
58 try
59 {
60 _running = !_parent._predicate(value);
61 }
62 catch (Exception exception)
63 {
64 base._observer.OnError(exception);
65 base.Dispose();
66 return;
67 }
68 }
69
70 if (_running)
71 {
72 base._observer.OnNext(value);
73 }
74 }
75
76 public void OnError(Exception error)
77 {
78 base._observer.OnError(error);
79 base.Dispose();
80 }
81
82 public void OnCompleted()
83 {
84 base._observer.OnCompleted();
85 base.Dispose();
86 }
87 }
88
89 class SkipWhileImpl : Sink<TSource>, IObserver<TSource>
90 {
91 private readonly SkipWhile<TSource> _parent;
92 private bool _running;
93 private int _index;
94
95 public SkipWhileImpl(SkipWhile<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
96 : base(observer, cancel)
97 {
98 _parent = parent;
99 _running = false;
100 _index = 0;
101 }
102
103 public void OnNext(TSource value)
104 {
105 if (!_running)
106 {
107 try
108 {
109 _running = !_parent._predicateI(value, checked(_index++));
110 }
111 catch (Exception exception)
112 {
113 base._observer.OnError(exception);
114 base.Dispose();
115 return;
116 }
117 }
118
119 if (_running)
120 {
121 base._observer.OnNext(value);
122 }
123 }
124
125 public void OnError(Exception error)
126 {
127 base._observer.OnError(error);
128 base.Dispose();
129 }
130
131 public void OnCompleted()
132 {
133 base._observer.OnCompleted();
134 base.Dispose();
135 }
136 }
137 }
138 }
139 #endif