コード例 #1
0
        private async Task HandleSource(Task <bool> t)
        {
            if (t.IsFaulted)
            {
                if (TryDisposeSource())
                {
                    await _subject.Error(ExceptionHelper.Extract(t.Exception));
                }
            }
            else if (t.Result)
            {
                var v = _source.Current;
                if (TryDisposeSource())
                {
                    await _subject.Next(v);

                    MoveNextSource();
                }
            }
            else
            {
                if (TryDisposeSource())
                {
                    await _subject.Complete();
                }
            }
        }
コード例 #2
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();
            }
        }
コード例 #3
0
 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();
     }
 }