Defer.cs 5.4 KB

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