// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace System.Linq { public static partial class AsyncEnumerable { #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.firstordefaultasync?view=net-9.0-pp#system-linq-asyncenumerable-firstordefaultasync-1(system-collections-generic-iasyncenumerable((-0))-system-threading-cancellationtoken) /// /// Returns the first element of an async-enumerable sequence, or a default value if no such element exists. /// /// The type of the elements in the source sequence. /// Source async-enumerable sequence. /// The optional cancellation token to be used for cancelling the sequence at any time. /// ValueTask containing the first element in the async-enumerable sequence, or a default value if no such element exists. /// is null. public static ValueTask FirstOrDefaultAsync(this IAsyncEnumerable source, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); return Core(source, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { var first = await TryGetFirst(source, cancellationToken).ConfigureAwait(false); return first.HasValue ? first.Value : default; } } // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.firstordefaultasync?view=net-9.0-pp#system-linq-asyncenumerable-firstordefaultasync-1(system-collections-generic-iasyncenumerable((-0))-system-func((-0-system-boolean))-system-threading-cancellationtoken) /// /// Returns the first element of an async-enumerable sequence that satisfies the condition in the predicate, or a default value if no such element exists. /// /// The type of the elements in the source sequence. /// Source async-enumerable sequence. /// A predicate function to evaluate for elements in the source sequence. /// The optional cancellation token to be used for cancelling the sequence at any time. /// ValueTask containing the first element in the async-enumerable sequence that satisfies the condition in the predicate, or a default value if no such element exists. /// or is null. public static ValueTask FirstOrDefaultAsync(this IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); return Core(source, predicate, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false); return first.HasValue ? first.Value : default; } } #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.firstordefaultasync?view=net-9.0-pp#system-linq-asyncenumerable-firstordefaultasync-1(system-collections-generic-iasyncenumerable((-0))-system-func((-0-system-threading-cancellationtoken-system-threading-tasks-valuetask((system-boolean))))-system-threading-cancellationtoken) /// /// Returns the first element of an async-enumerable sequence that satisfies the condition in the predicate, or a default value if no element satisfies the condition in the predicate. /// /// The type of element in the sequence. /// Source async-enumerable sequence. /// An asynchronous predicate to invoke and await on each element of the sequence. /// An optional cancellation token for cancelling the sequence at any time. /// A ValueTask containing the first element in the sequence that satisfies the predicate, or a default value if no element satisfies the predicate. /// or is . [GenerateAsyncOverload] [Obsolete("Use FirstOrDefaultAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the FirstOrDefaultAwaitAsync functionality now exists as overloads of FirstOrDefaultAsync.")] private static ValueTask FirstOrDefaultAwaitAsyncCore(this IAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); return Core(source, predicate, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) { var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false); return first.HasValue ? first.Value : default; } } #if !NO_DEEP_CANCELLATION [GenerateAsyncOverload] [Obsolete("Use FirstOrDefaultAsync. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the FirstOrDefaultAwaitWithCancellationAsync functionality now exists as overloads of FirstOrDefaultAsync.")] private static ValueTask FirstOrDefaultAwaitWithCancellationAsyncCore(this IAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken = default) { if (source == null) throw Error.ArgumentNull(nameof(source)); if (predicate == null) throw Error.ArgumentNull(nameof(predicate)); return Core(source, predicate, cancellationToken); static async ValueTask Core(IAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) { var first = await TryGetFirst(source, predicate, cancellationToken).ConfigureAwait(false); return first.HasValue ? first.Value : default; } } #endif private static ValueTask> TryGetFirst(IAsyncEnumerable source, CancellationToken cancellationToken) { if (source is IList list) { if (list.Count > 0) { return new ValueTask>(new Maybe(list[0])); } } else if (source is IAsyncPartition p) { return p.TryGetFirstAsync(cancellationToken); } else { return Core(source, cancellationToken); static async ValueTask> Core(IAsyncEnumerable source, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { if (await e.MoveNextAsync()) { return new Maybe(e.Current); } } return new Maybe(); } } return new ValueTask>(new Maybe()); } private static async ValueTask> TryGetFirst(IAsyncEnumerable source, Func predicate, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var value = e.Current; if (predicate(value)) { return new Maybe(value); } } } return new Maybe(); } private static async ValueTask> TryGetFirst(IAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var value = e.Current; if (await predicate(value).ConfigureAwait(false)) { return new Maybe(value); } } } return new Maybe(); } #if !NO_DEEP_CANCELLATION private static async ValueTask> TryGetFirst(IAsyncEnumerable source, Func> predicate, CancellationToken cancellationToken) { await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) { while (await e.MoveNextAsync()) { var value = e.Current; if (await predicate(value, cancellationToken).ConfigureAwait(false)) { return new Maybe(value); } } } return new Maybe(); } #endif } }