public static Producer <TOut> Transform <TIn, TOut>(this Producer <TIn> producer, Func <TIn, CancellationToken, ValueTask <TOut> > transform, ChannelOptions?options = null) { var actor = new TransformActor <TIn, TOut>(transform); producer.LinkTo(actor.Inbox, options); actor.Start(); return(actor.Outbox); }
public static async IAsyncEnumerable <T> ToIAsyncEnumerable <T>(this Producer <T> producer, [EnumeratorCancellation] CancellationToken cancellationToken = default, ChannelOptions?options = null) { using var consumer = new Consumer <T>(); producer.LinkTo(consumer, options); await foreach (var item in consumer.ReadAllAsync(cancellationToken)) { yield return(item); } }
public static Producer <T> RegulatePressure <T, TFeedback>(this Producer <T> producer, int limit, Producer <TFeedback> feedback, Func <int, int, TFeedback, int>?regulate = null, ChannelOptions?options = null) { regulate ??= (pressure, _2, _3) => pressure - 1; var actor = new PressureControlActor <T, TFeedback>(limit, regulate); producer.LinkTo(actor.Inbox, options); feedback.LinkTo(actor.Feedback, options); actor.Start(); return(actor.Outbox); }
public static async Task <IList <T> > ToListAsync <T>(this Producer <T> producer, CancellationToken cancellationToken = default, ChannelOptions?options = null) { using var consumer = new Consumer <T>(); producer.LinkTo(consumer, options); var result = new List <T>(); await foreach (var item in consumer.ReadAllAsync(cancellationToken)) { result.Add(item); } return(result); }
public static async IAsyncEnumerable <WindowProducer <T> > Window <T>(this Producer <T> producer, int size = int.MaxValue, TimeSpan?duration = null, ChannelOptions?options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) { using var actor = new WindowActor <T>(size, duration); producer.LinkTo(actor.Inbox, options); using var consumer = new Consumer <WindowProducer <T> >(); actor.Outbox.LinkTo(consumer); actor.Start(); await foreach (var window in consumer.ReadAllAsync(cancellationToken)) { yield return(window); } await actor; }
public static async IAsyncEnumerable <GroupProducer <TKey, T> > GroupBy <TKey, T>(this Producer <T> producer, Func <T, TKey> selector, ChannelOptions?options = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) { var actor = new GroupByActor <TKey, T>(selector); producer.LinkTo(actor.Inbox, options); var consumer = new Consumer <GroupProducer <TKey, T> >(); actor.Outbox.LinkTo(consumer); actor.Start(); await foreach (var group in consumer.ReadAllAsync(cancellationToken)) { yield return(group); } }
public static Task Consume <T>(this Producer <T> producer, Func <T, ValueTask> consumer, ChannelOptions?options = null) { var actor = new ConsumerActor <T>(async(reader, ct) => { await foreach (var item in reader.ReadAllAsync(ct)) { await consumer(item); } }); producer.LinkTo(actor.Inbox, options); actor.Start(); return(actor.Completion); }
public static Producer <TOut> Zip <TIn, TOther, TOut>(this Producer <TIn> producer, Producer <TOther> other, Func <TIn, TOther, TOut> action) { var actor = new TransformActor <TIn, TOut>(async(readerIn, writer, ct) => { using var consumer = new Consumer <TOther>(); other.LinkTo(consumer); await writer.WriteAsync( action( await readerIn.ReadAsync(ct), await consumer.ReadAsync(ct) ), ct ); }); return(actor.Outbox); }
public static Producer <T> PauseIfEmpty <T>(this Producer <T> producer, TimeSpan pause, ChannelOptions?options = null) { var actor = new TransformActor <T, T>(async(reader, writer, cancellationToken) => { while (!cancellationToken.IsCancellationRequested) { while (reader.TryRead(out var item)) { await writer.WriteAsync(item, cancellationToken); } await Task.Delay(pause); if (!await reader.WaitToReadAsync(cancellationToken)) { break; } } }); producer.LinkTo(actor.Inbox, options); return(actor.Outbox); }