Create.cs 4.7 KB

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