AsyncIterator.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. private static readonly Task CompletedTask = Task.FromResult(true); // TODO: Change to Task.CompletedTask when all build targets allow.
  12. internal abstract class AsyncIterator<TSource> : IAsyncEnumerable<TSource>, IAsyncEnumerator<TSource>
  13. {
  14. private readonly int threadId;
  15. private CancellationTokenSource cancellationTokenSource;
  16. private bool currentIsInvalid = true;
  17. internal TSource current;
  18. internal AsyncIteratorState state = AsyncIteratorState.New;
  19. protected AsyncIterator()
  20. {
  21. threadId = Environment.CurrentManagedThreadId;
  22. }
  23. public IAsyncEnumerator<TSource> GetAsyncEnumerator()
  24. {
  25. var enumerator = state == AsyncIteratorState.New && threadId == Environment.CurrentManagedThreadId ?
  26. this :
  27. Clone();
  28. enumerator.state = AsyncIteratorState.Allocated;
  29. enumerator.cancellationTokenSource = new CancellationTokenSource();
  30. try
  31. {
  32. enumerator.OnGetEnumerator();
  33. }
  34. catch
  35. {
  36. enumerator.DisposeAsync(); // REVIEW: fire-and-forget?
  37. throw;
  38. }
  39. return enumerator;
  40. }
  41. public virtual Task DisposeAsync()
  42. {
  43. if (cancellationTokenSource != null)
  44. {
  45. if (!cancellationTokenSource.IsCancellationRequested)
  46. {
  47. cancellationTokenSource.Cancel();
  48. }
  49. cancellationTokenSource.Dispose();
  50. }
  51. current = default(TSource);
  52. state = AsyncIteratorState.Disposed;
  53. return CompletedTask;
  54. }
  55. public TSource Current
  56. {
  57. get
  58. {
  59. if (currentIsInvalid)
  60. throw new InvalidOperationException("Enumerator is in an invalid state");
  61. return current;
  62. }
  63. }
  64. public async Task<bool> MoveNextAsync()
  65. {
  66. // Note: MoveNext *must* be implemented as an async method to ensure
  67. // that any exceptions thrown from the MoveNextCore call are handled
  68. // by the try/catch, whether they're sync or async
  69. if (state == AsyncIteratorState.Disposed)
  70. {
  71. return false;
  72. }
  73. try
  74. {
  75. var result = await MoveNextCore()
  76. .ConfigureAwait(false);
  77. currentIsInvalid = !result; // if move next is false, invalid otherwise valid
  78. return result;
  79. }
  80. catch
  81. {
  82. currentIsInvalid = true;
  83. await DisposeAsync().ConfigureAwait(false);
  84. throw;
  85. }
  86. }
  87. public abstract AsyncIterator<TSource> Clone();
  88. public virtual IAsyncEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector)
  89. {
  90. return new SelectEnumerableAsyncIterator<TSource, TResult>(this, selector);
  91. }
  92. public virtual IAsyncEnumerable<TResult> Select<TResult>(Func<TSource, Task<TResult>> selector)
  93. {
  94. return new SelectEnumerableAsyncIteratorWithTask<TSource, TResult>(this, selector);
  95. }
  96. public virtual IAsyncEnumerable<TSource> Where(Func<TSource, bool> predicate)
  97. {
  98. return new WhereEnumerableAsyncIterator<TSource>(this, predicate);
  99. }
  100. public virtual IAsyncEnumerable<TSource> Where(Func<TSource, Task<bool>> predicate)
  101. {
  102. return new WhereEnumerableAsyncIteratorWithTask<TSource>(this, predicate);
  103. }
  104. protected abstract Task<bool> MoveNextCore();
  105. protected virtual void OnGetEnumerator()
  106. {
  107. }
  108. }
  109. internal enum AsyncIteratorState
  110. {
  111. New = 0,
  112. Allocated = 1,
  113. Iterating = 2,
  114. Disposed = -1
  115. }
  116. }
  117. }