Create.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<T> CreateEnumerable<T>(Func<IAsyncEnumerator<T>> getEnumerator)
  14. {
  15. return new AnonymousAsyncEnumerable<T>(getEnumerator);
  16. }
  17. public static IAsyncEnumerator<T> CreateEnumerator<T>(Func<CancellationToken, Task<bool>> moveNext, Func<T> current, Action dispose)
  18. {
  19. return new AnonymousAsyncEnumerator<T>(moveNext, current, dispose);
  20. }
  21. private static IAsyncEnumerator<T> CreateEnumerator<T>(Func<CancellationToken, Task<bool>> moveNext, Func<T> current,
  22. Action dispose, IDisposable enumerator)
  23. {
  24. return CreateEnumerator(
  25. async ct =>
  26. {
  27. using (ct.Register(dispose))
  28. {
  29. try
  30. {
  31. var result = await moveNext(ct)
  32. .ConfigureAwait(false);
  33. if (!result)
  34. {
  35. enumerator?.Dispose();
  36. }
  37. return result;
  38. }
  39. catch
  40. {
  41. enumerator?.Dispose();
  42. throw;
  43. }
  44. }
  45. }, current, dispose);
  46. }
  47. private static IAsyncEnumerator<T> CreateEnumerator<T>(Func<CancellationToken, TaskCompletionSource<bool>, Task<bool>> moveNext, Func<T> current, Action dispose)
  48. {
  49. var self = default(IAsyncEnumerator<T>);
  50. self = new AnonymousAsyncEnumerator<T>(
  51. async ct =>
  52. {
  53. var tcs = new TaskCompletionSource<bool>();
  54. var stop = new Action(
  55. () =>
  56. {
  57. self.Dispose();
  58. tcs.TrySetCanceled();
  59. });
  60. using (ct.Register(stop))
  61. {
  62. return await moveNext(ct, tcs)
  63. .ConfigureAwait(false);
  64. }
  65. },
  66. current,
  67. dispose
  68. );
  69. return self;
  70. }
  71. private class AnonymousAsyncEnumerable<T> : IAsyncEnumerable<T>
  72. {
  73. private readonly Func<IAsyncEnumerator<T>> getEnumerator;
  74. public AnonymousAsyncEnumerable(Func<IAsyncEnumerator<T>> getEnumerator)
  75. {
  76. this.getEnumerator = getEnumerator;
  77. }
  78. public IAsyncEnumerator<T> GetEnumerator()
  79. {
  80. return getEnumerator();
  81. }
  82. }
  83. private class AnonymousAsyncEnumerator<T> : IAsyncEnumerator<T>
  84. {
  85. private readonly Func<T> _current;
  86. private readonly Action _dispose;
  87. private readonly Func<CancellationToken, Task<bool>> _moveNext;
  88. private bool _disposed;
  89. public AnonymousAsyncEnumerator(Func<CancellationToken, Task<bool>> moveNext, Func<T> current, Action dispose)
  90. {
  91. _moveNext = moveNext;
  92. _current = current;
  93. _dispose = dispose;
  94. }
  95. public Task<bool> MoveNext(CancellationToken cancellationToken)
  96. {
  97. if (_disposed)
  98. return TaskExt.False;
  99. return _moveNext(cancellationToken);
  100. }
  101. public T Current => _current();
  102. public void Dispose()
  103. {
  104. if (!_disposed)
  105. {
  106. _disposed = true;
  107. _dispose();
  108. }
  109. }
  110. }
  111. }
  112. }