Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Catch.cs « Observαble « Linq « Reactive « System.Reactive.Linq « Rx.NET - github.com/mono/rx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 71e003796cfa71731c52287a1a8c0b064b4220dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

#if !NO_PERF
using System;
using System.Collections.Generic;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;

namespace System.Reactive.Linq.Observαble
{
    class Catch<TSource> : Producer<TSource>
    {
        private readonly IEnumerable<IObservable<TSource>> _sources;

        public Catch(IEnumerable<IObservable<TSource>> sources)
        {
            _sources = sources;
        }

        protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
        {
            var sink = new _(observer, cancel);
            setSink(sink);
            return sink.Run(_sources);
        }

        class _ : TailRecursiveSink<TSource>
        {
            public _(IObserver<TSource> observer, IDisposable cancel)
                : base(observer, cancel)
            {
            }

            protected override IEnumerable<IObservable<TSource>> Extract(IObservable<TSource> source)
            {
                var @catch = source as Catch<TSource>;
                if (@catch != null)
                    return @catch._sources;

                return null;
            }

            public override void OnNext(TSource value)
            {
                base._observer.OnNext(value);
            }

            private Exception _lastException;

            public override void OnError(Exception error)
            {
                _lastException = error;
                _recurse();
            }

            public override void OnCompleted()
            {
                base._observer.OnCompleted();
                base.Dispose();
            }

            protected override void Done()
            {
                if (_lastException != null)
                    base._observer.OnError(_lastException);
                else
                    base._observer.OnCompleted();

                base.Dispose();
            }
        }
    }

    class Catch<TSource, TException> : Producer<TSource> where TException : Exception
    {
        private readonly IObservable<TSource> _source;
        private readonly Func<TException, IObservable<TSource>> _handler;

        public Catch(IObservable<TSource> source, Func<TException, IObservable<TSource>> handler)
        {
            _source = source;
            _handler = handler;
        }

        protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
        {
            var sink = new _(this, observer, cancel);
            setSink(sink);
            return sink.Run();
        }

        class _ : Sink<TSource>, IObserver<TSource>
        {
            private readonly Catch<TSource, TException> _parent;

            public _(Catch<TSource, TException> parent, IObserver<TSource> observer, IDisposable cancel)
                : base(observer, cancel)
            {
                _parent = parent;
            }

            private SerialDisposable _subscription;

            public IDisposable Run()
            {
                _subscription = new SerialDisposable();

                var d1 = new SingleAssignmentDisposable();
                _subscription.Disposable = d1;
                d1.Disposable = _parent._source.SubscribeSafe(this);

                return _subscription;
            }

            public void OnNext(TSource value)
            {
                base._observer.OnNext(value);
            }

            public void OnError(Exception error)
            {
                var e = error as TException;
                if (e != null)
                {
                    var result = default(IObservable<TSource>);
                    try
                    {
                        result = _parent._handler(e);
                    }
                    catch (Exception ex)
                    {
                        base._observer.OnError(ex);
                        base.Dispose();
                        return;
                    }

                    var d = new SingleAssignmentDisposable();
                    _subscription.Disposable = d;
                    d.Disposable = result.SubscribeSafe(new ε(this));
                }
                else
                {
                    base._observer.OnError(error);
                    base.Dispose();
                }
            }

            public void OnCompleted()
            {
                base._observer.OnCompleted();
                base.Dispose();
            }

            class ε : IObserver<TSource>
            {
                private readonly _ _parent;

                public ε(_ parent)
                {
                    _parent = parent;
                }

                public void OnNext(TSource value)
                {
                    _parent._observer.OnNext(value);
                }

                public void OnError(Exception error)
                {
                    _parent._observer.OnError(error);
                    _parent.Dispose();
                }

                public void OnCompleted()
                {
                    _parent._observer.OnCompleted();
                    _parent.Dispose();
                }
            }
        }
    }
}
#endif