public static Task Collect <T>(this IFlow <T> flow, Func <T, CancellationToken, Task> asyncCollector, CancellationToken cancellationToken = default) { var collector = new FlowCollector <T>(asyncCollector); return(flow.Collect(collector, cancellationToken)); }
public static IFlow <R> Map <T, R>(this IFlow <T> source, Func <T, R> mapper) { return(FlowFactory.Flow <R>((collector, cancellationToken) => source.Collect( item => collector.Emit(mapper(item)), cancellationToken ) )); }
public static IFlow <T> OnNext <T>(this IFlow <T> source, Action <T> action) { return(FlowFactory.Flow <T>((collector, cancellationToken) => source.Collect(item => { action(item); collector.Emit(item); }, cancellationToken) )); }
public static IAsyncEnumerable <T> CollectEnumerable <T>(this IFlow <T> flow, CancellationToken cancellationToken = default) { var collector = new FlowCollectorEnumerator <T>(); cancellationToken.Register(async() => await collector.Finish()); flow .Collect(collector, cancellationToken) .ContinueWith(_ => collector.Finish(), cancellationToken); return(new FlowEnumerableAdapter <T>(collector)); }
public static Task Collect <T>(this IFlow <T> flow, Action <T> collectorAction, CancellationToken cancellationToken = default) { var collector = new FlowCollector <T>((item, _) => { collectorAction(item); return(Task.CompletedTask); }); return(flow.Collect(collector, cancellationToken)); }
public static IFlow <T> Filter <T>(this IFlow <T> source, Func <T, bool> predicate) { return(FlowFactory.Flow <T>((collector, cancellationToken) => source.Collect(item => { if (predicate(item)) { collector.Emit(item); } }, cancellationToken) )); }