Using.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. // REVIEW: Add support for IAsyncDisposable resources.
  12. /// <summary>
  13. /// Constructs an async-enumerable sequence that depends on a resource object, whose lifetime is tied to the resulting async-enumerable sequence's lifetime.
  14. /// </summary>
  15. /// <typeparam name="TSource">The type of the elements in the produced sequence.</typeparam>
  16. /// <typeparam name="TResource">The type of the resource used during the generation of the resulting sequence. Needs to implement <see cref="IDisposable"/>.</typeparam>
  17. /// <param name="resourceFactory">Factory function to obtain a resource object.</param>
  18. /// <param name="enumerableFactory">Factory function to obtain an async-enumerable sequence that depends on the obtained resource.</param>
  19. /// <returns>An async-enumerable sequence whose lifetime controls the lifetime of the dependent resource object.</returns>
  20. /// <exception cref="ArgumentNullException"><paramref name="resourceFactory"/> or <paramref name="enumerableFactory"/> is null.</exception>
  21. public static IAsyncEnumerable<TSource> Using<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory) where TResource : IDisposable
  22. {
  23. if (resourceFactory == null)
  24. throw Error.ArgumentNull(nameof(resourceFactory));
  25. if (enumerableFactory == null)
  26. throw Error.ArgumentNull(nameof(enumerableFactory));
  27. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  28. return Core(resourceFactory, enumerableFactory);
  29. static async IAsyncEnumerable<TSource> Core(Func<TResource> resourceFactory, Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  30. #else
  31. return AsyncEnumerable.Create(Core);
  32. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  33. #endif
  34. {
  35. using var resource = resourceFactory();
  36. await foreach (var item in enumerableFactory(resource).WithCancellation(cancellationToken).ConfigureAwait(false))
  37. {
  38. yield return item;
  39. }
  40. }
  41. }
  42. /// <summary>
  43. /// Constructs an async-enumerable sequence that depends on a resource object, whose lifetime is tied to the resulting async-enumerable sequence's lifetime.
  44. /// </summary>
  45. /// <typeparam name="TSource">The type of the elements in the produced sequence.</typeparam>
  46. /// <typeparam name="TResource">The type of the resource used during the generation of the resulting sequence. Needs to implement <see cref="IDisposable"/>.</typeparam>
  47. /// <param name="resourceFactory">Asynchronous factory function to obtain a resource object.</param>
  48. /// <param name="enumerableFactory">Asynchronous factory function to obtain an async-enumerable sequence that depends on the obtained resource.</param>
  49. /// <returns>An async-enumerable sequence whose lifetime controls the lifetime of the dependent resource object.</returns>
  50. /// <exception cref="ArgumentNullException"><paramref name="resourceFactory"/> or <paramref name="enumerableFactory"/> is null.</exception>
  51. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  52. /// <remarks>When a subscription to the resulting sequence is disposed, the CancellationToken that was fed to the asynchronous resource factory and async-enumerable factory functions will be signaled.</remarks>
  53. public static IAsyncEnumerable<TSource> Using<TSource, TResource>(Func<Task<TResource>> resourceFactory, Func<TResource, ValueTask<IAsyncEnumerable<TSource>>> enumerableFactory) where TResource : IDisposable
  54. {
  55. if (resourceFactory == null)
  56. throw Error.ArgumentNull(nameof(resourceFactory));
  57. if (enumerableFactory == null)
  58. throw Error.ArgumentNull(nameof(enumerableFactory));
  59. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  60. return Core(resourceFactory, enumerableFactory);
  61. static async IAsyncEnumerable<TSource> Core(Func<Task<TResource>> resourceFactory, Func<TResource, ValueTask<IAsyncEnumerable<TSource>>> enumerableFactory, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  62. #else
  63. return AsyncEnumerable.Create(Core);
  64. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  65. #endif
  66. {
  67. using var resource = await resourceFactory().ConfigureAwait(false);
  68. await foreach (var item in (await enumerableFactory(resource).ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false))
  69. {
  70. yield return item;
  71. }
  72. }
  73. }
  74. #if !NO_DEEP_CANCELLATION
  75. /// <summary>
  76. /// Constructs an async-enumerable sequence that depends on a resource object, whose lifetime is tied to the resulting async-enumerable sequence's lifetime. The resource is obtained and used through asynchronous methods.
  77. /// The CancellationToken passed to the asynchronous methods is tied to the returned disposable subscription, allowing best-effort cancellation at any stage of the resource acquisition or usage.
  78. /// </summary>
  79. /// <typeparam name="TSource">The type of the elements in the produced sequence.</typeparam>
  80. /// <typeparam name="TResource">The type of the resource used during the generation of the resulting sequence. Needs to implement <see cref="IDisposable"/>.</typeparam>
  81. /// <param name="resourceFactory">Asynchronous factory function to obtain a resource object.</param>
  82. /// <param name="enumerableFactory">Asynchronous factory function to obtain an async-enumerable sequence that depends on the obtained resource.</param>
  83. /// <returns>An async-enumerable sequence whose lifetime controls the lifetime of the dependent resource object.</returns>
  84. /// <exception cref="ArgumentNullException"><paramref name="resourceFactory"/> or <paramref name="enumerableFactory"/> is null.</exception>
  85. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  86. /// <remarks>When a subscription to the resulting sequence is disposed, the CancellationToken that was fed to the asynchronous resource factory and async-enumerable factory functions will be signaled.</remarks>
  87. public static IAsyncEnumerable<TSource> Using<TSource, TResource>(Func<CancellationToken, Task<TResource>> resourceFactory, Func<TResource, CancellationToken, ValueTask<IAsyncEnumerable<TSource>>> enumerableFactory) where TResource : IDisposable
  88. {
  89. if (resourceFactory == null)
  90. throw Error.ArgumentNull(nameof(resourceFactory));
  91. if (enumerableFactory == null)
  92. throw Error.ArgumentNull(nameof(enumerableFactory));
  93. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  94. return Core(resourceFactory, enumerableFactory);
  95. static async IAsyncEnumerable<TSource> Core(Func<CancellationToken, Task<TResource>> resourceFactory, Func<TResource, CancellationToken, ValueTask<IAsyncEnumerable<TSource>>> enumerableFactory, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  96. #else
  97. return AsyncEnumerable.Create(Core);
  98. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  99. #endif
  100. {
  101. using var resource = await resourceFactory(cancellationToken).ConfigureAwait(false);
  102. await foreach (var item in (await enumerableFactory(resource, cancellationToken).ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false))
  103. {
  104. yield return item;
  105. }
  106. }
  107. }
  108. #endif
  109. }
  110. }