// 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.Threading; namespace System.Linq { /// /// Provides a set of extension methods for . /// public static partial class AsyncEnumerable { // // REVIEW: Create methods may not belong in System.Linq.Async. Async iterators can be // used to implement these interfaces. Move to System.Interactive.Async? // /// /// Creates a new enumerable using the specified delegates implementing the members of . /// /// The type of the elements returned by the enumerable sequence. /// The delegate implementing the method. /// A new enumerable instance. public static IAsyncEnumerable Create(Func> getAsyncEnumerator) { if (getAsyncEnumerator == null) throw Error.ArgumentNull(nameof(getAsyncEnumerator)); return new AnonymousAsyncEnumerable(getAsyncEnumerator); } private sealed class AnonymousAsyncEnumerable : IAsyncEnumerable { private readonly Func> _getEnumerator; public AnonymousAsyncEnumerable(Func> getEnumerator) => _getEnumerator = getEnumerator; public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior. return _getEnumerator(cancellationToken); } } } }