Create.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.Diagnostics;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<T> CreateEnumerable<T>(Func<IAsyncEnumerator<T>> getEnumerator)
  13. {
  14. if (getEnumerator == null)
  15. throw Error.ArgumentNull(nameof(getEnumerator));
  16. return new AnonymousAsyncEnumerable<T>(_ => getEnumerator());
  17. }
  18. public static IAsyncEnumerable<T> CreateEnumerable<T>(Func<CancellationToken, IAsyncEnumerator<T>> getEnumerator)
  19. {
  20. if (getEnumerator == null)
  21. throw Error.ArgumentNull(nameof(getEnumerator));
  22. return new AnonymousAsyncEnumerable<T>(getEnumerator);
  23. }
  24. public static IAsyncEnumerable<T> CreateEnumerable<T>(Func<Task<IAsyncEnumerator<T>>> getEnumerator)
  25. {
  26. if (getEnumerator == null)
  27. throw Error.ArgumentNull(nameof(getEnumerator));
  28. return new AnonymousAsyncEnumerableWithTask<T>(_ => getEnumerator());
  29. }
  30. public static IAsyncEnumerable<T> CreateEnumerable<T>(Func<CancellationToken, Task<IAsyncEnumerator<T>>> getEnumerator)
  31. {
  32. if (getEnumerator == null)
  33. throw Error.ArgumentNull(nameof(getEnumerator));
  34. return new AnonymousAsyncEnumerableWithTask<T>(getEnumerator);
  35. }
  36. public static IAsyncEnumerator<T> CreateEnumerator<T>(Func<ValueTask<bool>> moveNext, Func<T> current, Func<ValueTask> dispose)
  37. {
  38. return AsyncEnumerator.Create(moveNext, current, dispose);
  39. }
  40. private static IAsyncEnumerator<T> CreateEnumerator<T>(Func<TaskCompletionSource<bool>, ValueTask<bool>> moveNext, Func<T> current, Func<ValueTask> dispose)
  41. {
  42. return AsyncEnumerator.Create(moveNext, current, dispose);
  43. }
  44. private sealed class AnonymousAsyncEnumerable<T> : IAsyncEnumerable<T>
  45. {
  46. private readonly Func<CancellationToken, IAsyncEnumerator<T>> _getEnumerator;
  47. public AnonymousAsyncEnumerable(Func<CancellationToken, IAsyncEnumerator<T>> getEnumerator)
  48. {
  49. Debug.Assert(getEnumerator != null);
  50. _getEnumerator = getEnumerator;
  51. }
  52. public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken) => _getEnumerator(cancellationToken);
  53. }
  54. private sealed class AnonymousAsyncEnumerableWithTask<T> : IAsyncEnumerable<T>
  55. {
  56. private readonly Func<CancellationToken, Task<IAsyncEnumerator<T>>> _getEnumerator;
  57. public AnonymousAsyncEnumerableWithTask(Func<CancellationToken, Task<IAsyncEnumerator<T>>> getEnumerator)
  58. {
  59. Debug.Assert(getEnumerator != null);
  60. _getEnumerator = getEnumerator;
  61. }
  62. public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken) => new Enumerator(_getEnumerator, cancellationToken);
  63. private sealed class Enumerator : IAsyncEnumerator<T>
  64. {
  65. private Func<CancellationToken, Task<IAsyncEnumerator<T>>> _getEnumerator;
  66. private readonly CancellationToken _cancellationToken;
  67. private IAsyncEnumerator<T> _enumerator;
  68. public Enumerator(Func<CancellationToken, Task<IAsyncEnumerator<T>>> getEnumerator, CancellationToken cancellationToken)
  69. {
  70. Debug.Assert(getEnumerator != null);
  71. _getEnumerator = getEnumerator;
  72. _cancellationToken = cancellationToken;
  73. }
  74. public T Current
  75. {
  76. get
  77. {
  78. if (_enumerator == null)
  79. throw new InvalidOperationException();
  80. return _enumerator.Current;
  81. }
  82. }
  83. public async ValueTask DisposeAsync()
  84. {
  85. var old = Interlocked.Exchange(ref _enumerator, DisposedEnumerator.Instance);
  86. if (_enumerator != null)
  87. {
  88. await _enumerator.DisposeAsync().ConfigureAwait(false);
  89. }
  90. }
  91. public ValueTask<bool> MoveNextAsync()
  92. {
  93. if (_enumerator == null)
  94. {
  95. return InitAndMoveNextAsync();
  96. }
  97. return _enumerator.MoveNextAsync();
  98. }
  99. private async ValueTask<bool> InitAndMoveNextAsync()
  100. {
  101. try
  102. {
  103. _enumerator = await _getEnumerator(_cancellationToken).ConfigureAwait(false);
  104. }
  105. catch (Exception ex)
  106. {
  107. _enumerator = Throw<T>(ex).GetAsyncEnumerator(_cancellationToken);
  108. throw;
  109. }
  110. finally
  111. {
  112. _getEnumerator = null;
  113. }
  114. return await _enumerator.MoveNextAsync().ConfigureAwait(false);
  115. }
  116. private sealed class DisposedEnumerator : IAsyncEnumerator<T>
  117. {
  118. public static readonly DisposedEnumerator Instance = new DisposedEnumerator();
  119. public T Current => throw new ObjectDisposedException("this");
  120. public ValueTask DisposeAsync() => TaskExt.CompletedTask;
  121. public ValueTask<bool> MoveNextAsync() => throw new ObjectDisposedException("this");
  122. }
  123. }
  124. }
  125. }
  126. }