12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Runtime.CompilerServices;
- namespace System.Threading.Tasks
- {
- public static class AsyncEnumerableExtensions
- {
- #if !BCL_HAS_CONFIGUREAWAIT // https://github.com/dotnet/coreclr/pull/21939
- /// <summary>Configures how awaits on the tasks returned from an async iteration will be performed.</summary>
- /// <typeparam name="T">The type of the objects being iterated.</typeparam>
- /// <param name="source">The source enumerable being iterated.</param>
- /// <param name="continueOnCapturedContext">Whether to capture and marshal back to the current context.</param>
- /// <returns>The configured enumerable.</returns>
- public static ConfiguredCancelableAsyncEnumerable<T> ConfigureAwait<T>(
- this IAsyncEnumerable<T> source, bool continueOnCapturedContext) =>
- new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext, cancellationToken: default);
- /// <summary>Sets the <see cref="CancellationToken"/> to be passed to <see cref="IAsyncEnumerable{T}.GetAsyncEnumerator(CancellationToken)"/> when iterating.</summary>
- /// <typeparam name="T">The type of the objects being iterated.</typeparam>
- /// <param name="source">The source enumerable being iterated.</param>
- /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
- /// <returns>The configured enumerable.</returns>
- public static ConfiguredCancelableAsyncEnumerable<T> WithCancellation<T>(
- this IAsyncEnumerable<T> source, CancellationToken cancellationToken) =>
- new ConfiguredCancelableAsyncEnumerable<T>(source, continueOnCapturedContext: true, cancellationToken);
- #elif !REFERENCE_ASSEMBLY
- // we need to carry an impl that delegates to the BCL version of these
- /// <summary>Configures how awaits on the tasks returned from an async iteration will be performed.</summary>
- /// <typeparam name="T">The type of the objects being iterated.</typeparam>
- /// <param name="source">The source enumerable being iterated.</param>
- /// <param name="continueOnCapturedContext">Whether to capture and marshal back to the current context.</param>
- /// <returns>The configured enumerable.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ConfiguredCancelableAsyncEnumerable<T> ConfigureAwait<T>(
- this IAsyncEnumerable<T> source, bool continueOnCapturedContext) => TaskExtensions.ConfigureAwait(source, continueOnCapturedContext);
- /// <summary>Sets the <see cref="CancellationToken"/> to be passed to <see cref="IAsyncEnumerable{T}.GetAsyncEnumerator(CancellationToken)"/> when iterating.</summary>
- /// <typeparam name="T">The type of the objects being iterated.</typeparam>
- /// <param name="source">The source enumerable being iterated.</param>
- /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
- /// <returns>The configured enumerable.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static ConfiguredCancelableAsyncEnumerable<T> WithCancellation<T>(
- this IAsyncEnumerable<T> source, CancellationToken cancellationToken) => TaskExtensions.WithCancellation(source, cancellationToken);
- #else
- // Reference assembly, these won't be emmited, but keep these internal so we can compile
- /// <summary>Configures how awaits on the tasks returned from an async iteration will be performed.</summary>
- /// <typeparam name="T">The type of the objects being iterated.</typeparam>
- /// <param name="source">The source enumerable being iterated.</param>
- /// <param name="continueOnCapturedContext">Whether to capture and marshal back to the current context.</param>
- /// <returns>The configured enumerable.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static ConfiguredCancelableAsyncEnumerable<T> ConfigureAwait<T>(
- this IAsyncEnumerable<T> source, bool continueOnCapturedContext) => TaskExtensions.ConfigureAwait(source, continueOnCapturedContext);
- /// <summary>Sets the <see cref="CancellationToken"/> to be passed to <see cref="IAsyncEnumerable{T}.GetAsyncEnumerator(CancellationToken)"/> when iterating.</summary>
- /// <typeparam name="T">The type of the objects being iterated.</typeparam>
- /// <param name="source">The source enumerable being iterated.</param>
- /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
- /// <returns>The configured enumerable.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static ConfiguredCancelableAsyncEnumerable<T> WithCancellation<T>(
- this IAsyncEnumerable<T> source, CancellationToken cancellationToken) => TaskExtensions.WithCancellation(source, cancellationToken);
- #endif
- public static ConfiguredCancelableAsyncEnumerable<T>.Enumerator GetConfiguredAsyncEnumerator<T>(this IAsyncEnumerable<T> enumerable, CancellationToken cancellationToken, bool continueOnCapturedContext)
- {
- #if REFERENCE_ASSEMBLY
- return default;
- #else
- return ConfigureAwait(enumerable, continueOnCapturedContext).WithCancellation(cancellationToken).GetAsyncEnumerator();
- #endif
- }
- }
- }
|