Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/Contains.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
7 namespace System.Reactive.Linq.ObservableImpl
8 {
9 class Contains<TSource> : Producer<bool>
10 {
11 private readonly IObservable<TSource> _source;
12 private readonly TSource _value;
13 private readonly IEqualityComparer<TSource> _comparer;
14
15 public Contains(IObservable<TSource> source, TSource value, IEqualityComparer<TSource> comparer)
16 {
17 _source = source;
18 _value = value;
19 _comparer = comparer;
20 }
21
22 protected override IDisposable Run(IObserver<bool> observer, IDisposable cancel, Action<IDisposable> setSink)
23 {
24 var sink = new _(this, observer, cancel);
25 setSink(sink);
26 return _source.SubscribeSafe(sink);
27 }
28
29 class _ : Sink<bool>, IObserver<TSource>
30 {
31 private readonly Contains<TSource> _parent;
32
33 public _(Contains<TSource> parent, IObserver<bool> observer, IDisposable cancel)
34 : base(observer, cancel)
35 {
36 _parent = parent;
37 }
38
39 public void OnNext(TSource value)
40 {
41 var res = false;
42 try
43 {
44 res = _parent._comparer.Equals(value, _parent._value);
45 }
46 catch (Exception ex)
47 {
48 base._observer.OnError(ex);
49 base.Dispose();
50 return;
51 }
52
53 if (res)
54 {
55 base._observer.OnNext(true);
56 base._observer.OnCompleted();
57 base.Dispose();
58 }
59 }
60
61 public void OnError(Exception error)
62 {
63 base._observer.OnError(error);
64 base.Dispose();
65 }
66
67 public void OnCompleted()
68 {
69 base._observer.OnNext(false);
70 base._observer.OnCompleted();
71 base.Dispose();
72 }
73 }
74 }
75 }
76 #endif