AsyncEnumerableExtensions.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. namespace System.Threading.Tasks
  7. {
  8. // This type is only public for ref assms < .NET Standard 2.1 and implementations
  9. #if !BCL_HAS_CONFIGUREAWAIT || !REFERENCE_ASSEMBLY
  10. public
  11. #else
  12. internal
  13. #endif
  14. static class AsyncEnumerableExtensions
  15. {
  16. #if !BCL_HAS_CONFIGUREAWAIT // https://github.com/dotnet/coreclr/pull/21939
  17. /// <summary>Configures how awaits on the tasks returned from an async disposable will be performed.</summary>
  18. /// <param name="source">The source async disposable.</param>
  19. /// <param name="continueOnCapturedContext">Whether to capture and marshal back to the current context.</param>
  20. /// <returns>The configured async disposable.</returns>
  21. public static ConfiguredAsyncDisposable ConfigureAwait(this IAsyncDisposable source, bool continueOnCapturedContext) =>
  22. new ConfiguredAsyncDisposable(source, continueOnCapturedContext);
  23. /// <summary>Configures how awaits on the tasks returned from an async iteration will be performed.</summary>
  24. /// <typeparam name="T">The type of the objects being iterated.</typeparam>
  25. /// <param name="source">The source enumerable being iterated.</param>
  26. /// <param name="continueOnCapturedContext">Whether to capture and marshal back to the current context.</param>
  27. /// <returns>The configured enumerable.</returns>
  28. public static ConfiguredCancelableAsyncEnumerable<T> ConfigureAwait<T>(
  29. this IAsyncEnumerable<T> source, bool continueOnCapturedContext) =>
  30. new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext, cancellationToken: default);
  31. /// <summary>Sets the <see cref="CancellationToken"/> to be passed to <see cref="IAsyncEnumerable{T}.GetAsyncEnumerator(CancellationToken)"/> when iterating.</summary>
  32. /// <typeparam name="T">The type of the objects being iterated.</typeparam>
  33. /// <param name="source">The source enumerable being iterated.</param>
  34. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
  35. /// <returns>The configured enumerable.</returns>
  36. public static ConfiguredCancelableAsyncEnumerable<T> WithCancellation<T>(
  37. this IAsyncEnumerable<T> source, CancellationToken cancellationToken) =>
  38. new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext: true, cancellationToken);
  39. #else
  40. // we need to carry an impl that delegates to the BCL version of these in the lib
  41. // They won't be in the ref
  42. /// <summary>Configures how awaits on the tasks returned from an async disposable will be performed.</summary>
  43. /// <param name="source">The source async disposable.</param>
  44. /// <param name="continueOnCapturedContext">Whether to capture and marshal back to the current context.</param>
  45. /// <returns>The configured async disposable.</returns>
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public static ConfiguredAsyncDisposable ConfigureAwait(this IAsyncDisposable source, bool continueOnCapturedContext) =>
  48. TaskExtensions.ConfigureAwait(source, continueOnCapturedContext);
  49. /// <summary>Configures how awaits on the tasks returned from an async iteration will be performed.</summary>
  50. /// <typeparam name="T">The type of the objects being iterated.</typeparam>
  51. /// <param name="source">The source enumerable being iterated.</param>
  52. /// <param name="continueOnCapturedContext">Whether to capture and marshal back to the current context.</param>
  53. /// <returns>The configured enumerable.</returns>
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. public static ConfiguredCancelableAsyncEnumerable<T> ConfigureAwait<T>(
  56. this IAsyncEnumerable<T> source, bool continueOnCapturedContext) => TaskExtensions.ConfigureAwait(source, continueOnCapturedContext);
  57. /// <summary>Sets the <see cref="CancellationToken"/> to be passed to <see cref="IAsyncEnumerable{T}.GetAsyncEnumerator(CancellationToken)"/> when iterating.</summary>
  58. /// <typeparam name="T">The type of the objects being iterated.</typeparam>
  59. /// <param name="source">The source enumerable being iterated.</param>
  60. /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
  61. /// <returns>The configured enumerable.</returns>
  62. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. public static ConfiguredCancelableAsyncEnumerable<T> WithCancellation<T>(
  64. this IAsyncEnumerable<T> source, CancellationToken cancellationToken) => TaskExtensions.WithCancellation(source, cancellationToken);
  65. #endif
  66. }
  67. }