Example #1
0
 static async Task GetSyncNoWaitIterator <T>(IEnumerable <T> source, IAsyncConsumer <T> consumer)
 {
     foreach (T item in source)
     {
         consumer.YieldAsync(item);
     }
 }
Example #2
0
 /// <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));
 }
Example #3
0
        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);
 }
Example #5
0
        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);
        }
Example #6
0
        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();
     }
 }
Example #8
0
 private Func <IMessage, Task> CreateAsyncConsumerDelegate <TIn>(IAsyncConsumer <TIn> asyncConsumer) where TIn : IMessage
 {
     return(async o => await asyncConsumer.HandleAsync((TIn)o).ConfigureAwait(false));
 }