Defer.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /// <summary>
  12. /// Returns an async-enumerable sequence that invokes the specified factory function whenever a new observer subscribes.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>
  15. /// <param name="factory">The async-enumerable factory function to invoke for each consumer that starts enumerating the resulting asynchronous sequence.</param>
  16. /// <returns>An async-enumerable sequence whose observers trigger an invocation of the given async-enumerable factory function.</returns>
  17. /// <exception cref="ArgumentNullException"><paramref name="factory"/> is null.</exception>
  18. public static IAsyncEnumerable<TSource> Defer<TSource>(Func<IAsyncEnumerable<TSource>> factory)
  19. {
  20. if (factory == null)
  21. throw Error.ArgumentNull(nameof(factory));
  22. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  23. return Core(factory);
  24. static async IAsyncEnumerable<TSource> Core(Func<IAsyncEnumerable<TSource>> factory, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  25. #else
  26. return AsyncEnumerable.Create(Core);
  27. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  28. #endif
  29. {
  30. await foreach (var item in factory().WithCancellation(cancellationToken).ConfigureAwait(false))
  31. {
  32. yield return item;
  33. }
  34. }
  35. }
  36. /// <summary>
  37. /// Returns an async-enumerable sequence that starts the specified asynchronous factory function whenever a new observer subscribes.
  38. /// </summary>
  39. /// <typeparam name="TSource">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>
  40. /// <param name="factory">Asynchronous factory function to start for each consumer that starts enumerating the resulting asynchronous sequence.</param>
  41. /// <returns>An async-enumerable sequence whose observers trigger the given asynchronous async-enumerable factory function to be started.</returns>
  42. /// <exception cref="ArgumentNullException"><paramref name="factory"/> is null.</exception>
  43. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  44. public static IAsyncEnumerable<TSource> Defer<TSource>(Func<Task<IAsyncEnumerable<TSource>>> factory)
  45. {
  46. if (factory == null)
  47. throw Error.ArgumentNull(nameof(factory));
  48. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  49. return Core(factory);
  50. static async IAsyncEnumerable<TSource> Core(Func<Task<IAsyncEnumerable<TSource>>> factory, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  51. #else
  52. return AsyncEnumerable.Create(Core);
  53. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  54. #endif
  55. {
  56. await foreach (var item in (await factory().ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false))
  57. {
  58. yield return item;
  59. }
  60. }
  61. }
  62. #if !NO_DEEP_CANCELLATION
  63. /// <summary>
  64. /// Returns an async-enumerable sequence that starts the specified cancellable asynchronous factory function whenever a new observer subscribes.
  65. /// The CancellationToken passed to the asynchronous factory function is tied to the returned disposable subscription, allowing best-effort cancellation.
  66. /// </summary>
  67. /// <typeparam name="TSource">The type of the elements in the sequence returned by the factory function, and in the resulting sequence.</typeparam>
  68. /// <param name="factory">Asynchronous factory function, supporting cancellation, to start for each consumer that starts enumerating the resulting asynchronous sequence.</param>
  69. /// <returns>An async-enumerable sequence whose observers trigger the given asynchronous async-enumerable factory function to be started.</returns>
  70. /// <exception cref="ArgumentNullException"><paramref name="factory"/> is null.</exception>
  71. /// <remarks>This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11.</remarks>
  72. /// <remarks>When a subscription to the resulting sequence is disposed, the CancellationToken that was fed to the asynchronous async-enumerable factory function will be signaled.</remarks>
  73. public static IAsyncEnumerable<TSource> Defer<TSource>(Func<CancellationToken, Task<IAsyncEnumerable<TSource>>> factory)
  74. {
  75. if (factory == null)
  76. throw Error.ArgumentNull(nameof(factory));
  77. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  78. return Core(factory);
  79. static async IAsyncEnumerable<TSource> Core(Func<CancellationToken, Task<IAsyncEnumerable<TSource>>> factory, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  80. #else
  81. return AsyncEnumerable.Create(Core);
  82. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  83. #endif
  84. {
  85. await foreach (var item in (await factory(cancellationToken).ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false))
  86. {
  87. yield return item;
  88. }
  89. }
  90. }
  91. #endif
  92. }
  93. }