| 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<TResult> Select<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TResult> selector) |
| 13 |
{ |
| 14 |
if (source == null) |
| 15 |
throw new ArgumentNullException("source"); |
| 16 |
if (selector == null) |
| 17 |
throw new ArgumentNullException("selector"); |
| 18 |
|
| 19 |
return Create(() => |
| 20 |
{ |
| 21 |
var e = source.GetEnumerator(); |
| 22 |
var current = default(TResult); |
| 23 |
|
| 24 |
var cts = new CancellationTokenDisposable(); |
| 25 |
var d = new CompositeDisposable(cts, e); |
| 26 |
|
| 27 |
return Create( |
| 28 |
(ct, tcs) => |
| 29 |
{ |
| 30 |
e.MoveNext(cts.Token).ContinueWith(t => |
| 31 |
{ |
| 32 |
t.Handle(tcs, res => |
| 33 |
{ |
| 34 |
if (res) |
| 35 |
{ |
| 36 |
try |
| 37 |
{ |
| 38 |
current = selector(e.Current); |
| 39 |
} |
| 40 |
catch (Exception ex) |
| 41 |
{ |
| 42 |
tcs.TrySetException(ex); |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
tcs.TrySetResult(true); |
| 47 |
} |
| 48 |
else |
| 49 |
{ |
| 50 |
tcs.TrySetResult(false); |
| 51 |
} |
| 52 |
}); |
| 53 |
}); |
| 54 |
|
| 55 |
return tcs.Task.UsingEnumerator(e); |
| 56 |
}, |
| 57 |
() => current, |
| 58 |
d.Dispose |
| 59 |
); |
| 60 |
}); |
| 61 |
} |
| 62 |
|
| 63 |
public static IAsyncEnumerable<TResult> Select<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, int, TResult> selector) |
| 64 |
{ |
| 65 |
if (source == null) |
| 66 |
throw new ArgumentNullException("source"); |
| 67 |
if (selector == null) |
| 68 |
throw new ArgumentNullException("selector"); |
| 69 |
|
| 70 |
return Create(() => |
| 71 |
{ |
| 72 |
var e = source.GetEnumerator(); |
| 73 |
var current = default(TResult); |
| 74 |
var index = 0; |
| 75 |
|
| 76 |
var cts = new CancellationTokenDisposable(); |
| 77 |
var d = new CompositeDisposable(cts, e); |
| 78 |
|
| 79 |
return Create( |
| 80 |
(ct, tcs) => |
| 81 |
{ |
| 82 |
e.MoveNext(cts.Token).ContinueWith(t => |
| 83 |
{ |
| 84 |
t.Handle(tcs, res => |
| 85 |
{ |
| 86 |
if (res) |
| 87 |
{ |
| 88 |
try |
| 89 |
{ |
| 90 |
current = selector(e.Current, index++); |
| 91 |
} |
| 92 |
catch (Exception ex) |
| 93 |
{ |
| 94 |
tcs.TrySetException(ex); |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
tcs.TrySetResult(true); |
| 99 |
} |
| 100 |
else |
| 101 |
{ |
| 102 |
tcs.TrySetResult(false); |
| 103 |
} |
| 104 |
}); |
| 105 |
}); |
| 106 |
|
| 107 |
return tcs.Task.UsingEnumerator(e); |
| 108 |
}, |
| 109 |
() => current, |
| 110 |
d.Dispose |
| 111 |
); |
| 112 |
}); |
| 113 |
} |
| 114 |
|
| 115 |
public static IAsyncEnumerable<TSource> AsAsyncEnumerable<TSource>(this IAsyncEnumerable<TSource> source) |
| 116 |
{ |
| 117 |
if (source == null) |
| 118 |
throw new ArgumentNullException("source"); |
| 119 |
|
| 120 |
return source.Select(x => x); |
| 121 |
} |
| 122 |
|
| 123 |
public static IAsyncEnumerable<TSource> Where<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) |
| 124 |
{ |
| 125 |
if (source == null) |
| 126 |
throw new ArgumentNullException("source"); |
| 127 |
if (predicate == null) |
| 128 |
throw new ArgumentNullException("predicate"); |
| 129 |
|
| 130 |
return Create(() => |
| 131 |
{ |
| 132 |
var e = source.GetEnumerator(); |
| 133 |
|
| 134 |
var cts = new CancellationTokenDisposable(); |
| 135 |
var d = new CompositeDisposable(cts, e); |
| 136 |
|
| 137 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 138 |
f = (tcs, ct) => |
| 139 |
{ |
| 140 |
e.MoveNext(ct).ContinueWith(t => |
| 141 |
{ |
| 142 |
t.Handle(tcs, res => |
| 143 |
{ |
| 144 |
if (res) |
| 145 |
{ |
| 146 |
var b = false; |
| 147 |
try |
| 148 |
{ |
| 149 |
b = predicate(e.Current); |
| 150 |
} |
| 151 |
catch (Exception ex) |
| 152 |
{ |
| 153 |
tcs.TrySetException(ex); |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
if (b) |
| 158 |
tcs.TrySetResult(true); |
| 159 |
else |
| 160 |
f(tcs, ct); |
| 161 |
} |
| 162 |
else |
| 163 |
tcs.TrySetResult(false); |
| 164 |
}); |
| 165 |
}); |
| 166 |
}; |
| 167 |
|
| 168 |
return Create( |
| 169 |
(ct, tcs) => |
| 170 |
{ |
| 171 |
f(tcs, cts.Token); |
| 172 |
return tcs.Task.UsingEnumerator(e); |
| 173 |
}, |
| 174 |
() => e.Current, |
| 175 |
d.Dispose |
| 176 |
); |
| 177 |
}); |
| 178 |
} |
| 179 |
|
| 180 |
public static IAsyncEnumerable<TSource> Where<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate) |
| 181 |
{ |
| 182 |
if (source == null) |
| 183 |
throw new ArgumentNullException("source"); |
| 184 |
if (predicate == null) |
| 185 |
throw new ArgumentNullException("predicate"); |
| 186 |
|
| 187 |
return Create(() => |
| 188 |
{ |
| 189 |
var e = source.GetEnumerator(); |
| 190 |
var index = 0; |
| 191 |
|
| 192 |
var cts = new CancellationTokenDisposable(); |
| 193 |
var d = new CompositeDisposable(cts, e); |
| 194 |
|
| 195 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 196 |
f = (tcs, ct) => |
| 197 |
{ |
| 198 |
e.MoveNext(ct).ContinueWith(t => |
| 199 |
{ |
| 200 |
t.Handle(tcs, res => |
| 201 |
{ |
| 202 |
if (res) |
| 203 |
{ |
| 204 |
var b = false; |
| 205 |
try |
| 206 |
{ |
| 207 |
b = predicate(e.Current, index++); |
| 208 |
} |
| 209 |
catch (Exception ex) |
| 210 |
{ |
| 211 |
tcs.TrySetException(ex); |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
if (b) |
| 216 |
tcs.TrySetResult(true); |
| 217 |
else |
| 218 |
f(tcs, ct); |
| 219 |
} |
| 220 |
else |
| 221 |
tcs.TrySetResult(false); |
| 222 |
}); |
| 223 |
}); |
| 224 |
}; |
| 225 |
|
| 226 |
return Create( |
| 227 |
(ct, tcs) => |
| 228 |
{ |
| 229 |
f(tcs, cts.Token); |
| 230 |
return tcs.Task.UsingEnumerator(e); |
| 231 |
}, |
| 232 |
() => e.Current, |
| 233 |
d.Dispose |
| 234 |
); |
| 235 |
}); |
| 236 |
} |
| 237 |
|
| 238 |
public static IAsyncEnumerable<TResult> SelectMany<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, IAsyncEnumerable<TResult>> selector) |
| 239 |
{ |
| 240 |
if (source == null) |
| 241 |
throw new ArgumentNullException("source"); |
| 242 |
if (selector == null) |
| 243 |
throw new ArgumentNullException("selector"); |
| 244 |
|
| 245 |
return Create(() => |
| 246 |
{ |
| 247 |
var e = source.GetEnumerator(); |
| 248 |
var ie = default(IAsyncEnumerator<TResult>); |
| 249 |
|
| 250 |
var cts = new CancellationTokenDisposable(); |
| 251 |
var d = new CompositeDisposable(cts, e); |
| 252 |
|
| 253 |
var outer = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 254 |
var inner = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 255 |
|
| 256 |
inner = (tcs, ct) => |
| 257 |
{ |
| 258 |
ie.MoveNext(ct).ContinueWith(t => |
| 259 |
{ |
| 260 |
t.Handle(tcs, res => |
| 261 |
{ |
| 262 |
if (res) |
| 263 |
{ |
| 264 |
tcs.TrySetResult(true); |
| 265 |
} |
| 266 |
else |
| 267 |
{ |
| 268 |
ie = null; |
| 269 |
outer(tcs, ct); |
| 270 |
} |
| 271 |
}); |
| 272 |
}); |
| 273 |
}; |
| 274 |
|
| 275 |
outer = (tcs, ct) => |
| 276 |
{ |
| 277 |
e.MoveNext(ct).ContinueWith(t => |
| 278 |
{ |
| 279 |
t.Handle(tcs, res => |
| 280 |
{ |
| 281 |
if (res) |
| 282 |
{ |
| 283 |
try |
| 284 |
{ |
| 285 |
ie = selector(e.Current).GetEnumerator(); |
| 286 |
inner(tcs, ct); |
| 287 |
} |
| 288 |
catch (Exception ex) |
| 289 |
{ |
| 290 |
tcs.TrySetException(ex); |
| 291 |
} |
| 292 |
} |
| 293 |
else |
| 294 |
tcs.TrySetResult(false); |
| 295 |
}); |
| 296 |
}); |
| 297 |
}; |
| 298 |
|
| 299 |
return Create( |
| 300 |
(ct, tcs) => |
| 301 |
{ |
| 302 |
if (ie == null) |
| 303 |
outer(tcs, cts.Token); |
| 304 |
else |
| 305 |
inner(tcs, cts.Token); |
| 306 |
|
| 307 |
return tcs.Task.UsingEnumerator(e); |
| 308 |
}, |
| 309 |
() => ie.Current, |
| 310 |
d.Dispose |
| 311 |
); |
| 312 |
}); |
| 313 |
} |
| 314 |
|
| 315 |
public static IAsyncEnumerable<TResult> SelectMany<TSource, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, int, IAsyncEnumerable<TResult>> selector) |
| 316 |
{ |
| 317 |
if (source == null) |
| 318 |
throw new ArgumentNullException("source"); |
| 319 |
if (selector == null) |
| 320 |
throw new ArgumentNullException("selector"); |
| 321 |
|
| 322 |
return Create(() => |
| 323 |
{ |
| 324 |
var e = source.GetEnumerator(); |
| 325 |
var ie = default(IAsyncEnumerator<TResult>); |
| 326 |
var index = 0; |
| 327 |
|
| 328 |
var cts = new CancellationTokenDisposable(); |
| 329 |
var d = new CompositeDisposable(cts, e); |
| 330 |
|
| 331 |
var outer = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 332 |
var inner = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 333 |
|
| 334 |
inner = (tcs, ct) => |
| 335 |
{ |
| 336 |
ie.MoveNext(ct).ContinueWith(t => |
| 337 |
{ |
| 338 |
t.Handle(tcs, res => |
| 339 |
{ |
| 340 |
if (res) |
| 341 |
{ |
| 342 |
tcs.TrySetResult(true); |
| 343 |
} |
| 344 |
else |
| 345 |
{ |
| 346 |
ie = null; |
| 347 |
outer(tcs, ct); |
| 348 |
} |
| 349 |
}); |
| 350 |
}); |
| 351 |
}; |
| 352 |
|
| 353 |
outer = (tcs, ct) => |
| 354 |
{ |
| 355 |
e.MoveNext(ct).ContinueWith(t => |
| 356 |
{ |
| 357 |
t.Handle(tcs, res => |
| 358 |
{ |
| 359 |
if (res) |
| 360 |
{ |
| 361 |
try |
| 362 |
{ |
| 363 |
ie = selector(e.Current, index++).GetEnumerator(); |
| 364 |
inner(tcs, ct); |
| 365 |
} |
| 366 |
catch (Exception ex) |
| 367 |
{ |
| 368 |
tcs.TrySetException(ex); |
| 369 |
} |
| 370 |
} |
| 371 |
else |
| 372 |
tcs.TrySetResult(false); |
| 373 |
}); |
| 374 |
}); |
| 375 |
}; |
| 376 |
|
| 377 |
return Create( |
| 378 |
(ct, tcs) => |
| 379 |
{ |
| 380 |
if (ie == null) |
| 381 |
outer(tcs, cts.Token); |
| 382 |
else |
| 383 |
inner(tcs, cts.Token); |
| 384 |
|
| 385 |
return tcs.Task.UsingEnumerator(e); |
| 386 |
}, |
| 387 |
() => ie.Current, |
| 388 |
d.Dispose |
| 389 |
); |
| 390 |
}); |
| 391 |
} |
| 392 |
|
| 393 |
public static IAsyncEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, IAsyncEnumerable<TCollection>> selector, Func<TSource, TCollection, TResult> resultSelector) |
| 394 |
{ |
| 395 |
if (source == null) |
| 396 |
throw new ArgumentNullException("source"); |
| 397 |
if (selector == null) |
| 398 |
throw new ArgumentNullException("selector"); |
| 399 |
if (resultSelector == null) |
| 400 |
throw new ArgumentNullException("resultSelector"); |
| 401 |
|
| 402 |
return source.SelectMany(x => selector(x).Select(y => resultSelector(x, y))); |
| 403 |
} |
| 404 |
|
| 405 |
public static IAsyncEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, int, IAsyncEnumerable<TCollection>> selector, Func<TSource, TCollection, TResult> resultSelector) |
| 406 |
{ |
| 407 |
if (source == null) |
| 408 |
throw new ArgumentNullException("source"); |
| 409 |
if (selector == null) |
| 410 |
throw new ArgumentNullException("selector"); |
| 411 |
if (resultSelector == null) |
| 412 |
throw new ArgumentNullException("resultSelector"); |
| 413 |
|
| 414 |
return source.SelectMany((x, i) => selector(x, i).Select(y => resultSelector(x, y))); |
| 415 |
} |
| 416 |
|
| 417 |
public static IAsyncEnumerable<TType> OfType<TType>(this IAsyncEnumerable<object> source) |
| 418 |
{ |
| 419 |
if (source == null) |
| 420 |
throw new ArgumentNullException("source"); |
| 421 |
|
| 422 |
return source.Where(x => x is TType).Cast<TType>(); |
| 423 |
} |
| 424 |
|
| 425 |
public static IAsyncEnumerable<TResult> Cast<TResult>(this IAsyncEnumerable<object> source) |
| 426 |
{ |
| 427 |
if (source == null) |
| 428 |
throw new ArgumentNullException("source"); |
| 429 |
|
| 430 |
return source.Select(x => (TResult)x); |
| 431 |
} |
| 432 |
|
| 433 |
public static IAsyncEnumerable<TSource> Take<TSource>(this IAsyncEnumerable<TSource> source, int count) |
| 434 |
{ |
| 435 |
if (source == null) |
| 436 |
throw new ArgumentNullException("source"); |
| 437 |
if (count < 0) |
| 438 |
throw new ArgumentOutOfRangeException("count"); |
| 439 |
|
| 440 |
return Create(() => |
| 441 |
{ |
| 442 |
var e = source.GetEnumerator(); |
| 443 |
var n = count; |
| 444 |
|
| 445 |
var cts = new CancellationTokenDisposable(); |
| 446 |
var d = new CompositeDisposable(cts, e); |
| 447 |
|
| 448 |
return Create( |
| 449 |
(ct, tcs) => |
| 450 |
{ |
| 451 |
if (n == 0) |
| 452 |
return TaskExt.Return(false, cts.Token); |
| 453 |
|
| 454 |
e.MoveNext(cts.Token).ContinueWith(t => |
| 455 |
{ |
| 456 |
t.Handle(tcs, res => |
| 457 |
{ |
| 458 |
--n; |
| 459 |
tcs.TrySetResult(res); |
| 460 |
}); |
| 461 |
}); |
| 462 |
|
| 463 |
return tcs.Task.UsingEnumerator(e); |
| 464 |
}, |
| 465 |
() => e.Current, |
| 466 |
d.Dispose |
| 467 |
); |
| 468 |
}); |
| 469 |
} |
| 470 |
|
| 471 |
public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) |
| 472 |
{ |
| 473 |
if (source == null) |
| 474 |
throw new ArgumentNullException("source"); |
| 475 |
if (predicate == null) |
| 476 |
throw new ArgumentNullException("predicate"); |
| 477 |
|
| 478 |
return Create(() => |
| 479 |
{ |
| 480 |
var e = source.GetEnumerator(); |
| 481 |
|
| 482 |
var cts = new CancellationTokenDisposable(); |
| 483 |
var d = new CompositeDisposable(cts, e); |
| 484 |
|
| 485 |
return Create( |
| 486 |
(ct, tcs) => |
| 487 |
{ |
| 488 |
e.MoveNext(cts.Token).ContinueWith(t => |
| 489 |
{ |
| 490 |
t.Handle(tcs, res => |
| 491 |
{ |
| 492 |
if (res) |
| 493 |
{ |
| 494 |
var b = false; |
| 495 |
|
| 496 |
try |
| 497 |
{ |
| 498 |
b = predicate(e.Current); |
| 499 |
} |
| 500 |
catch (Exception ex) |
| 501 |
{ |
| 502 |
tcs.TrySetException(ex); |
| 503 |
return; |
| 504 |
} |
| 505 |
|
| 506 |
if (b) |
| 507 |
{ |
| 508 |
tcs.TrySetResult(true); |
| 509 |
return; |
| 510 |
} |
| 511 |
} |
| 512 |
tcs.TrySetResult(false); |
| 513 |
}); |
| 514 |
}); |
| 515 |
|
| 516 |
return tcs.Task.UsingEnumerator(e); |
| 517 |
}, |
| 518 |
() => e.Current, |
| 519 |
d.Dispose |
| 520 |
); |
| 521 |
}); |
| 522 |
} |
| 523 |
|
| 524 |
public static IAsyncEnumerable<TSource> TakeWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate) |
| 525 |
{ |
| 526 |
if (source == null) |
| 527 |
throw new ArgumentNullException("source"); |
| 528 |
if (predicate == null) |
| 529 |
throw new ArgumentNullException("predicate"); |
| 530 |
|
| 531 |
return Create(() => |
| 532 |
{ |
| 533 |
var e = source.GetEnumerator(); |
| 534 |
var index = 0; |
| 535 |
|
| 536 |
var cts = new CancellationTokenDisposable(); |
| 537 |
var d = new CompositeDisposable(cts, e); |
| 538 |
|
| 539 |
return Create( |
| 540 |
(ct, tcs) => |
| 541 |
{ |
| 542 |
e.MoveNext(cts.Token).ContinueWith(t => |
| 543 |
{ |
| 544 |
t.Handle(tcs, res => |
| 545 |
{ |
| 546 |
if (res) |
| 547 |
{ |
| 548 |
var b = false; |
| 549 |
|
| 550 |
try |
| 551 |
{ |
| 552 |
b = predicate(e.Current, index++); |
| 553 |
} |
| 554 |
catch (Exception ex) |
| 555 |
{ |
| 556 |
tcs.TrySetException(ex); |
| 557 |
return; |
| 558 |
} |
| 559 |
|
| 560 |
if (b) |
| 561 |
{ |
| 562 |
tcs.TrySetResult(true); |
| 563 |
return; |
| 564 |
} |
| 565 |
} |
| 566 |
tcs.TrySetResult(false); |
| 567 |
}); |
| 568 |
}); |
| 569 |
|
| 570 |
return tcs.Task.UsingEnumerator(e); |
| 571 |
}, |
| 572 |
() => e.Current, |
| 573 |
d.Dispose |
| 574 |
); |
| 575 |
}); |
| 576 |
} |
| 577 |
|
| 578 |
public static IAsyncEnumerable<TSource> Skip<TSource>(this IAsyncEnumerable<TSource> source, int count) |
| 579 |
{ |
| 580 |
if (source == null) |
| 581 |
throw new ArgumentNullException("source"); |
| 582 |
if (count < 0) |
| 583 |
throw new ArgumentOutOfRangeException("count"); |
| 584 |
|
| 585 |
return Create(() => |
| 586 |
{ |
| 587 |
var e = source.GetEnumerator(); |
| 588 |
var n = count; |
| 589 |
|
| 590 |
var cts = new CancellationTokenDisposable(); |
| 591 |
var d = new CompositeDisposable(cts, e); |
| 592 |
|
| 593 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 594 |
f = (tcs, ct) => |
| 595 |
{ |
| 596 |
if (n == 0) |
| 597 |
e.MoveNext(ct).ContinueWith(t => |
| 598 |
{ |
| 599 |
t.Handle(tcs, x => tcs.TrySetResult(x)); |
| 600 |
}); |
| 601 |
else |
| 602 |
{ |
| 603 |
--n; |
| 604 |
e.MoveNext(ct).ContinueWith(t => |
| 605 |
{ |
| 606 |
t.Handle(tcs, res => |
| 607 |
{ |
| 608 |
if (!res) |
| 609 |
tcs.TrySetResult(false); |
| 610 |
else |
| 611 |
f(tcs, ct); |
| 612 |
}); |
| 613 |
}); |
| 614 |
} |
| 615 |
}; |
| 616 |
|
| 617 |
return Create( |
| 618 |
(ct, tcs) => |
| 619 |
{ |
| 620 |
f(tcs, cts.Token); |
| 621 |
return tcs.Task.UsingEnumerator(e); |
| 622 |
}, |
| 623 |
() => e.Current, |
| 624 |
d.Dispose |
| 625 |
); |
| 626 |
}); |
| 627 |
} |
| 628 |
|
| 629 |
public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate) |
| 630 |
{ |
| 631 |
if (source == null) |
| 632 |
throw new ArgumentNullException("source"); |
| 633 |
if (predicate == null) |
| 634 |
throw new ArgumentNullException("predicate"); |
| 635 |
|
| 636 |
return Create(() => |
| 637 |
{ |
| 638 |
var e = source.GetEnumerator(); |
| 639 |
var skipping = true; |
| 640 |
|
| 641 |
var cts = new CancellationTokenDisposable(); |
| 642 |
var d = new CompositeDisposable(cts, e); |
| 643 |
|
| 644 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 645 |
f = (tcs, ct) => |
| 646 |
{ |
| 647 |
if (skipping) |
| 648 |
e.MoveNext(ct).ContinueWith(t => |
| 649 |
{ |
| 650 |
t.Handle(tcs, res => |
| 651 |
{ |
| 652 |
if (res) |
| 653 |
{ |
| 654 |
var result = false; |
| 655 |
try |
| 656 |
{ |
| 657 |
result = predicate(e.Current); |
| 658 |
} |
| 659 |
catch (Exception ex) |
| 660 |
{ |
| 661 |
tcs.TrySetException(ex); |
| 662 |
return; |
| 663 |
} |
| 664 |
if (result) |
| 665 |
f(tcs, ct); |
| 666 |
else |
| 667 |
{ |
| 668 |
skipping = false; |
| 669 |
tcs.TrySetResult(true); |
| 670 |
} |
| 671 |
} |
| 672 |
else |
| 673 |
tcs.TrySetResult(false); |
| 674 |
}); |
| 675 |
}); |
| 676 |
else |
| 677 |
e.MoveNext(ct).ContinueWith(t => |
| 678 |
{ |
| 679 |
t.Handle(tcs, x => tcs.TrySetResult(x)); |
| 680 |
}); |
| 681 |
}; |
| 682 |
|
| 683 |
return Create( |
| 684 |
(ct, tcs) => |
| 685 |
{ |
| 686 |
f(tcs, cts.Token); |
| 687 |
return tcs.Task.UsingEnumerator(e); |
| 688 |
}, |
| 689 |
() => e.Current, |
| 690 |
d.Dispose |
| 691 |
); |
| 692 |
}); |
| 693 |
} |
| 694 |
|
| 695 |
public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate) |
| 696 |
{ |
| 697 |
if (source == null) |
| 698 |
throw new ArgumentNullException("source"); |
| 699 |
if (predicate == null) |
| 700 |
throw new ArgumentNullException("predicate"); |
| 701 |
|
| 702 |
return Create(() => |
| 703 |
{ |
| 704 |
var e = source.GetEnumerator(); |
| 705 |
var skipping = true; |
| 706 |
var index = 0; |
| 707 |
|
| 708 |
var cts = new CancellationTokenDisposable(); |
| 709 |
var d = new CompositeDisposable(cts, e); |
| 710 |
|
| 711 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 712 |
f = (tcs, ct) => |
| 713 |
{ |
| 714 |
if (skipping) |
| 715 |
e.MoveNext(ct).ContinueWith(t => |
| 716 |
{ |
| 717 |
t.Handle(tcs, res => |
| 718 |
{ |
| 719 |
if (res) |
| 720 |
{ |
| 721 |
var result = false; |
| 722 |
try |
| 723 |
{ |
| 724 |
result = predicate(e.Current, index++); |
| 725 |
} |
| 726 |
catch (Exception ex) |
| 727 |
{ |
| 728 |
tcs.TrySetException(ex); |
| 729 |
return; |
| 730 |
} |
| 731 |
if (result) |
| 732 |
f(tcs, ct); |
| 733 |
else |
| 734 |
{ |
| 735 |
skipping = false; |
| 736 |
tcs.TrySetResult(true); |
| 737 |
} |
| 738 |
} |
| 739 |
else |
| 740 |
tcs.TrySetResult(false); |
| 741 |
}); |
| 742 |
}); |
| 743 |
else |
| 744 |
e.MoveNext(ct).ContinueWith(t => |
| 745 |
{ |
| 746 |
t.Handle(tcs, x => tcs.TrySetResult(x)); |
| 747 |
}); |
| 748 |
}; |
| 749 |
|
| 750 |
return Create( |
| 751 |
(ct, tcs) => |
| 752 |
{ |
| 753 |
f(tcs, cts.Token); |
| 754 |
return tcs.Task.UsingEnumerator(e); |
| 755 |
}, |
| 756 |
() => e.Current, |
| 757 |
d.Dispose |
| 758 |
); |
| 759 |
}); |
| 760 |
} |
| 761 |
|
| 762 |
public static IAsyncEnumerable<TSource> DefaultIfEmpty<TSource>(this IAsyncEnumerable<TSource> source, TSource defaultValue) |
| 763 |
{ |
| 764 |
if (source == null) |
| 765 |
throw new ArgumentNullException("source"); |
| 766 |
|
| 767 |
return Create(() => |
| 768 |
{ |
| 769 |
var done = false; |
| 770 |
var hasElements = false; |
| 771 |
var e = source.GetEnumerator(); |
| 772 |
var current = default(TSource); |
| 773 |
|
| 774 |
var cts = new CancellationTokenDisposable(); |
| 775 |
var d = new CompositeDisposable(cts, e); |
| 776 |
|
| 777 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 778 |
f = (tcs, ct) => |
| 779 |
{ |
| 780 |
if (done) |
| 781 |
tcs.TrySetResult(false); |
| 782 |
else |
| 783 |
e.MoveNext(ct).ContinueWith(t => |
| 784 |
{ |
| 785 |
t.Handle(tcs, res => |
| 786 |
{ |
| 787 |
if (res) |
| 788 |
{ |
| 789 |
hasElements = true; |
| 790 |
current = e.Current; |
| 791 |
tcs.TrySetResult(true); |
| 792 |
} |
| 793 |
else |
| 794 |
{ |
| 795 |
done = true; |
| 796 |
|
| 797 |
if (!hasElements) |
| 798 |
{ |
| 799 |
current = defaultValue; |
| 800 |
tcs.TrySetResult(true); |
| 801 |
} |
| 802 |
else |
| 803 |
tcs.TrySetResult(false); |
| 804 |
} |
| 805 |
}); |
| 806 |
}); |
| 807 |
}; |
| 808 |
|
| 809 |
return Create( |
| 810 |
(ct, tcs) => |
| 811 |
{ |
| 812 |
f(tcs, cts.Token); |
| 813 |
return tcs.Task.UsingEnumerator(e); |
| 814 |
}, |
| 815 |
() => current, |
| 816 |
d.Dispose |
| 817 |
); |
| 818 |
}); |
| 819 |
} |
| 820 |
|
| 821 |
public static IAsyncEnumerable<TSource> DefaultIfEmpty<TSource>(this IAsyncEnumerable<TSource> source) |
| 822 |
{ |
| 823 |
if (source == null) |
| 824 |
throw new ArgumentNullException("source"); |
| 825 |
|
| 826 |
return source.DefaultIfEmpty(default(TSource)); |
| 827 |
} |
| 828 |
|
| 829 |
public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer) |
| 830 |
{ |
| 831 |
if (source == null) |
| 832 |
throw new ArgumentNullException("source"); |
| 833 |
if (comparer == null) |
| 834 |
throw new ArgumentNullException("comparer"); |
| 835 |
|
| 836 |
return Defer(() => |
| 837 |
{ |
| 838 |
var set = new HashSet<TSource>(comparer); |
| 839 |
return source.Where(set.Add); |
| 840 |
}); |
| 841 |
} |
| 842 |
|
| 843 |
public static IAsyncEnumerable<TSource> Distinct<TSource>(this IAsyncEnumerable<TSource> source) |
| 844 |
{ |
| 845 |
if (source == null) |
| 846 |
throw new ArgumentNullException("source"); |
| 847 |
|
| 848 |
return source.Distinct(EqualityComparer<TSource>.Default); |
| 849 |
} |
| 850 |
|
| 851 |
public static IAsyncEnumerable<TSource> Reverse<TSource>(this IAsyncEnumerable<TSource> source) |
| 852 |
{ |
| 853 |
if (source == null) |
| 854 |
throw new ArgumentNullException("source"); |
| 855 |
|
| 856 |
return Create(() => |
| 857 |
{ |
| 858 |
var e = source.GetEnumerator(); |
| 859 |
var stack = default(Stack<TSource>); |
| 860 |
|
| 861 |
var cts = new CancellationTokenDisposable(); |
| 862 |
var d = new CompositeDisposable(cts, e); |
| 863 |
|
| 864 |
return Create( |
| 865 |
(ct, tcs) => |
| 866 |
{ |
| 867 |
if (stack == null) |
| 868 |
{ |
| 869 |
Create(() => e).Aggregate(new Stack<TSource>(), (s, x) => { s.Push(x); return s; }, cts.Token).ContinueWith(t => |
| 870 |
{ |
| 871 |
t.Handle(tcs, res => |
| 872 |
{ |
| 873 |
stack = res; |
| 874 |
tcs.TrySetResult(stack.Count > 0); |
| 875 |
}); |
| 876 |
}, cts.Token); |
| 877 |
} |
| 878 |
else |
| 879 |
{ |
| 880 |
stack.Pop(); |
| 881 |
tcs.TrySetResult(stack.Count > 0); |
| 882 |
} |
| 883 |
|
| 884 |
return tcs.Task.UsingEnumerator(e); |
| 885 |
}, |
| 886 |
() => stack.Peek(), |
| 887 |
d.Dispose |
| 888 |
); |
| 889 |
}); |
| 890 |
} |
| 891 |
|
| 892 |
public static IOrderedAsyncEnumerable<TSource> OrderBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) |
| 893 |
{ |
| 894 |
if (source == null) |
| 895 |
throw new ArgumentNullException("source"); |
| 896 |
if (keySelector == null) |
| 897 |
throw new ArgumentNullException("keySelector"); |
| 898 |
if (comparer == null) |
| 899 |
throw new ArgumentNullException("comparer"); |
| 900 |
|
| 901 |
return new OrderedAsyncEnumerable<TSource, TKey>( |
| 902 |
Create(() => |
| 903 |
{ |
| 904 |
var current = default(IEnumerable<TSource>); |
| 905 |
|
| 906 |
return Create( |
| 907 |
ct => |
| 908 |
{ |
| 909 |
var tcs = new TaskCompletionSource<bool>(); |
| 910 |
if (current == null) |
| 911 |
{ |
| 912 |
source.ToList(ct).ContinueWith(t => |
| 913 |
{ |
| 914 |
t.Handle(tcs, res => |
| 915 |
{ |
| 916 |
current = res; |
| 917 |
tcs.TrySetResult(true); |
| 918 |
}); |
| 919 |
}); |
| 920 |
} |
| 921 |
else |
| 922 |
tcs.TrySetResult(false); |
| 923 |
return tcs.Task; |
| 924 |
}, |
| 925 |
() => current, |
| 926 |
() => { } |
| 927 |
); |
| 928 |
}), |
| 929 |
keySelector, |
| 930 |
comparer |
| 931 |
); |
| 932 |
} |
| 933 |
|
| 934 |
public static IOrderedAsyncEnumerable<TSource> OrderBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) |
| 935 |
{ |
| 936 |
if (source == null) |
| 937 |
throw new ArgumentNullException("source"); |
| 938 |
if (keySelector == null) |
| 939 |
throw new ArgumentNullException("keySelector"); |
| 940 |
|
| 941 |
return source.OrderBy(keySelector, Comparer<TKey>.Default); |
| 942 |
} |
| 943 |
|
| 944 |
public static IOrderedAsyncEnumerable<TSource> OrderByDescending<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) |
| 945 |
{ |
| 946 |
if (source == null) |
| 947 |
throw new ArgumentNullException("source"); |
| 948 |
if (keySelector == null) |
| 949 |
throw new ArgumentNullException("keySelector"); |
| 950 |
if (comparer == null) |
| 951 |
throw new ArgumentNullException("comparer"); |
| 952 |
|
| 953 |
return source.OrderBy(keySelector, new ReverseComparer<TKey>(comparer)); |
| 954 |
} |
| 955 |
|
| 956 |
public static IOrderedAsyncEnumerable<TSource> OrderByDescending<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) |
| 957 |
{ |
| 958 |
if (source == null) |
| 959 |
throw new ArgumentNullException("source"); |
| 960 |
if (keySelector == null) |
| 961 |
throw new ArgumentNullException("keySelector"); |
| 962 |
|
| 963 |
return source.OrderByDescending(keySelector, Comparer<TKey>.Default); |
| 964 |
} |
| 965 |
|
| 966 |
public static IOrderedAsyncEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) |
| 967 |
{ |
| 968 |
if (source == null) |
| 969 |
throw new ArgumentNullException("source"); |
| 970 |
if (keySelector == null) |
| 971 |
throw new ArgumentNullException("keySelector"); |
| 972 |
|
| 973 |
return source.ThenBy(keySelector, Comparer<TKey>.Default); |
| 974 |
} |
| 975 |
|
| 976 |
public static IOrderedAsyncEnumerable<TSource> ThenBy<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) |
| 977 |
{ |
| 978 |
if (source == null) |
| 979 |
throw new ArgumentNullException("source"); |
| 980 |
if (keySelector == null) |
| 981 |
throw new ArgumentNullException("keySelector"); |
| 982 |
if (comparer == null) |
| 983 |
throw new ArgumentNullException("comparer"); |
| 984 |
|
| 985 |
return source.CreateOrderedEnumerable(keySelector, comparer, false); |
| 986 |
} |
| 987 |
|
| 988 |
public static IOrderedAsyncEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) |
| 989 |
{ |
| 990 |
if (source == null) |
| 991 |
throw new ArgumentNullException("source"); |
| 992 |
if (keySelector == null) |
| 993 |
throw new ArgumentNullException("keySelector"); |
| 994 |
|
| 995 |
return source.ThenByDescending(keySelector, Comparer<TKey>.Default); |
| 996 |
} |
| 997 |
|
| 998 |
public static IOrderedAsyncEnumerable<TSource> ThenByDescending<TSource, TKey>(this IOrderedAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer) |
| 999 |
{ |
| 1000 |
if (source == null) |
| 1001 |
throw new ArgumentNullException("source"); |
| 1002 |
if (keySelector == null) |
| 1003 |
throw new ArgumentNullException("keySelector"); |
| 1004 |
if (comparer == null) |
| 1005 |
throw new ArgumentNullException("comparer"); |
| 1006 |
|
| 1007 |
return source.CreateOrderedEnumerable(keySelector, comparer, true); |
| 1008 |
} |
| 1009 |
|
| 1010 |
static IEnumerable<IGrouping<TKey, TElement>> GroupUntil<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IComparer<TKey> comparer) |
| 1011 |
{ |
| 1012 |
var group = default(EnumerableGrouping<TKey, TElement>); |
| 1013 |
foreach (var x in source) |
| 1014 |
{ |
| 1015 |
var key = keySelector(x); |
| 1016 |
if (group == null || comparer.Compare(group.Key, key) != 0) |
| 1017 |
{ |
| 1018 |
group = new EnumerableGrouping<TKey, TElement>(key); |
| 1019 |
yield return group; |
| 1020 |
} |
| 1021 |
group.Add(elementSelector(x)); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
class OrderedAsyncEnumerable<T, K> : IOrderedAsyncEnumerable<T> |
| 1026 |
{ |
| 1027 |
private readonly IAsyncEnumerable<IEnumerable<T>> equivalenceClasses; |
| 1028 |
private readonly Func<T, K> keySelector; |
| 1029 |
private readonly IComparer<K> comparer; |
| 1030 |
|
| 1031 |
public OrderedAsyncEnumerable(IAsyncEnumerable<IEnumerable<T>> equivalenceClasses, Func<T, K> keySelector, IComparer<K> comparer) |
| 1032 |
{ |
| 1033 |
this.equivalenceClasses = equivalenceClasses; |
| 1034 |
this.keySelector = keySelector; |
| 1035 |
this.comparer = comparer; |
| 1036 |
} |
| 1037 |
|
| 1038 |
public IOrderedAsyncEnumerable<T> CreateOrderedEnumerable<TKey>(Func<T, TKey> keySelector, IComparer<TKey> comparer, bool descending) |
| 1039 |
{ |
| 1040 |
if (descending) |
| 1041 |
comparer = new ReverseComparer<TKey>(comparer); |
| 1042 |
|
| 1043 |
return new OrderedAsyncEnumerable<T, TKey>(Classes(), keySelector, comparer); |
| 1044 |
} |
| 1045 |
|
| 1046 |
IAsyncEnumerable<IEnumerable<T>> Classes() |
| 1047 |
{ |
| 1048 |
return Create(() => |
| 1049 |
{ |
| 1050 |
var e = equivalenceClasses.GetEnumerator(); |
| 1051 |
var list = new List<IEnumerable<T>>(); |
| 1052 |
var e1 = default(IEnumerator<IEnumerable<T>>); |
| 1053 |
|
| 1054 |
var cts = new CancellationTokenDisposable(); |
| 1055 |
var d1 = new AssignableDisposable(); |
| 1056 |
var d = new CompositeDisposable(cts, e, d1); |
| 1057 |
|
| 1058 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 1059 |
var g = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 1060 |
|
| 1061 |
f = (tcs, ct) => |
| 1062 |
{ |
| 1063 |
e.MoveNext(ct).ContinueWith(t => |
| 1064 |
{ |
| 1065 |
t.Handle(tcs, res => |
| 1066 |
{ |
| 1067 |
if (res) |
| 1068 |
{ |
| 1069 |
try |
| 1070 |
{ |
| 1071 |
foreach (var group in e.Current.OrderBy(keySelector, comparer).GroupUntil(keySelector, x => x, comparer)) |
| 1072 |
list.Add(group); |
| 1073 |
f(tcs, ct); |
| 1074 |
} |
| 1075 |
catch (Exception exception) |
| 1076 |
{ |
| 1077 |
tcs.TrySetException(exception); |
| 1078 |
return; |
| 1079 |
} |
| 1080 |
} |
| 1081 |
else |
| 1082 |
{ |
| 1083 |
e.Dispose(); |
| 1084 |
|
| 1085 |
e1 = list.GetEnumerator(); |
| 1086 |
d1.Disposable = e1; |
| 1087 |
|
| 1088 |
g(tcs, ct); |
| 1089 |
} |
| 1090 |
}); |
| 1091 |
}); |
| 1092 |
}; |
| 1093 |
|
| 1094 |
g = (tcs, ct) => |
| 1095 |
{ |
| 1096 |
var res = false; |
| 1097 |
try |
| 1098 |
{ |
| 1099 |
res = e1.MoveNext(); |
| 1100 |
} |
| 1101 |
catch (Exception ex) |
| 1102 |
{ |
| 1103 |
tcs.TrySetException(ex); |
| 1104 |
return; |
| 1105 |
} |
| 1106 |
|
| 1107 |
tcs.TrySetResult(res); |
| 1108 |
}; |
| 1109 |
|
| 1110 |
return Create( |
| 1111 |
(ct, tcs) => |
| 1112 |
{ |
| 1113 |
if (e1 != null) |
| 1114 |
{ |
| 1115 |
g(tcs, cts.Token); |
| 1116 |
return tcs.Task.UsingEnumerator(e1); |
| 1117 |
} |
| 1118 |
else |
| 1119 |
{ |
| 1120 |
f(tcs, cts.Token); |
| 1121 |
return tcs.Task.UsingEnumerator(e); |
| 1122 |
} |
| 1123 |
}, |
| 1124 |
() => e1.Current, |
| 1125 |
d.Dispose |
| 1126 |
); |
| 1127 |
}); |
| 1128 |
} |
| 1129 |
|
| 1130 |
public IAsyncEnumerator<T> GetEnumerator() |
| 1131 |
{ |
| 1132 |
return Classes().SelectMany(x => x.ToAsyncEnumerable()).GetEnumerator(); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
class ReverseComparer<T> : IComparer<T> |
| 1137 |
{ |
| 1138 |
IComparer<T> comparer; |
| 1139 |
|
| 1140 |
public ReverseComparer(IComparer<T> comparer) |
| 1141 |
{ |
| 1142 |
this.comparer = comparer; |
| 1143 |
} |
| 1144 |
|
| 1145 |
public int Compare(T x, T y) |
| 1146 |
{ |
| 1147 |
return -comparer.Compare(x, y); |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
public static IAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer) |
| 1152 |
{ |
| 1153 |
if (source == null) |
| 1154 |
throw new ArgumentNullException("source"); |
| 1155 |
if (keySelector == null) |
| 1156 |
throw new ArgumentNullException("keySelector"); |
| 1157 |
if (elementSelector == null) |
| 1158 |
throw new ArgumentNullException("elementSelector"); |
| 1159 |
if (comparer == null) |
| 1160 |
throw new ArgumentNullException("comparer"); |
| 1161 |
|
| 1162 |
return Create(() => |
| 1163 |
{ |
| 1164 |
var gate = new object(); |
| 1165 |
|
| 1166 |
var e = source.GetEnumerator(); |
| 1167 |
var count = 1; |
| 1168 |
|
| 1169 |
var map = new Dictionary<TKey, Grouping<TKey, TElement>>(comparer); |
| 1170 |
var list = new List<IAsyncGrouping<TKey, TElement>>(); |
| 1171 |
|
| 1172 |
var index = 0; |
| 1173 |
|
| 1174 |
var current = default(IAsyncGrouping<TKey, TElement>); |
| 1175 |
var faulted = default(Exception); |
| 1176 |
|
| 1177 |
var task = default(Task<bool>); |
| 1178 |
|
| 1179 |
var cts = new CancellationTokenDisposable(); |
| 1180 |
var refCount = new Disposable( |
| 1181 |
() => |
| 1182 |
{ |
| 1183 |
if (Interlocked.Decrement(ref count) == 0) |
| 1184 |
e.Dispose(); |
| 1185 |
} |
| 1186 |
); |
| 1187 |
var d = new CompositeDisposable(cts, refCount); |
| 1188 |
|
| 1189 |
var iterateSource = default(Func<CancellationToken, Task<bool>>); |
| 1190 |
iterateSource = ct => |
| 1191 |
{ |
| 1192 |
var tcs = default(TaskCompletionSource<bool>); |
| 1193 |
lock (gate) |
| 1194 |
{ |
| 1195 |
if (task != null) |
| 1196 |
{ |
| 1197 |
return task; |
| 1198 |
} |
| 1199 |
else |
| 1200 |
{ |
| 1201 |
tcs = new TaskCompletionSource<bool>(); |
| 1202 |
task = tcs.Task.UsingEnumerator(e); |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
if (faulted != null) |
| 1207 |
{ |
| 1208 |
tcs.TrySetException(faulted); |
| 1209 |
return task; |
| 1210 |
} |
| 1211 |
|
| 1212 |
e.MoveNext(ct).ContinueWith(t => |
| 1213 |
{ |
| 1214 |
t.Handle(tcs, |
| 1215 |
res => |
| 1216 |
{ |
| 1217 |
if (res) |
| 1218 |
{ |
| 1219 |
var key = default(TKey); |
| 1220 |
var element = default(TElement); |
| 1221 |
|
| 1222 |
var cur = e.Current; |
| 1223 |
try |
| 1224 |
{ |
| 1225 |
key = keySelector(cur); |
| 1226 |
element = elementSelector(cur); |
| 1227 |
} |
| 1228 |
catch (Exception exception) |
| 1229 |
{ |
| 1230 |
foreach (var v in map.Values) |
| 1231 |
v.Error(exception); |
| 1232 |
|
| 1233 |
tcs.TrySetException(exception); |
| 1234 |
return; |
| 1235 |
} |
| 1236 |
|
| 1237 |
var group = default(Grouping<TKey, TElement>); |
| 1238 |
if (!map.TryGetValue(key, out group)) |
| 1239 |
{ |
| 1240 |
group = new Grouping<TKey, TElement>(key, iterateSource, refCount); |
| 1241 |
map.Add(key, group); |
| 1242 |
lock (list) |
| 1243 |
list.Add(group); |
| 1244 |
|
| 1245 |
Interlocked.Increment(ref count); |
| 1246 |
} |
| 1247 |
group.Add(element); |
| 1248 |
} |
| 1249 |
|
| 1250 |
tcs.TrySetResult(res); |
| 1251 |
}, |
| 1252 |
ex => |
| 1253 |
{ |
| 1254 |
foreach (var v in map.Values) |
| 1255 |
v.Error(ex); |
| 1256 |
|
| 1257 |
faulted = ex; |
| 1258 |
tcs.TrySetException(ex); |
| 1259 |
} |
| 1260 |
); |
| 1261 |
|
| 1262 |
lock (gate) |
| 1263 |
{ |
| 1264 |
task = null; |
| 1265 |
} |
| 1266 |
}); |
| 1267 |
|
| 1268 |
return tcs.Task.UsingEnumerator(e); |
| 1269 |
}; |
| 1270 |
|
| 1271 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 1272 |
f = (tcs, ct) => |
| 1273 |
{ |
| 1274 |
iterateSource(ct).ContinueWith(t => |
| 1275 |
{ |
| 1276 |
t.Handle(tcs, |
| 1277 |
res => |
| 1278 |
{ |
| 1279 |
current = null; |
| 1280 |
lock (list) |
| 1281 |
{ |
| 1282 |
if (index < list.Count) |
| 1283 |
current = list[index++]; |
| 1284 |
} |
| 1285 |
|
| 1286 |
if (current != null) |
| 1287 |
{ |
| 1288 |
tcs.TrySetResult(true); |
| 1289 |
} |
| 1290 |
else |
| 1291 |
{ |
| 1292 |
if (res) |
| 1293 |
f(tcs, ct); |
| 1294 |
else |
| 1295 |
tcs.TrySetResult(false); |
| 1296 |
} |
| 1297 |
} |
| 1298 |
); |
| 1299 |
}); |
| 1300 |
}; |
| 1301 |
|
| 1302 |
return Create( |
| 1303 |
(ct, tcs) => |
| 1304 |
{ |
| 1305 |
f(tcs, cts.Token); |
| 1306 |
return tcs.Task; |
| 1307 |
}, |
| 1308 |
() => current, |
| 1309 |
d.Dispose |
| 1310 |
); |
| 1311 |
}); |
| 1312 |
} |
| 1313 |
|
| 1314 |
public static IAsyncEnumerable<IAsyncGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector) |
| 1315 |
{ |
| 1316 |
if (source == null) |
| 1317 |
throw new ArgumentNullException("source"); |
| 1318 |
if (keySelector == null) |
| 1319 |
throw new ArgumentNullException("keySelector"); |
| 1320 |
if (elementSelector == null) |
| 1321 |
throw new ArgumentNullException("elementSelector"); |
| 1322 |
|
| 1323 |
return source.GroupBy(keySelector, elementSelector, EqualityComparer<TKey>.Default); |
| 1324 |
} |
| 1325 |
|
| 1326 |
public static IAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) |
| 1327 |
{ |
| 1328 |
if (source == null) |
| 1329 |
throw new ArgumentNullException("source"); |
| 1330 |
if (keySelector == null) |
| 1331 |
throw new ArgumentNullException("keySelector"); |
| 1332 |
if (comparer == null) |
| 1333 |
throw new ArgumentNullException("comparer"); |
| 1334 |
|
| 1335 |
return source.GroupBy(keySelector, x => x, comparer); |
| 1336 |
} |
| 1337 |
|
| 1338 |
public static IAsyncEnumerable<IAsyncGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) |
| 1339 |
{ |
| 1340 |
if (source == null) |
| 1341 |
throw new ArgumentNullException("source"); |
| 1342 |
if (keySelector == null) |
| 1343 |
throw new ArgumentNullException("keySelector"); |
| 1344 |
|
| 1345 |
return source.GroupBy(keySelector, x => x, EqualityComparer<TKey>.Default); |
| 1346 |
} |
| 1347 |
|
| 1348 |
public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, TResult> resultSelector, IEqualityComparer<TKey> comparer) |
| 1349 |
{ |
| 1350 |
if (source == null) |
| 1351 |
throw new ArgumentNullException("source"); |
| 1352 |
if (keySelector == null) |
| 1353 |
throw new ArgumentNullException("keySelector"); |
| 1354 |
if (elementSelector == null) |
| 1355 |
throw new ArgumentNullException("elementSelector"); |
| 1356 |
if (resultSelector == null) |
| 1357 |
throw new ArgumentNullException("resultSelector"); |
| 1358 |
if (comparer == null) |
| 1359 |
throw new ArgumentNullException("comparer"); |
| 1360 |
|
| 1361 |
return source.GroupBy(keySelector, elementSelector, comparer).Select(g => resultSelector(g.Key, g)); |
| 1362 |
} |
| 1363 |
|
| 1364 |
public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, Func<TKey, IAsyncEnumerable<TElement>, TResult> resultSelector) |
| 1365 |
{ |
| 1366 |
if (source == null) |
| 1367 |
throw new ArgumentNullException("source"); |
| 1368 |
if (keySelector == null) |
| 1369 |
throw new ArgumentNullException("keySelector"); |
| 1370 |
if (elementSelector == null) |
| 1371 |
throw new ArgumentNullException("elementSelector"); |
| 1372 |
if (resultSelector == null) |
| 1373 |
throw new ArgumentNullException("resultSelector"); |
| 1374 |
|
| 1375 |
return source.GroupBy(keySelector, elementSelector, EqualityComparer<TKey>.Default).Select(g => resultSelector(g.Key, g)); |
| 1376 |
} |
| 1377 |
|
| 1378 |
public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IAsyncEnumerable<TSource>, TResult> resultSelector, IEqualityComparer<TKey> comparer) |
| 1379 |
{ |
| 1380 |
if (source == null) |
| 1381 |
throw new ArgumentNullException("source"); |
| 1382 |
if (keySelector == null) |
| 1383 |
throw new ArgumentNullException("keySelector"); |
| 1384 |
if (resultSelector == null) |
| 1385 |
throw new ArgumentNullException("resultSelector"); |
| 1386 |
if (comparer == null) |
| 1387 |
throw new ArgumentNullException("comparer"); |
| 1388 |
|
| 1389 |
return source.GroupBy(keySelector, x => x, comparer).Select(g => resultSelector(g.Key, g)); |
| 1390 |
} |
| 1391 |
|
| 1392 |
public static IAsyncEnumerable<TResult> GroupBy<TSource, TKey, TResult>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, IAsyncEnumerable<TSource>, TResult> resultSelector) |
| 1393 |
{ |
| 1394 |
if (source == null) |
| 1395 |
throw new ArgumentNullException("source"); |
| 1396 |
if (keySelector == null) |
| 1397 |
throw new ArgumentNullException("keySelector"); |
| 1398 |
if (resultSelector == null) |
| 1399 |
throw new ArgumentNullException("resultSelector"); |
| 1400 |
|
| 1401 |
return source.GroupBy(keySelector, x => x, EqualityComparer<TKey>.Default).Select(g => resultSelector(g.Key, g)); |
| 1402 |
} |
| 1403 |
|
| 1404 |
class Grouping<TKey, TElement> : IAsyncGrouping<TKey, TElement> |
| 1405 |
{ |
| 1406 |
private readonly Func<CancellationToken, Task<bool>> iterateSource; |
| 1407 |
private readonly IDisposable sourceDisposable; |
| 1408 |
private readonly List<TElement> elements = new List<TElement>(); |
| 1409 |
private bool done = false; |
| 1410 |
private Exception exception = null; |
| 1411 |
|
| 1412 |
public Grouping(TKey key, Func<CancellationToken, Task<bool>> iterateSource, IDisposable sourceDisposable) |
| 1413 |
{ |
| 1414 |
this.iterateSource = iterateSource; |
| 1415 |
this.sourceDisposable = sourceDisposable; |
| 1416 |
Key = key; |
| 1417 |
} |
| 1418 |
|
| 1419 |
public TKey Key |
| 1420 |
{ |
| 1421 |
get; |
| 1422 |
private set; |
| 1423 |
} |
| 1424 |
|
| 1425 |
public void Add(TElement element) |
| 1426 |
{ |
| 1427 |
lock (elements) |
| 1428 |
elements.Add(element); |
| 1429 |
} |
| 1430 |
|
| 1431 |
public void Error(Exception exception) |
| 1432 |
{ |
| 1433 |
done = true; |
| 1434 |
this.exception = exception; |
| 1435 |
} |
| 1436 |
|
| 1437 |
public IAsyncEnumerator<TElement> GetEnumerator() |
| 1438 |
{ |
| 1439 |
var index = -1; |
| 1440 |
|
| 1441 |
var cts = new CancellationTokenDisposable(); |
| 1442 |
var d = new CompositeDisposable(cts, sourceDisposable); |
| 1443 |
|
| 1444 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 1445 |
f = (tcs, ct) => |
| 1446 |
{ |
| 1447 |
var size = 0; |
| 1448 |
lock (elements) |
| 1449 |
size = elements.Count; |
| 1450 |
|
| 1451 |
if (index < size) |
| 1452 |
{ |
| 1453 |
tcs.TrySetResult(true); |
| 1454 |
} |
| 1455 |
else if (done) |
| 1456 |
{ |
| 1457 |
if (exception != null) |
| 1458 |
tcs.TrySetException(exception); |
| 1459 |
else |
| 1460 |
tcs.TrySetResult(false); |
| 1461 |
} |
| 1462 |
else |
| 1463 |
{ |
| 1464 |
iterateSource(ct).ContinueWith(t => |
| 1465 |
{ |
| 1466 |
t.Handle(tcs, res => |
| 1467 |
{ |
| 1468 |
if (res) |
| 1469 |
f(tcs, ct); |
| 1470 |
else |
| 1471 |
tcs.TrySetResult(false); |
| 1472 |
}); |
| 1473 |
}); |
| 1474 |
} |
| 1475 |
}; |
| 1476 |
|
| 1477 |
return Create( |
| 1478 |
(ct, tcs) => |
| 1479 |
{ |
| 1480 |
++index; |
| 1481 |
f(tcs, cts.Token); |
| 1482 |
return tcs.Task; |
| 1483 |
}, |
| 1484 |
() => elements[index], |
| 1485 |
d.Dispose |
| 1486 |
); |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
#region Ix |
| 1491 |
|
| 1492 |
public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext) |
| 1493 |
{ |
| 1494 |
if (source == null) |
| 1495 |
throw new ArgumentNullException("source"); |
| 1496 |
if (onNext == null) |
| 1497 |
throw new ArgumentNullException("onNext"); |
| 1498 |
|
| 1499 |
return DoHelper(source, onNext, _ => { }, () => { }); |
| 1500 |
} |
| 1501 |
|
| 1502 |
public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted) |
| 1503 |
{ |
| 1504 |
if (source == null) |
| 1505 |
throw new ArgumentNullException("source"); |
| 1506 |
if (onNext == null) |
| 1507 |
throw new ArgumentNullException("onNext"); |
| 1508 |
if (onCompleted == null) |
| 1509 |
throw new ArgumentNullException("onCompleted"); |
| 1510 |
|
| 1511 |
return DoHelper(source, onNext, _ => { }, onCompleted); |
| 1512 |
} |
| 1513 |
|
| 1514 |
public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError) |
| 1515 |
{ |
| 1516 |
if (source == null) |
| 1517 |
throw new ArgumentNullException("source"); |
| 1518 |
if (onNext == null) |
| 1519 |
throw new ArgumentNullException("onNext"); |
| 1520 |
if (onError == null) |
| 1521 |
throw new ArgumentNullException("onError"); |
| 1522 |
|
| 1523 |
return DoHelper(source, onNext, onError, () => { }); |
| 1524 |
} |
| 1525 |
|
| 1526 |
public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted) |
| 1527 |
{ |
| 1528 |
if (source == null) |
| 1529 |
throw new ArgumentNullException("source"); |
| 1530 |
if (onNext == null) |
| 1531 |
throw new ArgumentNullException("onNext"); |
| 1532 |
if (onError == null) |
| 1533 |
throw new ArgumentNullException("onError"); |
| 1534 |
if (onCompleted == null) |
| 1535 |
throw new ArgumentNullException("onCompleted"); |
| 1536 |
|
| 1537 |
return DoHelper(source, onNext, onError, onCompleted); |
| 1538 |
} |
| 1539 |
|
| 1540 |
#if !NO_RXINTERFACES |
| 1541 |
public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, IObserver<TSource> observer) |
| 1542 |
{ |
| 1543 |
if (source == null) |
| 1544 |
throw new ArgumentNullException("source"); |
| 1545 |
if (observer == null) |
| 1546 |
throw new ArgumentNullException("observer"); |
| 1547 |
|
| 1548 |
return DoHelper(source, observer.OnNext, observer.OnError, observer.OnCompleted); |
| 1549 |
} |
| 1550 |
#endif |
| 1551 |
|
| 1552 |
private static IAsyncEnumerable<TSource> DoHelper<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted) |
| 1553 |
{ |
| 1554 |
return Create(() => |
| 1555 |
{ |
| 1556 |
var e = source.GetEnumerator(); |
| 1557 |
|
| 1558 |
var cts = new CancellationTokenDisposable(); |
| 1559 |
var d = new CompositeDisposable(cts, e); |
| 1560 |
|
| 1561 |
var current = default(TSource); |
| 1562 |
|
| 1563 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 1564 |
f = (tcs, ct) => |
| 1565 |
{ |
| 1566 |
e.MoveNext(ct).ContinueWith(t => |
| 1567 |
{ |
| 1568 |
if (!t.IsCanceled) |
| 1569 |
{ |
| 1570 |
try |
| 1571 |
{ |
| 1572 |
if (t.IsFaulted) |
| 1573 |
{ |
| 1574 |
onError(t.Exception); |
| 1575 |
} |
| 1576 |
else if (!t.Result) |
| 1577 |
{ |
| 1578 |
onCompleted(); |
| 1579 |
} |
| 1580 |
else |
| 1581 |
{ |
| 1582 |
current = e.Current; |
| 1583 |
onNext(current); |
| 1584 |
} |
| 1585 |
} |
| 1586 |
catch (Exception ex) |
| 1587 |
{ |
| 1588 |
tcs.TrySetException(ex); |
| 1589 |
return; |
| 1590 |
} |
| 1591 |
} |
| 1592 |
|
| 1593 |
t.Handle(tcs, res => |
| 1594 |
{ |
| 1595 |
tcs.TrySetResult(res); |
| 1596 |
}); |
| 1597 |
}); |
| 1598 |
}; |
| 1599 |
|
| 1600 |
return Create( |
| 1601 |
(ct, tcs) => |
| 1602 |
{ |
| 1603 |
f(tcs, cts.Token); |
| 1604 |
return tcs.Task.UsingEnumerator(e); |
| 1605 |
}, |
| 1606 |
() => current, |
| 1607 |
d.Dispose |
| 1608 |
); |
| 1609 |
}); |
| 1610 |
} |
| 1611 |
|
| 1612 |
public static void ForEach<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken) |
| 1613 |
{ |
| 1614 |
if (source == null) |
| 1615 |
throw new ArgumentNullException("source"); |
| 1616 |
if (action == null) |
| 1617 |
throw new ArgumentNullException("action"); |
| 1618 |
|
| 1619 |
source.ForEachAsync(action, cancellationToken).Wait(cancellationToken); |
| 1620 |
} |
| 1621 |
|
| 1622 |
public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> action, CancellationToken cancellationToken) |
| 1623 |
{ |
| 1624 |
if (source == null) |
| 1625 |
throw new ArgumentNullException("source"); |
| 1626 |
if (action == null) |
| 1627 |
throw new ArgumentNullException("action"); |
| 1628 |
|
| 1629 |
return source.ForEachAsync((x, i) => action(x), cancellationToken); |
| 1630 |
} |
| 1631 |
|
| 1632 |
public static void ForEach<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken) |
| 1633 |
{ |
| 1634 |
if (source == null) |
| 1635 |
throw new ArgumentNullException("source"); |
| 1636 |
if (action == null) |
| 1637 |
throw new ArgumentNullException("action"); |
| 1638 |
|
| 1639 |
source.ForEachAsync(action, cancellationToken).Wait(cancellationToken); |
| 1640 |
} |
| 1641 |
|
| 1642 |
public static Task ForEachAsync<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource, int> action, CancellationToken cancellationToken) |
| 1643 |
{ |
| 1644 |
if (source == null) |
| 1645 |
throw new ArgumentNullException("source"); |
| 1646 |
if (action == null) |
| 1647 |
throw new ArgumentNullException("action"); |
| 1648 |
|
| 1649 |
var tcs = new TaskCompletionSource<bool>(); |
| 1650 |
|
| 1651 |
var e = source.GetEnumerator(); |
| 1652 |
|
| 1653 |
var i = 0; |
| 1654 |
|
| 1655 |
var f = default(Action<CancellationToken>); |
| 1656 |
f = ct => |
| 1657 |
{ |
| 1658 |
e.MoveNext(ct).ContinueWith(t => |
| 1659 |
{ |
| 1660 |
t.Handle(tcs, res => |
| 1661 |
{ |
| 1662 |
if (res) |
| 1663 |
{ |
| 1664 |
try |
| 1665 |
{ |
| 1666 |
action(e.Current, i++); |
| 1667 |
} |
| 1668 |
catch (Exception ex) |
| 1669 |
{ |
| 1670 |
tcs.TrySetException(ex); |
| 1671 |
return; |
| 1672 |
} |
| 1673 |
|
| 1674 |
f(ct); |
| 1675 |
} |
| 1676 |
else |
| 1677 |
tcs.TrySetResult(true); |
| 1678 |
}); |
| 1679 |
}); |
| 1680 |
}; |
| 1681 |
|
| 1682 |
f(cancellationToken); |
| 1683 |
|
| 1684 |
return tcs.Task.UsingEnumerator(e); |
| 1685 |
} |
| 1686 |
|
| 1687 |
public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source, int count) |
| 1688 |
{ |
| 1689 |
if (source == null) |
| 1690 |
throw new ArgumentNullException("source"); |
| 1691 |
if (count < 0) |
| 1692 |
throw new ArgumentOutOfRangeException("count"); |
| 1693 |
|
| 1694 |
return Create(() => |
| 1695 |
{ |
| 1696 |
var e = default(IAsyncEnumerator<TSource>); |
| 1697 |
var a = new AssignableDisposable(); |
| 1698 |
var n = count; |
| 1699 |
var current = default(TSource); |
| 1700 |
|
| 1701 |
var cts = new CancellationTokenDisposable(); |
| 1702 |
var d = new CompositeDisposable(cts, a); |
| 1703 |
|
| 1704 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 1705 |
f = (tcs, ct) => |
| 1706 |
{ |
| 1707 |
if (e == null) |
| 1708 |
{ |
| 1709 |
if (n-- == 0) |
| 1710 |
{ |
| 1711 |
tcs.TrySetResult(false); |
| 1712 |
return; |
| 1713 |
} |
| 1714 |
|
| 1715 |
try |
| 1716 |
{ |
| 1717 |
e = source.GetEnumerator(); |
| 1718 |
} |
| 1719 |
catch (Exception ex) |
| 1720 |
{ |
| 1721 |
tcs.TrySetException(ex); |
| 1722 |
return; |
| 1723 |
} |
| 1724 |
|
| 1725 |
a.Disposable = e; |
| 1726 |
} |
| 1727 |
|
| 1728 |
e.MoveNext(ct).ContinueWith(t => |
| 1729 |
{ |
| 1730 |
t.Handle(tcs, res => |
| 1731 |
{ |
| 1732 |
if (res) |
| 1733 |
{ |
| 1734 |
current = e.Current; |
| 1735 |
tcs.TrySetResult(true); |
| 1736 |
} |
| 1737 |
else |
| 1738 |
{ |
| 1739 |
e = null; |
| 1740 |
f(tcs, ct); |
| 1741 |
} |
| 1742 |
}); |
| 1743 |
}); |
| 1744 |
}; |
| 1745 |
|
| 1746 |
return Create( |
| 1747 |
(ct, tcs) => |
| 1748 |
{ |
| 1749 |
f(tcs, cts.Token); |
| 1750 |
return tcs.Task.UsingEnumerator(d); |
| 1751 |
}, |
| 1752 |
() => current, |
| 1753 |
d.Dispose |
| 1754 |
); |
| 1755 |
}); |
| 1756 |
} |
| 1757 |
|
| 1758 |
public static IAsyncEnumerable<TSource> Repeat<TSource>(this IAsyncEnumerable<TSource> source) |
| 1759 |
{ |
| 1760 |
if (source == null) |
| 1761 |
throw new ArgumentNullException("source"); |
| 1762 |
|
| 1763 |
return Create(() => |
| 1764 |
{ |
| 1765 |
var e = default(IAsyncEnumerator<TSource>); |
| 1766 |
var a = new AssignableDisposable(); |
| 1767 |
var current = default(TSource); |
| 1768 |
|
| 1769 |
var cts = new CancellationTokenDisposable(); |
| 1770 |
var d = new CompositeDisposable(cts, a); |
| 1771 |
|
| 1772 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 1773 |
f = (tcs, ct) => |
| 1774 |
{ |
| 1775 |
if (e == null) |
| 1776 |
{ |
| 1777 |
try |
| 1778 |
{ |
| 1779 |
e = source.GetEnumerator(); |
| 1780 |
} |
| 1781 |
catch (Exception ex) |
| 1782 |
{ |
| 1783 |
tcs.TrySetException(ex); |
| 1784 |
return; |
| 1785 |
} |
| 1786 |
|
| 1787 |
a.Disposable = e; |
| 1788 |
} |
| 1789 |
|
| 1790 |
e.MoveNext(ct).ContinueWith(t => |
| 1791 |
{ |
| 1792 |
t.Handle(tcs, res => |
| 1793 |
{ |
| 1794 |
if (res) |
| 1795 |
{ |
| 1796 |
current = e.Current; |
| 1797 |
tcs.TrySetResult(true); |
| 1798 |
} |
| 1799 |
else |
| 1800 |
{ |
| 1801 |
e = null; |
| 1802 |
f(tcs, ct); |
| 1803 |
} |
| 1804 |
}); |
| 1805 |
}); |
| 1806 |
}; |
| 1807 |
|
| 1808 |
return Create( |
| 1809 |
(ct, tcs) => |
| 1810 |
{ |
| 1811 |
f(tcs, cts.Token); |
| 1812 |
return tcs.Task.UsingEnumerator(d); |
| 1813 |
}, |
| 1814 |
() => current, |
| 1815 |
d.Dispose |
| 1816 |
); |
| 1817 |
}); |
| 1818 |
} |
| 1819 |
|
| 1820 |
public static IAsyncEnumerable<TSource> IgnoreElements<TSource>(this IAsyncEnumerable<TSource> source) |
| 1821 |
{ |
| 1822 |
if (source == null) |
| 1823 |
throw new ArgumentNullException("source"); |
| 1824 |
|
| 1825 |
return Create(() => |
| 1826 |
{ |
| 1827 |
var e = source.GetEnumerator(); |
| 1828 |
|
| 1829 |
var cts = new CancellationTokenDisposable(); |
| 1830 |
var d = new CompositeDisposable(cts, e); |
| 1831 |
|
| 1832 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 1833 |
f = (tcs, ct) => |
| 1834 |
{ |
| 1835 |
e.MoveNext(ct).ContinueWith(t => |
| 1836 |
{ |
| 1837 |
t.Handle(tcs, res => |
| 1838 |
{ |
| 1839 |
if (!res) |
| 1840 |
{ |
| 1841 |
tcs.TrySetResult(false); |
| 1842 |
return; |
| 1843 |
} |
| 1844 |
|
| 1845 |
f(tcs, ct); |
| 1846 |
}); |
| 1847 |
}); |
| 1848 |
}; |
| 1849 |
|
| 1850 |
return Create<TSource>( |
| 1851 |
(ct, tcs) => |
| 1852 |
{ |
| 1853 |
f(tcs, cts.Token); |
| 1854 |
return tcs.Task.UsingEnumerator(e); |
| 1855 |
}, |
| 1856 |
() => { throw new InvalidOperationException(); }, |
| 1857 |
d.Dispose |
| 1858 |
); |
| 1859 |
}); |
| 1860 |
} |
| 1861 |
|
| 1862 |
public static IAsyncEnumerable<TSource> StartWith<TSource>(this IAsyncEnumerable<TSource> source, params TSource[] values) |
| 1863 |
{ |
| 1864 |
if (source == null) |
| 1865 |
throw new ArgumentNullException("source"); |
| 1866 |
|
| 1867 |
return values.ToAsyncEnumerable().Concat(source); |
| 1868 |
} |
| 1869 |
|
| 1870 |
public static IAsyncEnumerable<IList<TSource>> Buffer<TSource>(this IAsyncEnumerable<TSource> source, int count) |
| 1871 |
{ |
| 1872 |
if (source == null) |
| 1873 |
throw new ArgumentNullException("source"); |
| 1874 |
if (count <= 0) |
| 1875 |
throw new ArgumentOutOfRangeException("count"); |
| 1876 |
|
| 1877 |
return source.Buffer_(count, count); |
| 1878 |
} |
| 1879 |
|
| 1880 |
public static IAsyncEnumerable<IList<TSource>> Buffer<TSource>(this IAsyncEnumerable<TSource> source, int count, int skip) |
| 1881 |
{ |
| 1882 |
if (source == null) |
| 1883 |
throw new ArgumentNullException("source"); |
| 1884 |
if (count <= 0) |
| 1885 |
throw new ArgumentOutOfRangeException("count"); |
| 1886 |
if (skip <= 0) |
| 1887 |
throw new ArgumentOutOfRangeException("skip"); |
| 1888 |
|
| 1889 |
return source.Buffer_(count, skip); |
| 1890 |
} |
| 1891 |
|
| 1892 |
private static IAsyncEnumerable<IList<TSource>> Buffer_<TSource>(this IAsyncEnumerable<TSource> source, int count, int skip) |
| 1893 |
{ |
| 1894 |
return Create(() => |
| 1895 |
{ |
| 1896 |
var e = source.GetEnumerator(); |
| 1897 |
|
| 1898 |
var cts = new CancellationTokenDisposable(); |
| 1899 |
var d = new CompositeDisposable(cts, e); |
| 1900 |
|
| 1901 |
var buffers = new Queue<IList<TSource>>(); |
| 1902 |
|
| 1903 |
var i = 0; |
| 1904 |
|
| 1905 |
var current = default(IList<TSource>); |
| 1906 |
var stopped = false; |
| 1907 |
|
| 1908 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 1909 |
f = (tcs, ct) => |
| 1910 |
{ |
| 1911 |
if (!stopped) |
| 1912 |
{ |
| 1913 |
e.MoveNext(ct).ContinueWith(t => |
| 1914 |
{ |
| 1915 |
t.Handle(tcs, res => |
| 1916 |
{ |
| 1917 |
if (res) |
| 1918 |
{ |
| 1919 |
var item = e.Current; |
| 1920 |
|
| 1921 |
if (i++ % skip == 0) |
| 1922 |
buffers.Enqueue(new List<TSource>(count)); |
| 1923 |
|
| 1924 |
foreach (var buffer in buffers) |
| 1925 |
buffer.Add(item); |
| 1926 |
|
| 1927 |
if (buffers.Count > 0 && buffers.Peek().Count == count) |
| 1928 |
{ |
| 1929 |
current = buffers.Dequeue(); |
| 1930 |
tcs.TrySetResult(true); |
| 1931 |
return; |
| 1932 |
} |
| 1933 |
|
| 1934 |
f(tcs, ct); |
| 1935 |
} |
| 1936 |
else |
| 1937 |
{ |
| 1938 |
stopped = true; |
| 1939 |
e.Dispose(); |
| 1940 |
|
| 1941 |
f(tcs, ct); |
| 1942 |
} |
| 1943 |
}); |
| 1944 |
}); |
| 1945 |
} |
| 1946 |
else |
| 1947 |
{ |
| 1948 |
if (buffers.Count > 0) |
| 1949 |
{ |
| 1950 |
current = buffers.Dequeue(); |
| 1951 |
tcs.TrySetResult(true); |
| 1952 |
} |
| 1953 |
else |
| 1954 |
{ |
| 1955 |
tcs.TrySetResult(false); |
| 1956 |
} |
| 1957 |
} |
| 1958 |
}; |
| 1959 |
|
| 1960 |
return Create( |
| 1961 |
(ct, tcs) => |
| 1962 |
{ |
| 1963 |
f(tcs, cts.Token); |
| 1964 |
return tcs.Task.UsingEnumerator(e); |
| 1965 |
}, |
| 1966 |
() => current, |
| 1967 |
d.Dispose |
| 1968 |
); |
| 1969 |
}); |
| 1970 |
} |
| 1971 |
|
| 1972 |
public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) |
| 1973 |
{ |
| 1974 |
if (source == null) |
| 1975 |
throw new ArgumentNullException("source"); |
| 1976 |
if (keySelector == null) |
| 1977 |
throw new ArgumentNullException("keySelector"); |
| 1978 |
if (comparer == null) |
| 1979 |
throw new ArgumentNullException("comparer"); |
| 1980 |
|
| 1981 |
return Defer(() => |
| 1982 |
{ |
| 1983 |
var set = new HashSet<TKey>(comparer); |
| 1984 |
return source.Where(item => set.Add(keySelector(item))); |
| 1985 |
}); |
| 1986 |
} |
| 1987 |
|
| 1988 |
public static IAsyncEnumerable<TSource> Distinct<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) |
| 1989 |
{ |
| 1990 |
if (source == null) |
| 1991 |
throw new ArgumentNullException("source"); |
| 1992 |
if (keySelector == null) |
| 1993 |
throw new ArgumentNullException("keySelector"); |
| 1994 |
|
| 1995 |
return source.Distinct(keySelector, EqualityComparer<TKey>.Default); |
| 1996 |
} |
| 1997 |
|
| 1998 |
public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source) |
| 1999 |
{ |
| 2000 |
if (source == null) |
| 2001 |
throw new ArgumentNullException("source"); |
| 2002 |
|
| 2003 |
return source.DistinctUntilChanged_(x => x, EqualityComparer<TSource>.Default); |
| 2004 |
} |
| 2005 |
|
| 2006 |
public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource>(this IAsyncEnumerable<TSource> source, IEqualityComparer<TSource> comparer) |
| 2007 |
{ |
| 2008 |
if (source == null) |
| 2009 |
throw new ArgumentNullException("source"); |
| 2010 |
if (comparer == null) |
| 2011 |
throw new ArgumentNullException("comparer"); |
| 2012 |
|
| 2013 |
return source.DistinctUntilChanged_(x => x, comparer); |
| 2014 |
} |
| 2015 |
|
| 2016 |
public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector) |
| 2017 |
{ |
| 2018 |
if (source == null) |
| 2019 |
throw new ArgumentNullException("source"); |
| 2020 |
if (keySelector == null) |
| 2021 |
throw new ArgumentNullException("keySelector"); |
| 2022 |
|
| 2023 |
return source.DistinctUntilChanged_(keySelector, EqualityComparer<TKey>.Default); |
| 2024 |
} |
| 2025 |
|
| 2026 |
public static IAsyncEnumerable<TSource> DistinctUntilChanged<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) |
| 2027 |
{ |
| 2028 |
if (source == null) |
| 2029 |
throw new ArgumentNullException("source"); |
| 2030 |
if (keySelector == null) |
| 2031 |
throw new ArgumentNullException("keySelector"); |
| 2032 |
if (comparer == null) |
| 2033 |
throw new ArgumentNullException("comparer"); |
| 2034 |
|
| 2035 |
return source.DistinctUntilChanged_(keySelector, comparer); |
| 2036 |
} |
| 2037 |
|
| 2038 |
private static IAsyncEnumerable<TSource> DistinctUntilChanged_<TSource, TKey>(this IAsyncEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) |
| 2039 |
{ |
| 2040 |
return Create(() => |
| 2041 |
{ |
| 2042 |
var e = source.GetEnumerator(); |
| 2043 |
|
| 2044 |
var cts = new CancellationTokenDisposable(); |
| 2045 |
var d = new CompositeDisposable(cts, e); |
| 2046 |
|
| 2047 |
var currentKey = default(TKey); |
| 2048 |
var hasCurrentKey = false; |
| 2049 |
var current = default(TSource); |
| 2050 |
|
| 2051 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 2052 |
f = (tcs, ct) => |
| 2053 |
{ |
| 2054 |
e.MoveNext(ct).ContinueWith(t => |
| 2055 |
{ |
| 2056 |
t.Handle(tcs, res => |
| 2057 |
{ |
| 2058 |
if (res) |
| 2059 |
{ |
| 2060 |
var item = e.Current; |
| 2061 |
var key = default(TKey); |
| 2062 |
var comparerEquals = false; |
| 2063 |
|
| 2064 |
try |
| 2065 |
{ |
| 2066 |
key = keySelector(item); |
| 2067 |
|
| 2068 |
if (hasCurrentKey) |
| 2069 |
{ |
| 2070 |
comparerEquals = comparer.Equals(currentKey, key); |
| 2071 |
} |
| 2072 |
} |
| 2073 |
catch (Exception ex) |
| 2074 |
{ |
| 2075 |
tcs.TrySetException(ex); |
| 2076 |
return; |
| 2077 |
} |
| 2078 |
|
| 2079 |
if (!hasCurrentKey || !comparerEquals) |
| 2080 |
{ |
| 2081 |
hasCurrentKey = true; |
| 2082 |
currentKey = key; |
| 2083 |
|
| 2084 |
current = item; |
| 2085 |
tcs.TrySetResult(true); |
| 2086 |
} |
| 2087 |
else |
| 2088 |
{ |
| 2089 |
f(tcs, ct); |
| 2090 |
} |
| 2091 |
} |
| 2092 |
else |
| 2093 |
{ |
| 2094 |
tcs.TrySetResult(false); |
| 2095 |
} |
| 2096 |
}); |
| 2097 |
}); |
| 2098 |
}; |
| 2099 |
|
| 2100 |
return Create( |
| 2101 |
(ct, tcs) => |
| 2102 |
{ |
| 2103 |
f(tcs, cts.Token); |
| 2104 |
return tcs.Task.UsingEnumerator(e); |
| 2105 |
}, |
| 2106 |
() => current, |
| 2107 |
d.Dispose |
| 2108 |
); |
| 2109 |
}); |
| 2110 |
} |
| 2111 |
|
| 2112 |
public static IAsyncEnumerable<TSource> Expand<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, IAsyncEnumerable<TSource>> selector) |
| 2113 |
{ |
| 2114 |
if (source == null) |
| 2115 |
throw new ArgumentNullException("source"); |
| 2116 |
if (selector == null) |
| 2117 |
throw new ArgumentNullException("selector"); |
| 2118 |
|
| 2119 |
return Create(() => |
| 2120 |
{ |
| 2121 |
var e = default(IAsyncEnumerator<TSource>); |
| 2122 |
|
| 2123 |
var cts = new CancellationTokenDisposable(); |
| 2124 |
var a = new AssignableDisposable(); |
| 2125 |
var d = new CompositeDisposable(cts, a); |
| 2126 |
|
| 2127 |
var queue = new Queue<IAsyncEnumerable<TSource>>(); |
| 2128 |
queue.Enqueue(source); |
| 2129 |
|
| 2130 |
var current = default(TSource); |
| 2131 |
|
| 2132 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 2133 |
f = (tcs, ct) => |
| 2134 |
{ |
| 2135 |
if (e == null) |
| 2136 |
{ |
| 2137 |
if (queue.Count > 0) |
| 2138 |
{ |
| 2139 |
var src = queue.Dequeue(); |
| 2140 |
|
| 2141 |
try |
| 2142 |
{ |
| 2143 |
e = src.GetEnumerator(); |
| 2144 |
} |
| 2145 |
catch (Exception ex) |
| 2146 |
{ |
| 2147 |
tcs.TrySetException(ex); |
| 2148 |
return; |
| 2149 |
} |
| 2150 |
|
| 2151 |
a.Disposable = e; |
| 2152 |
f(tcs, ct); |
| 2153 |
} |
| 2154 |
else |
| 2155 |
{ |
| 2156 |
tcs.TrySetResult(false); |
| 2157 |
} |
| 2158 |
} |
| 2159 |
else |
| 2160 |
{ |
| 2161 |
e.MoveNext(ct).ContinueWith(t => |
| 2162 |
{ |
| 2163 |
t.Handle(tcs, res => |
| 2164 |
{ |
| 2165 |
if (res) |
| 2166 |
{ |
| 2167 |
var item = e.Current; |
| 2168 |
|
| 2169 |
var next = default(IAsyncEnumerable<TSource>); |
| 2170 |
try |
| 2171 |
{ |
| 2172 |
next = selector(item); |
| 2173 |
} |
| 2174 |
catch (Exception ex) |
| 2175 |
{ |
| 2176 |
tcs.TrySetException(ex); |
| 2177 |
return; |
| 2178 |
} |
| 2179 |
|
| 2180 |
queue.Enqueue(next); |
| 2181 |
current = item; |
| 2182 |
tcs.TrySetResult(true); |
| 2183 |
} |
| 2184 |
else |
| 2185 |
{ |
| 2186 |
e = null; |
| 2187 |
f(tcs, ct); |
| 2188 |
} |
| 2189 |
}); |
| 2190 |
}); |
| 2191 |
} |
| 2192 |
}; |
| 2193 |
|
| 2194 |
return Create( |
| 2195 |
(ct, tcs) => |
| 2196 |
{ |
| 2197 |
f(tcs, cts.Token); |
| 2198 |
return tcs.Task.UsingEnumerator(a); |
| 2199 |
}, |
| 2200 |
() => current, |
| 2201 |
d.Dispose |
| 2202 |
); |
| 2203 |
}); |
| 2204 |
} |
| 2205 |
|
| 2206 |
public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator) |
| 2207 |
{ |
| 2208 |
if (source == null) |
| 2209 |
throw new ArgumentNullException("source"); |
| 2210 |
if (accumulator == null) |
| 2211 |
throw new ArgumentNullException("accumulator"); |
| 2212 |
|
| 2213 |
return Create(() => |
| 2214 |
{ |
| 2215 |
var e = source.GetEnumerator(); |
| 2216 |
|
| 2217 |
var cts = new CancellationTokenDisposable(); |
| 2218 |
var d = new CompositeDisposable(cts, e); |
| 2219 |
|
| 2220 |
var acc = seed; |
| 2221 |
var current = default(TAccumulate); |
| 2222 |
|
| 2223 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 2224 |
f = (tcs, ct) => |
| 2225 |
{ |
| 2226 |
e.MoveNext(ct).ContinueWith(t => |
| 2227 |
{ |
| 2228 |
t.Handle(tcs, res => |
| 2229 |
{ |
| 2230 |
if (!res) |
| 2231 |
{ |
| 2232 |
tcs.TrySetResult(false); |
| 2233 |
return; |
| 2234 |
} |
| 2235 |
|
| 2236 |
var item = e.Current; |
| 2237 |
try |
| 2238 |
{ |
| 2239 |
acc = accumulator(acc, item); |
| 2240 |
} |
| 2241 |
catch (Exception ex) |
| 2242 |
{ |
| 2243 |
tcs.TrySetException(ex); |
| 2244 |
return; |
| 2245 |
} |
| 2246 |
|
| 2247 |
current = acc; |
| 2248 |
tcs.TrySetResult(true); |
| 2249 |
}); |
| 2250 |
}); |
| 2251 |
}; |
| 2252 |
|
| 2253 |
return Create( |
| 2254 |
(ct, tcs) => |
| 2255 |
{ |
| 2256 |
f(tcs, cts.Token); |
| 2257 |
return tcs.Task.UsingEnumerator(e); |
| 2258 |
}, |
| 2259 |
() => current, |
| 2260 |
d.Dispose |
| 2261 |
); |
| 2262 |
}); |
| 2263 |
} |
| 2264 |
|
| 2265 |
public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator) |
| 2266 |
{ |
| 2267 |
if (source == null) |
| 2268 |
throw new ArgumentNullException("source"); |
| 2269 |
if (accumulator == null) |
| 2270 |
throw new ArgumentNullException("accumulator"); |
| 2271 |
|
| 2272 |
return Create(() => |
| 2273 |
{ |
| 2274 |
var e = source.GetEnumerator(); |
| 2275 |
|
| 2276 |
var cts = new CancellationTokenDisposable(); |
| 2277 |
var d = new CompositeDisposable(cts, e); |
| 2278 |
|
| 2279 |
var hasSeed = false; |
| 2280 |
var acc = default(TSource); |
| 2281 |
var current = default(TSource); |
| 2282 |
|
| 2283 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 2284 |
f = (tcs, ct) => |
| 2285 |
{ |
| 2286 |
e.MoveNext(ct).ContinueWith(t => |
| 2287 |
{ |
| 2288 |
t.Handle(tcs, res => |
| 2289 |
{ |
| 2290 |
if (!res) |
| 2291 |
{ |
| 2292 |
tcs.TrySetResult(false); |
| 2293 |
return; |
| 2294 |
} |
| 2295 |
|
| 2296 |
var item = e.Current; |
| 2297 |
|
| 2298 |
if (!hasSeed) |
| 2299 |
{ |
| 2300 |
hasSeed = true; |
| 2301 |
acc = item; |
| 2302 |
f(tcs, ct); |
| 2303 |
return; |
| 2304 |
} |
| 2305 |
|
| 2306 |
try |
| 2307 |
{ |
| 2308 |
acc = accumulator(acc, item); |
| 2309 |
} |
| 2310 |
catch (Exception ex) |
| 2311 |
{ |
| 2312 |
tcs.TrySetException(ex); |
| 2313 |
return; |
| 2314 |
} |
| 2315 |
|
| 2316 |
current = acc; |
| 2317 |
tcs.TrySetResult(true); |
| 2318 |
}); |
| 2319 |
}); |
| 2320 |
}; |
| 2321 |
|
| 2322 |
return Create( |
| 2323 |
(ct, tcs) => |
| 2324 |
{ |
| 2325 |
f(tcs, cts.Token); |
| 2326 |
return tcs.Task.UsingEnumerator(e); |
| 2327 |
}, |
| 2328 |
() => current, |
| 2329 |
d.Dispose |
| 2330 |
); |
| 2331 |
}); |
| 2332 |
} |
| 2333 |
|
| 2334 |
public static IAsyncEnumerable<TSource> TakeLast<TSource>(this IAsyncEnumerable<TSource> source, int count) |
| 2335 |
{ |
| 2336 |
if (source == null) |
| 2337 |
throw new ArgumentNullException("source"); |
| 2338 |
if (count < 0) |
| 2339 |
throw new ArgumentOutOfRangeException("count"); |
| 2340 |
|
| 2341 |
return Create(() => |
| 2342 |
{ |
| 2343 |
var e = source.GetEnumerator(); |
| 2344 |
|
| 2345 |
var cts = new CancellationTokenDisposable(); |
| 2346 |
var d = new CompositeDisposable(cts, e); |
| 2347 |
|
| 2348 |
var q = new Queue<TSource>(count); |
| 2349 |
var done = false; |
| 2350 |
var current = default(TSource); |
| 2351 |
|
| 2352 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 2353 |
f = (tcs, ct) => |
| 2354 |
{ |
| 2355 |
if (!done) |
| 2356 |
{ |
| 2357 |
e.MoveNext(ct).ContinueWith(t => |
| 2358 |
{ |
| 2359 |
t.Handle(tcs, res => |
| 2360 |
{ |
| 2361 |
if (res) |
| 2362 |
{ |
| 2363 |
var item = e.Current; |
| 2364 |
|
| 2365 |
if (q.Count >= count) |
| 2366 |
q.Dequeue(); |
| 2367 |
q.Enqueue(item); |
| 2368 |
} |
| 2369 |
else |
| 2370 |
{ |
| 2371 |
done = true; |
| 2372 |
e.Dispose(); |
| 2373 |
} |
| 2374 |
|
| 2375 |
f(tcs, ct); |
| 2376 |
}); |
| 2377 |
}); |
| 2378 |
} |
| 2379 |
else |
| 2380 |
{ |
| 2381 |
if (q.Count > 0) |
| 2382 |
{ |
| 2383 |
current = q.Dequeue(); |
| 2384 |
tcs.TrySetResult(true); |
| 2385 |
} |
| 2386 |
else |
| 2387 |
{ |
| 2388 |
tcs.TrySetResult(false); |
| 2389 |
} |
| 2390 |
} |
| 2391 |
}; |
| 2392 |
|
| 2393 |
return Create( |
| 2394 |
(ct, tcs) => |
| 2395 |
{ |
| 2396 |
f(tcs, cts.Token); |
| 2397 |
return tcs.Task.UsingEnumerator(e); |
| 2398 |
}, |
| 2399 |
() => current, |
| 2400 |
d.Dispose |
| 2401 |
); |
| 2402 |
}); |
| 2403 |
} |
| 2404 |
|
| 2405 |
public static IAsyncEnumerable<TSource> SkipLast<TSource>(this IAsyncEnumerable<TSource> source, int count) |
| 2406 |
{ |
| 2407 |
if (source == null) |
| 2408 |
throw new ArgumentNullException("source"); |
| 2409 |
if (count < 0) |
| 2410 |
throw new ArgumentOutOfRangeException("count"); |
| 2411 |
|
| 2412 |
return Create(() => |
| 2413 |
{ |
| 2414 |
var e = source.GetEnumerator(); |
| 2415 |
|
| 2416 |
var cts = new CancellationTokenDisposable(); |
| 2417 |
var d = new CompositeDisposable(cts, e); |
| 2418 |
|
| 2419 |
var q = new Queue<TSource>(); |
| 2420 |
var current = default(TSource); |
| 2421 |
|
| 2422 |
var f = default(Action<TaskCompletionSource<bool>, CancellationToken>); |
| 2423 |
f = (tcs, ct) => |
| 2424 |
{ |
| 2425 |
e.MoveNext(ct).ContinueWith(t => |
| 2426 |
{ |
| 2427 |
t.Handle(tcs, res => |
| 2428 |
{ |
| 2429 |
if (res) |
| 2430 |
{ |
| 2431 |
var item = e.Current; |
| 2432 |
|
| 2433 |
q.Enqueue(item); |
| 2434 |
if (q.Count > count) |
| 2435 |
{ |
| 2436 |
current = q.Dequeue(); |
| 2437 |
tcs.TrySetResult(true); |
| 2438 |
} |
| 2439 |
else |
| 2440 |
{ |
| 2441 |
f(tcs, ct); |
| 2442 |
} |
| 2443 |
} |
| 2444 |
else |
| 2445 |
{ |
| 2446 |
tcs.TrySetResult(false); |
| 2447 |
} |
| 2448 |
}); |
| 2449 |
}); |
| 2450 |
}; |
| 2451 |
|
| 2452 |
return Create( |
| 2453 |
(ct, tcs) => |
| 2454 |
{ |
| 2455 |
f(tcs, cts.Token); |
| 2456 |
return tcs.Task.UsingEnumerator(e); |
| 2457 |
}, |
| 2458 |
() => current, |
| 2459 |
d.Dispose |
| 2460 |
); |
| 2461 |
}); |
| 2462 |
} |
| 2463 |
|
| 2464 |
#endregion |
| 2465 |
} |
| 2466 |
} |