Create.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static IAsyncEnumerable<T> CreateEnumerable<T>(Func<IAsyncEnumerator<T>> getEnumerator)
  12. {
  13. if (getEnumerator == null)
  14. throw new ArgumentNullException(nameof(getEnumerator));
  15. return new AnonymousAsyncEnumerable<T>(getEnumerator);
  16. }
  17. public static IAsyncEnumerator<T> CreateEnumerator<T>(Func<Task<bool>> moveNext, Func<T> current, Func<Task> dispose)
  18. {
  19. if (moveNext == null)
  20. throw new ArgumentNullException(nameof(moveNext));
  21. // Note: Many methods pass null in for the second two params. We're assuming
  22. // That the caller is responsible and knows what they're doing
  23. return new AnonymousAsyncIterator<T>(moveNext, current, dispose);
  24. }
  25. private static IAsyncEnumerator<T> CreateEnumerator<T>(Func<TaskCompletionSource<bool>, Task<bool>> moveNext, Func<T> current, Func<Task> dispose)
  26. {
  27. var self = new AnonymousAsyncIterator<T>(
  28. async () =>
  29. {
  30. var tcs = new TaskCompletionSource<bool>();
  31. var stop = new Action(() => tcs.TrySetCanceled());
  32. return await moveNext(tcs).ConfigureAwait(false);
  33. },
  34. current,
  35. dispose
  36. );
  37. return self;
  38. }
  39. private sealed class AnonymousAsyncEnumerable<T> : IAsyncEnumerable<T>
  40. {
  41. private readonly Func<IAsyncEnumerator<T>> getEnumerator;
  42. public AnonymousAsyncEnumerable(Func<IAsyncEnumerator<T>> getEnumerator)
  43. {
  44. Debug.Assert(getEnumerator != null);
  45. this.getEnumerator = getEnumerator;
  46. }
  47. public IAsyncEnumerator<T> GetAsyncEnumerator() => getEnumerator();
  48. }
  49. private sealed class AnonymousAsyncIterator<T> : AsyncIterator<T>
  50. {
  51. private readonly Func<T> currentFunc;
  52. private readonly Func<Task> dispose;
  53. private readonly Func<Task<bool>> moveNext;
  54. public AnonymousAsyncIterator(Func<Task<bool>> moveNext, Func<T> currentFunc, Func<Task> dispose)
  55. {
  56. Debug.Assert(moveNext != null);
  57. this.moveNext = moveNext;
  58. this.currentFunc = currentFunc;
  59. this.dispose = dispose;
  60. // Explicit call to initialize enumerator mode
  61. GetAsyncEnumerator();
  62. }
  63. public override AsyncIterator<T> Clone()
  64. {
  65. throw new NotSupportedException("AnonymousAsyncIterator cannot be cloned. It is only intended for use as an iterator.");
  66. }
  67. public override async Task DisposeAsync()
  68. {
  69. if (dispose != null)
  70. {
  71. await dispose().ConfigureAwait(false);
  72. }
  73. await base.DisposeAsync().ConfigureAwait(false);
  74. }
  75. protected override async Task<bool> MoveNextCore()
  76. {
  77. switch (state)
  78. {
  79. case AsyncIteratorState.Allocated:
  80. state = AsyncIteratorState.Iterating;
  81. goto case AsyncIteratorState.Iterating;
  82. case AsyncIteratorState.Iterating:
  83. if (await moveNext().ConfigureAwait(false))
  84. {
  85. current = currentFunc();
  86. return true;
  87. }
  88. await DisposeAsync().ConfigureAwait(false);
  89. break;
  90. }
  91. return false;
  92. }
  93. }
  94. }
  95. }