Using.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. return Core(resourceFactory, enumerableFactory);
  28. static async IAsyncEnumerable<TSource> Core(Func<TResource> resourceFactory, Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  29. {
  30. using var resource = resourceFactory();
  31. await foreach (var item in enumerableFactory(resource).WithCancellation(cancellationToken).ConfigureAwait(false))
  32. {
  33. yield return item;
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// Constructs an async-enumerable sequence that depends on a resource object, whose lifetime is tied to the resulting async-enumerable sequence's lifetime.
  39. /// </summary>
  40. /// <typeparam name="TSource">The type of the elements in the produced sequence.</typeparam>
  41. /// <typeparam name="TResource">The type of the resource used during the generation of the resulting sequence. Needs to implement <see cref="IDisposable"/>.</typeparam>
  42. /// <param name="resourceFactory">Asynchronous factory function to obtain a resource object.</param>
  43. /// <param name="enumerableFactory">Asynchronous factory function to obtain an async-enumerable sequence that depends on the obtained resource.</param>
  44. /// <returns>An async-enumerable sequence whose lifetime controls the lifetime of the dependent resource object.</returns>
  45. /// <exception cref="ArgumentNullException"><paramref name="resourceFactory"/> or <paramref name="enumerableFactory"/> is null.</exception>
  46. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  47. /// <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>
  48. public static IAsyncEnumerable<TSource> Using<TSource, TResource>(Func<Task<TResource>> resourceFactory, Func<TResource, ValueTask<IAsyncEnumerable<TSource>>> enumerableFactory) where TResource : IDisposable
  49. {
  50. if (resourceFactory == null)
  51. throw Error.ArgumentNull(nameof(resourceFactory));
  52. if (enumerableFactory == null)
  53. throw Error.ArgumentNull(nameof(enumerableFactory));
  54. return Core(resourceFactory, enumerableFactory);
  55. static async IAsyncEnumerable<TSource> Core(Func<Task<TResource>> resourceFactory, Func<TResource, ValueTask<IAsyncEnumerable<TSource>>> enumerableFactory, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  56. {
  57. using var resource = await resourceFactory().ConfigureAwait(false);
  58. await foreach (var item in (await enumerableFactory(resource).ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false))
  59. {
  60. yield return item;
  61. }
  62. }
  63. }
  64. #if !NO_DEEP_CANCELLATION
  65. /// <summary>
  66. /// 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.
  67. /// 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.
  68. /// </summary>
  69. /// <typeparam name="TSource">The type of the elements in the produced sequence.</typeparam>
  70. /// <typeparam name="TResource">The type of the resource used during the generation of the resulting sequence. Needs to implement <see cref="IDisposable"/>.</typeparam>
  71. /// <param name="resourceFactory">Asynchronous factory function to obtain a resource object.</param>
  72. /// <param name="enumerableFactory">Asynchronous factory function to obtain an async-enumerable sequence that depends on the obtained resource.</param>
  73. /// <returns>An async-enumerable sequence whose lifetime controls the lifetime of the dependent resource object.</returns>
  74. /// <exception cref="ArgumentNullException"><paramref name="resourceFactory"/> or <paramref name="enumerableFactory"/> is null.</exception>
  75. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  76. /// <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>
  77. public static IAsyncEnumerable<TSource> Using<TSource, TResource>(Func<CancellationToken, Task<TResource>> resourceFactory, Func<TResource, CancellationToken, ValueTask<IAsyncEnumerable<TSource>>> enumerableFactory) where TResource : IDisposable
  78. {
  79. if (resourceFactory == null)
  80. throw Error.ArgumentNull(nameof(resourceFactory));
  81. if (enumerableFactory == null)
  82. throw Error.ArgumentNull(nameof(enumerableFactory));
  83. return Core(resourceFactory, enumerableFactory);
  84. static async IAsyncEnumerable<TSource> Core(Func<CancellationToken, Task<TResource>> resourceFactory, Func<TResource, CancellationToken, ValueTask<IAsyncEnumerable<TSource>>> enumerableFactory, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  85. {
  86. using var resource = await resourceFactory(cancellationToken).ConfigureAwait(false);
  87. await foreach (var item in (await enumerableFactory(resource, cancellationToken).ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false))
  88. {
  89. yield return item;
  90. }
  91. }
  92. }
  93. #endif
  94. }
  95. }