@@ -8,6 +8,58 @@ namespace Open.ChannelExtensions
88{
99 public static partial class Extensions
1010 {
11+ /// <summary>
12+ /// Reads all entries from the source channel and writes them to the target.
13+ /// This is useful for managing different buffers sizes, especially if the source reader comes from a .Transform function.
14+ /// </summary>
15+ /// <typeparam name="T">The type contained by the source channel and written to the target..</typeparam>
16+ /// <param name="source">The source channel.</param>
17+ /// <param name="target">The target channel.</param>
18+ /// <param name="cancellationToken">An optional cancellation token.</param>
19+ public static async ValueTask PipeTo < T > ( this ChannelReader < T > source ,
20+ ChannelWriter < T > target ,
21+ CancellationToken cancellationToken = default )
22+ => await source . ReadAllAsync ( e => target . WriteAsync ( e , cancellationToken ) , cancellationToken ) ;
23+
24+ /// <summary>
25+ /// Reads all entries from the source channel and writes them to the target.
26+ /// This is useful for managing different buffers sizes, especially if the source reader comes from a .Transform function.
27+ /// </summary>
28+ /// <typeparam name="T">The type contained by the source channel and written to the target..</typeparam>
29+ /// <param name="source">The source channel.</param>
30+ /// <param name="target">The target channel.</param>
31+ /// <param name="complete">Indicates to call complete on the target when the source is complete.</param>
32+ /// <param name="cancellationToken">An optional cancellation token.</param>
33+ /// <returns>The channel reader of the target.</returns>
34+ public static ChannelReader < T > PipeTo < T > ( this ChannelReader < T > source ,
35+ Channel < T > target ,
36+ bool complete ,
37+ CancellationToken cancellationToken = default )
38+ {
39+ if ( source is null ) throw new ArgumentNullException ( nameof ( source ) ) ;
40+ if ( target is null ) throw new ArgumentNullException ( nameof ( target ) ) ;
41+ Contract . EndContractBlock ( ) ;
42+
43+ _ = PipeToCore ( ) ;
44+
45+ return target . Reader ;
46+
47+ async ValueTask PipeToCore ( )
48+ {
49+ try
50+ {
51+ await PipeTo ( source , target . Writer , cancellationToken ) ;
52+ if ( complete )
53+ target . Writer . Complete ( ) ;
54+ }
55+ catch ( Exception ex )
56+ {
57+ if ( complete )
58+ target . Writer . Complete ( ex ) ;
59+ }
60+ }
61+ }
62+
1163 /// <summary>
1264 /// Reads all entries concurrently and applies the values to the provided transform function before buffering the results into another channel for consumption.
1365 /// </summary>
0 commit comments