Debian Codegraph @

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