// 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
/// Configures how awaits on the tasks returned from an async iteration will be performed.
/// The type of the objects being iterated.
/// The source enumerable being iterated.
/// Whether to capture and marshal back to the current context.
/// The configured enumerable.
public static ConfiguredCancelableAsyncEnumerable ConfigureAwait(
this IAsyncEnumerable source, bool continueOnCapturedContext) =>
new ConfiguredCancelableAsyncEnumerable(source, continueOnCapturedContext, cancellationToken: default);
/// Sets the to be passed to when iterating.
/// The type of the objects being iterated.
/// The source enumerable being iterated.
/// The to use.
/// The configured enumerable.
public static ConfiguredCancelableAsyncEnumerable WithCancellation(
this IAsyncEnumerable source, CancellationToken cancellationToken) =>
new ConfiguredCancelableAsyncEnumerable(source, continueOnCapturedContext: true, cancellationToken);
#elif !REFERENCE_ASSEMBLY
// we need to carry an impl that delegates to the BCL version of these
/// Configures how awaits on the tasks returned from an async iteration will be performed.
/// The type of the objects being iterated.
/// The source enumerable being iterated.
/// Whether to capture and marshal back to the current context.
/// The configured enumerable.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ConfiguredCancelableAsyncEnumerable ConfigureAwait(
this IAsyncEnumerable source, bool continueOnCapturedContext) => TaskExtensions.ConfigureAwait(source, continueOnCapturedContext);
/// Sets the to be passed to when iterating.
/// The type of the objects being iterated.
/// The source enumerable being iterated.
/// The to use.
/// The configured enumerable.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ConfiguredCancelableAsyncEnumerable WithCancellation(
this IAsyncEnumerable source, CancellationToken cancellationToken) => TaskExtensions.WithCancellation(source, cancellationToken);
#else
// Reference assembly, these won't be emmited, but keep these internal so we can compile
/// Configures how awaits on the tasks returned from an async iteration will be performed.
/// The type of the objects being iterated.
/// The source enumerable being iterated.
/// Whether to capture and marshal back to the current context.
/// The configured enumerable.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ConfiguredCancelableAsyncEnumerable ConfigureAwait(
this IAsyncEnumerable source, bool continueOnCapturedContext) => TaskExtensions.ConfigureAwait(source, continueOnCapturedContext);
/// Sets the to be passed to when iterating.
/// The type of the objects being iterated.
/// The source enumerable being iterated.
/// The to use.
/// The configured enumerable.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static ConfiguredCancelableAsyncEnumerable WithCancellation(
this IAsyncEnumerable source, CancellationToken cancellationToken) => TaskExtensions.WithCancellation(source, cancellationToken);
#endif
public static ConfiguredCancelableAsyncEnumerable.Enumerator GetConfiguredAsyncEnumerator(this IAsyncEnumerable enumerable, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
#if REFERENCE_ASSEMBLY
return default;
#else
return ConfigureAwait(enumerable, continueOnCapturedContext).WithCancellation(cancellationToken).GetAsyncEnumerator();
#endif
}
}
}