| | | 1 | | using CounterpointCollective.Dataflow.Encapsulation; |
| | | 2 | | using CounterpointCollective.Dataflow.Fluent; |
| | | 3 | | using System.Collections.Concurrent; |
| | | 4 | | using System.Threading.Tasks.Dataflow; |
| | | 5 | | |
| | | 6 | | namespace CounterpointCollective.Dataflow |
| | | 7 | | { |
| | | 8 | | public class TeeBlock<I, T, O> : AbstractEncapsulatedPropagatorBlock<I, O> |
| | | 9 | | { |
| | | 10 | | private BoundedPropagatorBlock<I, O> b; |
| | 104 | 11 | | protected override ITargetBlock<I> TargetSide => b; |
| | | 12 | | |
| | 122 | 13 | | protected override ISourceBlock<O> SourceSide => b; |
| | | 14 | | |
| | 1 | 15 | | public int Count => b.Count; |
| | | 16 | | |
| | 2 | 17 | | public TeeBlock(IPropagatorBlock<I, T> inner, Func<I, T, O> combinator, DataflowBlockOptions options) |
| | | 18 | | { |
| | 2 | 19 | | var q = new ConcurrentQueue<I>(); |
| | | 20 | | |
| | 2 | 21 | | b = |
| | 2 | 22 | | new TransformBlock<I, I>(h => |
| | 2 | 23 | | { |
| | 102 | 24 | | q.Enqueue(h); |
| | 102 | 25 | | return h; |
| | 2 | 26 | | }, new() { CancellationToken = options.CancellationToken, SingleProducerConstrained = true }) |
| | 2 | 27 | | .Pipeline() |
| | 2 | 28 | | .LinkTo(inner, new() { PropagateCompletion = true }) |
| | 2 | 29 | | .Transform( |
| | 2 | 30 | | t => |
| | 2 | 31 | | { |
| | 102 | 32 | | if (q.TryDequeue(out var i)) |
| | 2 | 33 | | { |
| | 102 | 34 | | return combinator(i, t); |
| | 2 | 35 | | } else |
| | 2 | 36 | | { |
| | 0 | 37 | | throw new InvalidOperationException("Tee inner block must produce exactly 1 output message p |
| | 2 | 38 | | } |
| | 2 | 39 | | }, |
| | 2 | 40 | | new ExecutionDataflowBlockOptions() |
| | 2 | 41 | | { |
| | 2 | 42 | | CancellationToken = options.CancellationToken, |
| | 2 | 43 | | SingleProducerConstrained = true |
| | 2 | 44 | | } |
| | 2 | 45 | | ) |
| | 2 | 46 | | .Build() |
| | 2 | 47 | | .WithBoundedCapacity(options.BoundedCapacity); |
| | 2 | 48 | | } |
| | | 49 | | } |
| | | 50 | | } |