| 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 |
public static IAsyncEnumerable<TSource> Catch<TSource, TException>(this IAsyncEnumerable<TSource> source, Func<TException, IAsyncEnumerable<TSource>> handler) |
| 13 |
where TException : Exception |
| 14 |
{ |
| 15 |
if (source == null) |
| 16 |
throw new ArgumentNullException("source"); |
| 17 |
if (handler == null) |
| 18 |
throw new ArgumentNullException("handler"); |
| 19 |
|
| 20 |
return Create(() => |
| 21 |
{ |
| 22 |
var e = source.GetEnumerator(); |
| 23 |
|
| 24 |
var cts = new CancellationTokenDisposable(); |
| 25 |
var a = new AssignableDisposable { Disposable = e }; |
| 26 |
var d = new CompositeDisposable(cts, a); |
| 27 |
var done = false; |
| 28 |
|
| 29 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 30 |
f = (tcs, ct) => |
| 31 |
{ |
| 32 |
if (!done) |
| 33 |
{ |
| 34 |
e.MoveNext(ct).ContinueWith(t => |
| 35 |
{ |
| 36 |
t.Handle(tcs, |
| 37 |
res => |
| 38 |
{ |
| 39 |
tcs.TrySetResult(res); |
| 40 |
}, |
| 41 |
ex => |
| 42 |
{ |
| 43 |
var err = default(IAsyncEnumerator<TSource>); |
| 44 |
|
| 45 |
try |
| 46 |
{ |
| 47 |
ex.Flatten().Handle(ex_ => |
| 48 |
{ |
| 49 |
var exx = ex_ as TException; |
| 50 |
if (exx != null) |
| 51 |
{ |
| 52 |
err = handler(exx).GetEnumerator(); |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
return false; |
| 57 |
}); |
| 58 |
} |
| 59 |
catch (Exception ex2) |
| 60 |
{ |
| 61 |
tcs.TrySetException(ex2); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
if (err != null) |
| 66 |
{ |
| 67 |
e = err; |
| 68 |
a.Disposable = e; |
| 69 |
|
| 70 |
done = true; |
| 71 |
f(tcs, ct); |
| 72 |
} |
| 73 |
} |
| 74 |
); |
| 75 |
}); |
| 76 |
} |
| 77 |
else |
| 78 |
{ |
| 79 |
e.MoveNext(ct).ContinueWith(t => |
| 80 |
{ |
| 81 |
t.Handle(tcs, res => |
| 82 |
{ |
| 83 |
tcs.TrySetResult(res); |
| 84 |
}); |
| 85 |
}); |
| 86 |
} |
| 87 |
}; |
| 88 |
|
| 89 |
return Create( |
| 90 |
(ct, tcs) => |
| 91 |
{ |
| 92 |
f(tcs, cts.Token); |
| 93 |
return tcs.Task.UsingEnumerator(a); |
| 94 |
}, |
| 95 |
() => e.Current, |
| 96 |
d.Dispose |
| 97 |
); |
| 98 |
}); |
| 99 |
} |
| 100 |
|
| 101 |
public static IAsyncEnumerable<TSource> Catch<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources) |
| 102 |
{ |
| 103 |
if (sources == null) |
| 104 |
throw new ArgumentNullException("sources"); |
| 105 |
|
| 106 |
return sources.Catch_(); |
| 107 |
} |
| 108 |
|
| 109 |
public static IAsyncEnumerable<TSource> Catch<TSource>(params IAsyncEnumerable<TSource>[] sources) |
| 110 |
{ |
| 111 |
if (sources == null) |
| 112 |
throw new ArgumentNullException("sources"); |
| 113 |
|
| 114 |
return sources.Catch_(); |
| 115 |
} |
| 116 |
|
| 117 |
public static IAsyncEnumerable<TSource> Catch<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) |
| 118 |
{ |
| 119 |
if (first == null) |
| 120 |
throw new ArgumentNullException("first"); |
| 121 |
if (second == null) |
| 122 |
throw new ArgumentNullException("second"); |
| 123 |
|
| 124 |
return new[] { first, second }.Catch_(); |
| 125 |
} |
| 126 |
|
| 127 |
private static IAsyncEnumerable<TSource> Catch_<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources) |
| 128 |
{ |
| 129 |
return Create(() => |
| 130 |
{ |
| 131 |
var se = sources.GetEnumerator(); |
| 132 |
var e = default(IAsyncEnumerator<TSource>); |
| 133 |
|
| 134 |
var cts = new CancellationTokenDisposable(); |
| 135 |
var a = new AssignableDisposable(); |
| 136 |
var d = new CompositeDisposable(cts, se, a); |
| 137 |
|
| 138 |
var error = default(Exception); |
| 139 |
|
| 140 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 141 |
f = (tcs, ct) => |
| 142 |
{ |
| 143 |
if (e == null) |
| 144 |
{ |
| 145 |
var b = false; |
| 146 |
try |
| 147 |
{ |
| 148 |
b = se.MoveNext(); |
| 149 |
if (b) |
| 150 |
e = se.Current.GetEnumerator(); |
| 151 |
} |
| 152 |
catch (Exception ex) |
| 153 |
{ |
| 154 |
tcs.TrySetException(ex); |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
if (!b) |
| 159 |
{ |
| 160 |
if (error != null) |
| 161 |
{ |
| 162 |
tcs.TrySetException(error); |
| 163 |
return; |
| 164 |
} |
| 165 |
|
| 166 |
tcs.TrySetResult(false); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
error = null; |
| 171 |
|
| 172 |
a.Disposable = e; |
| 173 |
} |
| 174 |
|
| 175 |
e.MoveNext(ct).ContinueWith(t => |
| 176 |
{ |
| 177 |
t.Handle(tcs, |
| 178 |
res => |
| 179 |
{ |
| 180 |
tcs.TrySetResult(res); |
| 181 |
}, |
| 182 |
ex => |
| 183 |
{ |
| 184 |
e.Dispose(); |
| 185 |
e = null; |
| 186 |
|
| 187 |
error = ex; |
| 188 |
f(tcs, ct); |
| 189 |
} |
| 190 |
); |
| 191 |
}); |
| 192 |
}; |
| 193 |
|
| 194 |
return Create( |
| 195 |
(ct, tcs) => |
| 196 |
{ |
| 197 |
f(tcs, cts.Token); |
| 198 |
return tcs.Task.UsingEnumerator(a); |
| 199 |
}, |
| 200 |
() => e.Current, |
| 201 |
d.Dispose |
| 202 |
); |
| 203 |
}); |
| 204 |
} |
| 205 |
|
| 206 |
public static IAsyncEnumerable<TSource> Finally<TSource>(this IAsyncEnumerable<TSource> source, Action finallyAction) |
| 207 |
{ |
| 208 |
if (source == null) |
| 209 |
throw new ArgumentNullException("source"); |
| 210 |
if (finallyAction == null) |
| 211 |
throw new ArgumentNullException("finallyAction"); |
| 212 |
|
| 213 |
return Create(() => |
| 214 |
{ |
| 215 |
var e = source.GetEnumerator(); |
| 216 |
|
| 217 |
var cts = new CancellationTokenDisposable(); |
| 218 |
var r = new Disposable(finallyAction); |
| 219 |
var d = new CompositeDisposable(cts, e, r); |
| 220 |
|
| 221 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 222 |
f = (tcs, ct) => |
| 223 |
{ |
| 224 |
e.MoveNext(ct).ContinueWith(t => |
| 225 |
{ |
| 226 |
t.Handle(tcs, res => |
| 227 |
{ |
| 228 |
tcs.TrySetResult(res); |
| 229 |
}); |
| 230 |
}); |
| 231 |
}; |
| 232 |
|
| 233 |
return Create( |
| 234 |
(ct, tcs) => |
| 235 |
{ |
| 236 |
f(tcs, cts.Token); |
| 237 |
return tcs.Task.UsingEnumeratorSync(r); |
| 238 |
}, |
| 239 |
() => e.Current, |
| 240 |
d.Dispose |
| 241 |
); |
| 242 |
}); |
| 243 |
} |
| 244 |
|
| 245 |
public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second) |
| 246 |
{ |
| 247 |
if (first == null) |
| 248 |
throw new ArgumentNullException("first"); |
| 249 |
if (second == null) |
| 250 |
throw new ArgumentNullException("second"); |
| 251 |
|
| 252 |
return OnErrorResumeNext_(new[] { first, second }); |
| 253 |
} |
| 254 |
|
| 255 |
public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(params IAsyncEnumerable<TSource>[] sources) |
| 256 |
{ |
| 257 |
if (sources == null) |
| 258 |
throw new ArgumentNullException("sources"); |
| 259 |
|
| 260 |
return OnErrorResumeNext_(sources); |
| 261 |
} |
| 262 |
|
| 263 |
public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources) |
| 264 |
{ |
| 265 |
if (sources == null) |
| 266 |
throw new ArgumentNullException("sources"); |
| 267 |
|
| 268 |
return OnErrorResumeNext_(sources); |
| 269 |
} |
| 270 |
|
| 271 |
private static IAsyncEnumerable<TSource> OnErrorResumeNext_<TSource>(IEnumerable<IAsyncEnumerable<TSource>> sources) |
| 272 |
{ |
| 273 |
return Create(() => |
| 274 |
{ |
| 275 |
var se = sources.GetEnumerator(); |
| 276 |
var e = default(IAsyncEnumerator<TSource>); |
| 277 |
|
| 278 |
var cts = new CancellationTokenDisposable(); |
| 279 |
var a = new AssignableDisposable(); |
| 280 |
var d = new CompositeDisposable(cts, se, a); |
| 281 |
|
| 282 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 283 |
f = (tcs, ct) => |
| 284 |
{ |
| 285 |
if (e == null) |
| 286 |
{ |
| 287 |
var b = false; |
| 288 |
try |
| 289 |
{ |
| 290 |
b = se.MoveNext(); |
| 291 |
if (b) |
| 292 |
e = se.Current.GetEnumerator(); |
| 293 |
} |
| 294 |
catch (Exception ex) |
| 295 |
{ |
| 296 |
tcs.TrySetException(ex); |
| 297 |
return; |
| 298 |
} |
| 299 |
|
| 300 |
if (!b) |
| 301 |
{ |
| 302 |
tcs.TrySetResult(false); |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
a.Disposable = e; |
| 307 |
} |
| 308 |
|
| 309 |
e.MoveNext(ct).ContinueWith(t => |
| 310 |
{ |
| 311 |
t.Handle(tcs, |
| 312 |
res => |
| 313 |
{ |
| 314 |
if (res) |
| 315 |
{ |
| 316 |
tcs.TrySetResult(true); |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
e.Dispose(); |
| 321 |
e = null; |
| 322 |
|
| 323 |
f(tcs, ct); |
| 324 |
} |
| 325 |
}, |
| 326 |
ex => |
| 327 |
{ |
| 328 |
e.Dispose(); |
| 329 |
e = null; |
| 330 |
|
| 331 |
f(tcs, ct); |
| 332 |
} |
| 333 |
); |
| 334 |
}); |
| 335 |
}; |
| 336 |
|
| 337 |
return Create( |
| 338 |
(ct, tcs) => |
| 339 |
{ |
| 340 |
f(tcs, cts.Token); |
| 341 |
return tcs.Task.UsingEnumerator(a); |
| 342 |
}, |
| 343 |
() => e.Current, |
| 344 |
d.Dispose |
| 345 |
); |
| 346 |
}); |
| 347 |
} |
| 348 |
|
| 349 |
public static IAsyncEnumerable<TSource> Retry<TSource>(this IAsyncEnumerable<TSource> source) |
| 350 |
{ |
| 351 |
if (source == null) |
| 352 |
throw new ArgumentNullException("source"); |
| 353 |
|
| 354 |
return new[] { source }.Repeat().Catch(); |
| 355 |
} |
| 356 |
|
| 357 |
public static IAsyncEnumerable<TSource> Retry<TSource>(this IAsyncEnumerable<TSource> source, int retryCount) |
| 358 |
{ |
| 359 |
if (source == null) |
| 360 |
throw new ArgumentNullException("source"); |
| 361 |
if (retryCount < 0) |
| 362 |
throw new ArgumentOutOfRangeException("retryCount"); |
| 363 |
|
| 364 |
return new[] { source }.Repeat(retryCount).Catch(); |
| 365 |
} |
| 366 |
|
| 367 |
private static IEnumerable<TSource> Repeat<TSource>(this IEnumerable<TSource> source) |
| 368 |
{ |
| 369 |
while (true) |
| 370 |
foreach (var item in source) |
| 371 |
yield return item; |
| 372 |
} |
| 373 |
|
| 374 |
private static IEnumerable<TSource> Repeat<TSource>(this IEnumerable<TSource> source, int count) |
| 375 |
{ |
| 376 |
for (var i = 0; i < count; i++) |
| 377 |
foreach (var item in source) |
| 378 |
yield return item; |
| 379 |
} |
| 380 |
} |
| 381 |
} |