AsyncIterator.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. internal abstract class AsyncIterator<TSource> : IAsyncEnumerable<TSource>, IAsyncEnumerator<TSource>
  12. {
  13. public enum State
  14. {
  15. New = 0,
  16. Allocated = 1,
  17. Iterating = 2,
  18. Disposed = -1,
  19. }
  20. private readonly int threadId;
  21. internal State state = State.New;
  22. internal TSource current;
  23. private CancellationTokenSource cancellationTokenSource;
  24. protected AsyncIterator()
  25. {
  26. threadId = Environment.CurrentManagedThreadId;
  27. }
  28. public abstract AsyncIterator<TSource> Clone();
  29. public IAsyncEnumerator<TSource> GetEnumerator()
  30. {
  31. var enumerator = state == State.New && threadId == Environment.CurrentManagedThreadId ? this : Clone();
  32. enumerator.state = State.Allocated;
  33. enumerator.cancellationTokenSource = new CancellationTokenSource();
  34. return enumerator;
  35. }
  36. public virtual void Dispose()
  37. {
  38. if (!cancellationTokenSource.IsCancellationRequested)
  39. {
  40. cancellationTokenSource.Cancel();
  41. }
  42. cancellationTokenSource.Dispose();
  43. current = default(TSource);
  44. state = State.Disposed;
  45. }
  46. public TSource Current => current;
  47. public async Task<bool> MoveNext(CancellationToken cancellationToken)
  48. {
  49. if (state == State.Disposed)
  50. return false;
  51. using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, cancellationTokenSource.Token))
  52. using (cancellationToken.Register(Dispose))
  53. {
  54. try
  55. {
  56. var result = await MoveNextCore(cts.Token).ConfigureAwait(false);
  57. return result;
  58. }
  59. catch
  60. {
  61. Dispose();
  62. throw;
  63. }
  64. }
  65. }
  66. protected abstract Task<bool> MoveNextCore(CancellationToken cancellationToken);
  67. public virtual IAsyncEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
  68. {
  69. return new SelectEnumerableAsyncIterator<TSource, TResult>(this, selector);
  70. }
  71. public virtual IAsyncEnumerable<TSource> Where(Func<TSource, bool> predicate)
  72. {
  73. return new WhereEnumerableAsyncIterator<TSource>(this, predicate);
  74. }
  75. }
  76. }
  77. }