Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Internal/TailRecursiveSink.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.Collections.Generic;
5 using System.Reactive.Concurrency;
6 using System.Reactive.Disposables;
7
8 namespace System.Reactive
9 {
10 abstract class TailRecursiveSink<TSource> : Sink<TSource>, IObserver<TSource>
11 {
12 public TailRecursiveSink(IObserver<TSource> observer, IDisposable cancel)
13 : base(observer, cancel)
14 {
15 }
16
17 private bool _isDisposed;
18 private SerialDisposable _subscription;
19 private AsyncLock _gate;
20 private Stack<IEnumerator<IObservable<TSource>>> _stack;
21 private Stack<int?> _length;
22 protected Action _recurse;
23
24 public IDisposable Run(IEnumerable<IObservable<TSource>> sources)
25 {
26 _isDisposed = false;
27 _subscription = new SerialDisposable();
28 _gate = new AsyncLock();
29 _stack = new Stack<IEnumerator<IObservable<TSource>>>();
30 _length = new Stack<int?>();
31
32 var e = default(IEnumerator<IObservable<TSource>>);
33 if (!TryGetEnumerator(sources, out e))
34 return Disposable.Empty;
35
36 _stack.Push(e);
37 _length.Push(Helpers.GetLength(sources));
38
39 var cancelable = SchedulerDefaults.TailRecursion.Schedule(self =>
40 {
41 _recurse = self;
42 _gate.Wait(MoveNext);
43 });
44
45 return new CompositeDisposable(_subscription, cancelable, Disposable.Create(() => _gate.Wait(Dispose)));
46 }
47
48 protected abstract IEnumerable<IObservable<TSource>> Extract(IObservable<TSource> source);
49
50 private void MoveNext()
51 {
52 var hasNext = false;
53 var next = default(IObservable<TSource>);
54
55 do
56 {
57 if (_stack.Count == 0)
58 break;
59
60 if (_isDisposed)
61 return;
62
63 var e = _stack.Peek();
64 var l = _length.Peek();
65
66 var current = default(IObservable<TSource>);
67 try
68 {
69 hasNext = e.MoveNext();
70 if (hasNext)
71 current = e.Current;
72 }
73 catch (Exception ex)
74 {
75 e.Dispose();
76
77 base._observer.OnError(ex);
78 base.Dispose();
79 return;
80 }
81
82 if (!hasNext)
83 {
84 e.Dispose();
85
86 _stack.Pop();
87 _length.Pop();
88 }
89 else
90 {
91 var r = l - 1;
92 _length.Pop();
93 _length.Push(r);
94
95 try
96 {
97 next = Helpers.Unpack(current);
98 }
99 catch (Exception exception)
100 {
101 e.Dispose();
102 base._observer.OnError(exception);
103 base.Dispose();
104 return;
105 }
106
107 //
108 // Tail recursive case; drop the current frame.
109 //
110 if (r == 0)
111 {
112 e.Dispose();
113 _stack.Pop();
114 _length.Pop();
115 }
116
117 //
118 // Flattening of nested sequences. Prevents stack overflow in observers.
119 //
120 var nextSeq = Extract(next);
121 if (nextSeq != null)
122 {
123 var nextEnumerator = default(IEnumerator<IObservable<TSource>>);
124 if (!TryGetEnumerator(nextSeq, out nextEnumerator))
125 return;
126
127 _stack.Push(nextEnumerator);
128 _length.Push(Helpers.GetLength(nextSeq));
129
130 hasNext = false;
131 }
132 }
133 } while (!hasNext);
134
135 if (!hasNext)
136 {
137 Done();
138 return;
139 }
140
141 var d = new SingleAssignmentDisposable();
142 _subscription.Disposable = d;
143 d.Disposable = next.SubscribeSafe(this);
144 }
145
146 private new void Dispose()
147 {
148 while (_stack.Count > 0)
149 {
150 var e = _stack.Pop();
151 _length.Pop();
152
153 e.Dispose();
154 }
155
156 _isDisposed = true;
157 }
158
159 private bool TryGetEnumerator(IEnumerable<IObservable<TSource>> sources, out IEnumerator<IObservable<TSource>> result)
160 {
161 try
162 {
163 result = sources.GetEnumerator();
164 return true;
165 }
166 catch (Exception exception)
167 {
168 base._observer.OnError(exception);
169 base.Dispose();
170
171 result = null;
172 return false;
173 }
174 }
175
176 public abstract void OnCompleted();
177 public abstract void OnError(Exception error);
178 public abstract void OnNext(TSource value);
179
180 protected virtual void Done()
181 {
182 base._observer.OnCompleted();
183 base.Dispose();
184 }
185 }
186 }
187 #endif