Create.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 new ArgumentNullException(nameof(getEnumerator));
  16. return new AnonymousAsyncEnumerable<T>(getEnumerator);
  17. }
  18. public static IAsyncEnumerable<T> CreateEnumerable<T>(Func<Task<IAsyncEnumerator<T>>> getEnumerator)
  19. {
  20. if (getEnumerator == null)
  21. throw new ArgumentNullException(nameof(getEnumerator));
  22. return new AnonymousAsyncEnumerableWithTask<T>(getEnumerator);
  23. }
  24. public static IAsyncEnumerator<T> CreateEnumerator<T>(Func<Task<bool>> moveNext, Func<T> current, Func<Task> dispose)
  25. {
  26. return AsyncEnumerator.Create(moveNext, current, dispose);
  27. }
  28. private static IAsyncEnumerator<T> CreateEnumerator<T>(Func<TaskCompletionSource<bool>, Task<bool>> moveNext, Func<T> current, Func<Task> dispose)
  29. {
  30. return AsyncEnumerator.Create(moveNext, current, dispose);
  31. }
  32. private sealed class AnonymousAsyncEnumerable<T> : IAsyncEnumerable<T>
  33. {
  34. private readonly Func<IAsyncEnumerator<T>> getEnumerator;
  35. public AnonymousAsyncEnumerable(Func<IAsyncEnumerator<T>> getEnumerator)
  36. {
  37. Debug.Assert(getEnumerator != null);
  38. this.getEnumerator = getEnumerator;
  39. }
  40. public IAsyncEnumerator<T> GetAsyncEnumerator() => getEnumerator();
  41. }
  42. private sealed class AnonymousAsyncEnumerableWithTask<T> : IAsyncEnumerable<T>
  43. {
  44. private readonly Func<Task<IAsyncEnumerator<T>>> getEnumerator;
  45. public AnonymousAsyncEnumerableWithTask(Func<Task<IAsyncEnumerator<T>>> getEnumerator)
  46. {
  47. Debug.Assert(getEnumerator != null);
  48. this.getEnumerator = getEnumerator;
  49. }
  50. public IAsyncEnumerator<T> GetAsyncEnumerator() => new Enumerator(getEnumerator);
  51. private sealed class Enumerator : IAsyncEnumerator<T>
  52. {
  53. private Func<Task<IAsyncEnumerator<T>>> getEnumerator;
  54. private IAsyncEnumerator<T> enumerator;
  55. public Enumerator(Func<Task<IAsyncEnumerator<T>>> getEnumerator)
  56. {
  57. Debug.Assert(getEnumerator != null);
  58. this.getEnumerator = getEnumerator;
  59. }
  60. public T Current
  61. {
  62. get
  63. {
  64. if (enumerator == null)
  65. throw new InvalidOperationException();
  66. return enumerator.Current;
  67. }
  68. }
  69. public async Task DisposeAsync()
  70. {
  71. var old = Interlocked.Exchange(ref enumerator, DisposedEnumerator.Instance);
  72. if (enumerator != null)
  73. {
  74. await enumerator.DisposeAsync().ConfigureAwait(false);
  75. }
  76. }
  77. public Task<bool> MoveNextAsync()
  78. {
  79. if (enumerator == null)
  80. {
  81. return InitAndMoveNextAsync();
  82. }
  83. return enumerator.MoveNextAsync();
  84. }
  85. private async Task<bool> InitAndMoveNextAsync()
  86. {
  87. try
  88. {
  89. enumerator = await getEnumerator().ConfigureAwait(false);
  90. }
  91. catch (Exception ex)
  92. {
  93. enumerator = Throw<T>(ex).GetAsyncEnumerator();
  94. throw;
  95. }
  96. finally
  97. {
  98. getEnumerator = null;
  99. }
  100. return await enumerator.MoveNextAsync().ConfigureAwait(false);
  101. }
  102. private sealed class DisposedEnumerator : IAsyncEnumerator<T>
  103. {
  104. public static readonly DisposedEnumerator Instance = new DisposedEnumerator();
  105. public T Current => throw new ObjectDisposedException("this");
  106. public Task DisposeAsync() => TaskExt.CompletedTask;
  107. public Task<bool> MoveNextAsync() => throw new ObjectDisposedException("this");
  108. }
  109. }
  110. }
  111. }
  112. }