| | | 1 | | using System.Threading.Tasks.Dataflow; |
| | | 2 | | |
| | | 3 | | namespace CounterpointCollective.Dataflow |
| | | 4 | | { |
| | | 5 | | public static class BoundedPropagatorBlockExtensions |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Wraps an <see cref="IPropagatorBlock{I,O}"/> block into a <see cref="BoundedPropagatorBlock{I,O}"/> |
| | | 9 | | /// with the specified <paramref name="boundedCapacity"/>. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <typeparam name="I">The type of input messages accepted by the propagator block.</typeparam> |
| | | 12 | | /// <typeparam name="O">The type of output messages produced by the propagator block.</typeparam> |
| | | 13 | | /// <param name="b">The source propagator block to wrap with bounded capacity.</param> |
| | | 14 | | /// <param name="boundedCapacity">The maximum number of messages that can be buffered by the block.</param> |
| | | 15 | | /// <returns>A <see cref="BoundedPropagatorBlock{I,O}"/> that wraps the specified block <paramref name="b"/> and |
| | | 16 | | /// limit <paramref name="boundedCapacity"/>.</returns> |
| | | 17 | | /// <example> |
| | | 18 | | /// <code> |
| | | 19 | | /// |
| | | 20 | | /// // Create a custom block |
| | | 21 | | /// var myBlock = DataflowBlock.Encapsulate( |
| | | 22 | | /// new TransformBlock<int, int>(a => a + 1), |
| | | 23 | | /// new TransformBlock<int, int>(a => a - 1) |
| | | 24 | | /// ); |
| | | 25 | | /// |
| | | 26 | | /// // Wrap your custom block with bounded capacity |
| | | 27 | | /// var boundedBlock = myBlock.WithBoundedCapacity(3); |
| | | 28 | | /// |
| | | 29 | | /// //You can count how many messages are currently owned by the block |
| | | 30 | | /// var c = boundedBlock.Count; |
| | | 31 | | /// |
| | | 32 | | /// //You can also dynamically change the capacity |
| | | 33 | | /// boundedBlock.SetBoundedCapacity(10); |
| | | 34 | | /// </code> |
| | | 35 | | /// </example> |
| | | 36 | | public static BoundedPropagatorBlock<I, O> WithBoundedCapacity<I, O>(this IPropagatorBlock<I, O> b, int boundedC |
| | 2 | 37 | | => new (b, boundedCapacity); |
| | | 38 | | } |
| | | 39 | | } |