Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/ForEach.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.Threading;
6
7 namespace System.Reactive.Linq.ObservableImpl
8 {
9 class ForEach<TSource>
10 {
11 public class _ : IObserver<TSource>
12 {
13 private readonly Action<TSource> _onNext;
14 private readonly Action _done;
15
16 private Exception _exception;
17 private int _stopped;
18
19 public _(Action<TSource> onNext, Action done)
20 {
21 _onNext = onNext;
22 _done = done;
23
24 _stopped = 0;
25 }
26
27 public Exception Error
28 {
29 get { return _exception; }
30 }
31
32 public void OnNext(TSource value)
33 {
34 if (_stopped == 0)
35 {
36 try
37 {
38 _onNext(value);
39 }
40 catch (Exception ex)
41 {
42 OnError(ex);
43 }
44 }
45 }
46
47 public void OnError(Exception error)
48 {
49 if (Interlocked.Exchange(ref _stopped, 1) == 0)
50 {
51 _exception = error;
52 _done();
53 }
54 }
55
56 public void OnCompleted()
57 {
58 if (Interlocked.Exchange(ref _stopped, 1) == 0)
59 {
60 _done();
61 }
62 }
63 }
64
65 public class ForEachImpl : IObserver<TSource>
66 {
67 private readonly Action<TSource, int> _onNext;
68 private readonly Action _done;
69
70 private int _index;
71 private Exception _exception;
72 private int _stopped;
73
74 public ForEachImpl(Action<TSource, int> onNext, Action done)
75 {
76 _onNext = onNext;
77 _done = done;
78
79 _index = 0;
80 _stopped = 0;
81 }
82
83 public Exception Error
84 {
85 get { return _exception; }
86 }
87
88 public void OnNext(TSource value)
89 {
90 if (_stopped == 0)
91 {
92 try
93 {
94 _onNext(value, checked(_index++));
95 }
96 catch (Exception ex)
97 {
98 OnError(ex);
99 }
100 }
101 }
102
103 public void OnError(Exception error)
104 {
105 if (Interlocked.Exchange(ref _stopped, 1) == 0)
106 {
107 _exception = error;
108 _done();
109 }
110 }
111
112 public void OnCompleted()
113 {
114 if (Interlocked.Exchange(ref _stopped, 1) == 0)
115 {
116 _done();
117 }
118 }
119 }
120 }
121 }
122 #endif