static async Task GetSyncNoWaitIterator <T>(IEnumerable <T> source, IAsyncConsumer <T> consumer) { foreach (T item in source) { consumer.YieldAsync(item); } }
/// <summary> /// Consume the source async sequence by emitting items and terminal signals via /// the given <see cref="IAsyncConsumer{T}" /> consumer. /// </summary> /// <typeparam name="TSource">The element type.</typeparam> /// <param name="source">The source sequence to consume.</param> /// <param name="consumer">The push-awaitable consumer.</param> /// <param name="ct">The optional cancellation token to stop the consumption.</param> /// <returns>The task that is completed when the consumption terminates.</returns> public static ValueTask Consume <TSource>(this IAsyncEnumerable <TSource> source, IAsyncConsumer <TSource> consumer, CancellationToken ct = default) { RequireNonNull(source, nameof(source)); RequireNonNull(consumer, nameof(consumer)); return(ForEach.Consume(source, consumer, ct)); }
public static async ValueTask Consume <T>(this IAsyncEnumerable <T> source, IAsyncConsumer <T> consumer, CancellationToken ct) { var en = source.GetAsyncEnumerator(); try { if (!ct.IsCancellationRequested) { try { while (await en.MoveNextAsync()) { if (ct.IsCancellationRequested) { return; } await consumer.Next(en.Current); } await consumer.Complete(); } catch (Exception ex) { await consumer.Error(ex); } } } finally { await en.DisposeAsync(); } }
internal MulticastEnumerator(IAsyncEnumerator <TSource> source, IAsyncConsumer <TSource> subject, IAsyncEnumerator <TResult> result) { _source = source; _subject = subject; _result = result; _disposeTask = new TaskCompletionSource <bool>(); Volatile.Write(ref _disposeWip, 2); }
public static List <Task> FeedFast <T>(this IAsyncConsumer <T> consumer, IEnumerable <T> source) { List <Task> tasks = new List <Task>(); foreach (var item in source) { tasks.Add(consumer.YieldAsync(item)); } return(tasks); }
private static async Task FeedAsync <T>(this IAsyncEnumerable <T> source, IAsyncConsumer <T> consumer) { if (source == null) { throw new ArgumentNullException("source"); } if (consumer == null) { throw new ArgumentNullException("consumer"); } await source.ForEachAsync(item => consumer.YieldAsync(item)); }
private static async void FillQueue <T>(IAsyncEnumerable <T> source, IAsyncConsumer <T> queue, CancellationToken cancellationToken = default) { try { await foreach (var item in source.WithCancellation(cancellationToken)) { await queue.Next(item); } } catch (Exception e) { await queue.Error(e); } finally { await queue.Complete(); } }
private Func <IMessage, Task> CreateAsyncConsumerDelegate <TIn>(IAsyncConsumer <TIn> asyncConsumer) where TIn : IMessage { return(async o => await asyncConsumer.HandleAsync((TIn)o).ConfigureAwait(false)); }