Debian Codegraph @

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