Create.cs 4.0 KB

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