// 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 AsyncEnumerableEx { /// /// Returns an async-enumerable sequence that invokes the specified factory function whenever a new observer subscribes. /// /// The type of the elements in the sequence returned by the factory function, and in the resulting sequence. /// The async-enumerable factory function to invoke for each consumer that starts enumerating the resulting asynchronous sequence. /// An async-enumerable sequence whose observers trigger an invocation of the given async-enumerable factory function. /// is null. public static IAsyncEnumerable Defer(Func> factory) { if (factory == null) throw Error.ArgumentNull(nameof(factory)); return Core(factory); static async IAsyncEnumerable Core(Func> factory, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { await foreach (var item in factory().WithCancellation(cancellationToken).ConfigureAwait(false)) { yield return item; } } } /// /// Returns an async-enumerable sequence that starts the specified asynchronous factory function whenever a new observer subscribes. /// /// The type of the elements in the sequence returned by the factory function, and in the resulting sequence. /// Asynchronous factory function to start for each consumer that starts enumerating the resulting asynchronous sequence. /// An async-enumerable sequence whose observers trigger the given asynchronous async-enumerable factory function to be started. /// is null. /// This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11. public static IAsyncEnumerable Defer(Func>> factory) { if (factory == null) throw Error.ArgumentNull(nameof(factory)); return Core(factory); static async IAsyncEnumerable Core(Func>> factory, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { await foreach (var item in (await factory().ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false)) { yield return item; } } } #if !NO_DEEP_CANCELLATION /// /// Returns an async-enumerable sequence that starts the specified cancellable asynchronous factory function whenever a new observer subscribes. /// The CancellationToken passed to the asynchronous factory function is tied to the returned disposable subscription, allowing best-effort cancellation. /// /// The type of the elements in the sequence returned by the factory function, and in the resulting sequence. /// Asynchronous factory function, supporting cancellation, to start for each consumer that starts enumerating the resulting asynchronous sequence. /// An async-enumerable sequence whose observers trigger the given asynchronous async-enumerable factory function to be started. /// is null. /// This operator is especially useful in conjunction with the asynchronous programming features introduced in C# 5.0 and Visual Basic 11. /// When a subscription to the resulting sequence is disposed, the CancellationToken that was fed to the asynchronous async-enumerable factory function will be signaled. public static IAsyncEnumerable Defer(Func>> factory) { if (factory == null) throw Error.ArgumentNull(nameof(factory)); return Core(factory); static async IAsyncEnumerable Core(Func>> factory, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { await foreach (var item in (await factory(cancellationToken).ConfigureAwait(false)).WithCancellation(cancellationToken).ConfigureAwait(false)) { yield return item; } } } #endif } }