Debian Codegraph @

external/rx/Ix/NET/System.Interactive.Async/AsyncEnumerable.Creation.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Threading;
6 using System.Threading.Tasks;
7
8 namespace System.Linq
9 {
10 public static partial class AsyncEnumerable
11 {
12 static IAsyncEnumerable<T> Create<T>(Func<IAsyncEnumerator<T>> getEnumerator)
13 {
14 return new AnonymousAsyncEnumerable<T>(getEnumerator);
15 }
16
17 class AnonymousAsyncEnumerable<T> : IAsyncEnumerable<T>
18 {
19 Func<IAsyncEnumerator<T>> getEnumerator;
20
21 public AnonymousAsyncEnumerable(Func<IAsyncEnumerator<T>> getEnumerator)
22 {
23 this.getEnumerator = getEnumerator;
24 }
25
26 public IAsyncEnumerator<T> GetEnumerator()
27 {
28 return getEnumerator();
29 }
30 }
31
32 static IAsyncEnumerator<T> Create<T>(Func<CancellationToken, Task<bool>> moveNext, Func<T> current, Action dispose)
33 {
34 return new AnonymousAsyncEnumerator<T>(moveNext, current, dispose);
35 }
36
37 static IAsyncEnumerator<T> Create<T>(Func<CancellationToken, TaskCompletionSource<bool>, Task<bool>> moveNext, Func<T> current, Action dispose)
38 {
39 var self = default(IAsyncEnumerator<T>);
40 self = new AnonymousAsyncEnumerator<T>(
41 ct =>
42 {
43 var tcs = new TaskCompletionSource<bool>();
44
45 var stop = new Action(() =>
46 {
47 self.Dispose();
48 tcs.TrySetCanceled();
49 });
50
51 var ctr = ct.Register(stop);
52
53 var res = moveNext(ct, tcs).Finally(ctr.Dispose);
54 return res;
55 },
56 current,
57 dispose
58 );
59 return self;
60 }
61
62 class AnonymousAsyncEnumerator<T> : IAsyncEnumerator<T>
63 {
64 private readonly Func<CancellationToken, Task<bool>> _moveNext;
65 private readonly Func<T> _current;
66 private readonly Action _dispose;
67 private bool _disposed;
68
69 public AnonymousAsyncEnumerator(Func<CancellationToken, Task<bool>> moveNext, Func<T> current, Action dispose)
70 {
71 _moveNext = moveNext;
72 _current = current;
73 _dispose = dispose;
74 }
75
76 public Task<bool> MoveNext(CancellationToken cancellationToken)
77 {
78 if (_disposed)
79 return TaskExt.Return(false, CancellationToken.None);
80
81 return _moveNext(cancellationToken);
82 }
83
84 public T Current
85 {
86 get
87 {
88 return _current();
89 }
90 }
91
92 public void Dispose()
93 {
94 if (!_disposed)
95 {
96 _disposed = true;
97 _dispose();
98 }
99 }
100 }
101
102 public static IAsyncEnumerable<TValue> Return<TValue>(TValue value)
103 {
104 return new[] { value }.ToAsyncEnumerable();
105 }
106
107 public static IAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
108 {
109 if (exception == null)
110 throw new ArgumentNullException("exception");
111
112 return Create(() => Create<TValue>(
113 ct => TaskExt.Throw<bool>(exception, ct),
114 () => { throw new InvalidOperationException(); },
115 () => { })
116 );
117 }
118
119 public static IAsyncEnumerable<TValue> Never<TValue>()
120 {
121 return Create(() => Create<TValue>(
122 (ct, tcs) => tcs.Task,
123 () => { throw new InvalidOperationException(); },
124 () => { })
125 );
126 }
127
128 public static IAsyncEnumerable<TValue> Empty<TValue>()
129 {
130 return Create(() => Create<TValue>(
131 ct => TaskExt.Return(false, ct),
132 () => { throw new InvalidOperationException(); },
133 () => { })
134 );
135 }
136
137 public static IAsyncEnumerable<int> Range(int start, int count)
138 {
139 if (count < 0)
140 throw new ArgumentOutOfRangeException("count");
141
142 return Enumerable.Range(start, count).ToAsyncEnumerable();
143 }
144
145 public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element, int count)
146 {
147 if (count < 0)
148 throw new ArgumentOutOfRangeException("count");
149
150 return Enumerable.Repeat(element, count).ToAsyncEnumerable();
151 }
152
153 public static IAsyncEnumerable<TResult> Repeat<TResult>(TResult element)
154 {
155 return Create(() =>
156 {
157 return Create(
158 ct => TaskExt.Return(true, ct),
159 () => element,
160 () => { }
161 );
162 });
163 }
164
165 public static IAsyncEnumerable<TSource> Defer<TSource>(Func<IAsyncEnumerable<TSource>> factory)
166 {
167 if (factory == null)
168 throw new ArgumentNullException("factory");
169
170 return Create(() => factory().GetEnumerator());
171 }
172
173 public static IAsyncEnumerable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
174 {
175 if (condition == null)
176 throw new ArgumentNullException("condition");
177 if (iterate == null)
178 throw new ArgumentNullException("iterate");
179 if (resultSelector == null)
180 throw new ArgumentNullException("resultSelector");
181
182 return Create(() =>
183 {
184 var i = initialState;
185 var started = false;
186 var current = default(TResult);
187
188 return Create(
189 ct =>
190 {
191 var b = false;
192 try
193 {
194 if (started)
195 i = iterate(i);
196
197 b = condition(i);
198
199 if (b)
200 current = resultSelector(i);
201 }
202 catch (Exception ex)
203 {
204 return TaskExt.Throw<bool>(ex, ct);
205 }
206
207 if (!b)
208 return TaskExt.Return(false, ct);
209
210 if (!started)
211 started = true;
212
213 return TaskExt.Return(true, ct);
214 },
215 () => current,
216 () => { }
217 );
218 });
219 }
220
221 public static IAsyncEnumerable<TSource> Using<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory) where TResource : IDisposable
222 {
223 if (resourceFactory == null)
224 throw new ArgumentNullException("resourceFactory");
225 if (enumerableFactory == null)
226 throw new ArgumentNullException("enumerableFactory");
227
228 return Create(() =>
229 {
230 var resource = resourceFactory();
231 var e = default(IAsyncEnumerator<TSource>);
232
233 try
234 {
235 e = enumerableFactory(resource).GetEnumerator();
236 }
237 catch (Exception)
238 {
239 resource.Dispose();
240 throw;
241 }
242
243 var cts = new CancellationTokenDisposable();
244 var d = new CompositeDisposable(cts, resource, e);
245
246 var current = default(TSource);
247
248 return Create(
249 (ct, tcs) =>
250 {
251 e.MoveNext(cts.Token).ContinueWith(t =>
252 {
253 t.Handle(tcs,
254 res =>
255 {
256 if (res)
257 {
258 current = e.Current;
259 tcs.TrySetResult(true);
260 }
261 else
262 {
263 d.Dispose();
264 tcs.TrySetResult(false);
265 }
266 },
267 ex =>
268 {
269 d.Dispose();
270 tcs.TrySetException(ex);
271 }
272 );
273 });
274
275 return tcs.Task;
276 },
277 () => current,
278 d.Dispose
279 );
280 });
281 }
282 }
283 }