|
| 1 | +<!DOCTYPE html> |
| 2 | +<!--[if IE]><![endif]--> |
| 3 | +<html> |
| 4 | + |
| 5 | + <head> |
| 6 | + <meta charset="utf-8"> |
| 7 | + <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
| 8 | + <title>Open.ChannelExtensions </title> |
| 9 | + <meta name="viewport" content="width=device-width"> |
| 10 | + <meta name="title" content="Open.ChannelExtensions "> |
| 11 | + <meta name="generator" content="docfx 2.47.0.0"> |
| 12 | + |
| 13 | + <link rel="shortcut icon" href="favicon.ico"> |
| 14 | + <link rel="stylesheet" href="styles/docfx.vendor.css"> |
| 15 | + <link rel="stylesheet" href="styles/docfx.css"> |
| 16 | + <link rel="stylesheet" href="styles/main.css"> |
| 17 | + <meta property="docfx:navrel" content=""> |
| 18 | + <meta property="docfx:tocrel" content=""> |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + </head> |
| 23 | + <body data-spy="scroll" data-target="#affix" data-offset="120"> |
| 24 | + <div id="wrapper"> |
| 25 | + <header> |
| 26 | + |
| 27 | + <nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation"> |
| 28 | + <div class="container"> |
| 29 | + <div class="navbar-header"> |
| 30 | + <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar"> |
| 31 | + <span class="sr-only">Toggle navigation</span> |
| 32 | + <span class="icon-bar"></span> |
| 33 | + <span class="icon-bar"></span> |
| 34 | + <span class="icon-bar"></span> |
| 35 | + </button> |
| 36 | + |
| 37 | + <a class="navbar-brand" href="index.html"> |
| 38 | + <img id="logo" class="svg" src="logo.svg" alt=""> |
| 39 | + </a> |
| 40 | + </div> |
| 41 | + <div class="collapse navbar-collapse" id="navbar"> |
| 42 | + <form class="navbar-form navbar-right" role="search" id="search"> |
| 43 | + <div class="form-group"> |
| 44 | + <input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off"> |
| 45 | + </div> |
| 46 | + </form> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + </nav> |
| 50 | + |
| 51 | + <div class="subnav navbar navbar-default"> |
| 52 | + <div class="container hide-when-search" id="breadcrumb"> |
| 53 | + <ul class="breadcrumb"> |
| 54 | + <li></li> |
| 55 | + </ul> |
| 56 | + </div> |
| 57 | + </div> |
| 58 | + </header> |
| 59 | + <div role="main" class="container body-content hide-when-search"> |
| 60 | + <div class="article row grid"> |
| 61 | + <div class="col-md-10"> |
| 62 | + <article class="content wrap" id="_content" data-uid=""> |
| 63 | +<h1 id="openchannelextensions">Open.ChannelExtensions</h1> |
| 64 | + |
| 65 | +<p>A set of extensions for optimizing/simplifying System.Threading.Channels usage.</p> |
| 66 | +<h2 id="highlights">Highlights</h2> |
| 67 | +<p>Being able to define an asynchronous pipeline with best practice usage using simple expressive syntax:</p> |
| 68 | +<pre><code class="lang-cs">await Channel |
| 69 | + .CreateBounded<T>(10) |
| 70 | + .SourceAsync(source /* IEnumerable<Task<T>> */) |
| 71 | + .PipeAsync( |
| 72 | + maxConcurrency: 2, |
| 73 | + capacity: 5, |
| 74 | + transform: asyncTransform01) |
| 75 | + .Pipe(transform02, /* capacity */ 3) |
| 76 | + .ReadAllAsync(finalTransformedValue => { |
| 77 | + // Do something async with each final value. |
| 78 | + }); |
| 79 | +</code></pre><pre><code class="lang-cs">await source /* IEnumerable<T> */ |
| 80 | + .ToChannel(boundedSize: 10, singleReader: true) |
| 81 | + .PipeAsync(asyncTransform01, /* capacity */ 5) |
| 82 | + .Pipe( |
| 83 | + maxConcurrency: 2, |
| 84 | + capacity: 3, |
| 85 | + transform: transform02) |
| 86 | + .ReadAll(finalTransformedValue => { |
| 87 | + // Do something with each final value. |
| 88 | + }); |
| 89 | +</code></pre><h2 id="examples">Examples</h2> |
| 90 | +<h3 id="reading-until-the-channel-is-closed">Reading (until the channel is closed)</h3> |
| 91 | +<h4 id="one-by-one-read-each-entry-from-the-channel">One by one read each entry from the channel</h4> |
| 92 | +<pre><code class="lang-cs">await channel.ReadAll( |
| 93 | + entry => { /* Processing Code */ }); |
| 94 | +</code></pre><pre><code class="lang-cs">await channel.ReadAll( |
| 95 | + (entry, index) => { /* Processing Code */ }); |
| 96 | +</code></pre><pre><code class="lang-cs">await channel.ReadAllAsync( |
| 97 | + async entry => { await /* Processing Code */ }); |
| 98 | +</code></pre><pre><code class="lang-cs">await channel.ReadAllAsync( |
| 99 | + async (entry, index) => { await /* Processing Code */ }); |
| 100 | +</code></pre><h4 id="read-concurrently-each-entry-from-the-channel">Read concurrently each entry from the channel</h4> |
| 101 | +<pre><code class="lang-cs">await channel.ReadAllConcurrently( |
| 102 | + maxConcurrency, |
| 103 | + entry => { /* Processing Code */ }); |
| 104 | +</code></pre><pre><code class="lang-cs">await channel.ReadAllConcurrentlyAsync( |
| 105 | + maxConcurrency, |
| 106 | + async entry => { await /* Processing Code */ }); |
| 107 | +</code></pre><h3 id="writing">Writing</h3> |
| 108 | +<p>If <code>complete</code> is <code>true</code>, the channel will be closed when the source is empty.</p> |
| 109 | +<h4 id="dump-a-source-enumeration-into-the-channel">Dump a source enumeration into the channel</h4> |
| 110 | +<pre><code class="lang-cs">// source can be any IEnumerable<T>. |
| 111 | +await channel.WriteAll(source, complete: true); |
| 112 | +</code></pre><pre><code class="lang-cs">// source can be any IEnumerable<Task<T>> or IEnumerable<ValueTask<T>>. |
| 113 | +await channel.WriteAllAsync(source, complete: true); |
| 114 | +</code></pre><h4 id="synchronize-reading-from-the-source-and-process-the-results-concurrently">Synchronize reading from the source and process the results concurrently</h4> |
| 115 | +<pre><code class="lang-cs">// source can be any IEnumerable<Task<T>> or IEnumerable<ValueTask<T>>. |
| 116 | +await channel.WriteAllConcurrentlyAsync( |
| 117 | + maxConcurrency, source, complete: true); |
| 118 | +</code></pre><h3 id="pipelining--transforming">Pipelining / Transforming</h3> |
| 119 | +<h4 id="transform-and-buffer-entries">Transform and buffer entries</h4> |
| 120 | +<pre><code class="lang-cs">// Transform values in a source channel to new unbounded channel. |
| 121 | +var transformed = channel.Pipe( |
| 122 | + async value => /* transformation */); |
| 123 | +</code></pre><pre><code class="lang-cs">// Transform values in a source channel to new unbounded channel with a max concurrency of X. |
| 124 | +const X = 4; |
| 125 | +var transformed = channel.Pipe( |
| 126 | + X, async value => /* transformation */); |
| 127 | +</code></pre><pre><code class="lang-cs">// Transform values in a source channel to new bounded channel bound of N entries. |
| 128 | +const N = 5; |
| 129 | +var transformed = channel.Pipe( |
| 130 | + async value => /* transformation */, N); |
| 131 | +</code></pre><pre><code class="lang-cs">// Transform values in a source channel to new bounded channel bound of N entries with a max concurrency of X. |
| 132 | +const X = 4; |
| 133 | +const N = 5; |
| 134 | +var transformed = channel.Pipe( |
| 135 | + X, async value => /* transformation */, N); |
| 136 | + |
| 137 | +// or |
| 138 | +transformed = channel.Pipe( |
| 139 | + maxConcurrency: X, |
| 140 | + capacity: N, |
| 141 | + transform: async value => /* transformation */); |
| 142 | +</code></pre></article> |
| 143 | + </div> |
| 144 | + |
| 145 | + <div class="hidden-sm col-md-2" role="complementary"> |
| 146 | + <div class="sideaffix"> |
| 147 | + <div class="contribution"> |
| 148 | + <ul class="nav"> |
| 149 | + <li> |
| 150 | + <a href="https://github.com/electricessence/Open.ChannelExtensions/blob/master/Open.ChannelExtensions/README.md/#L1" class="contribution-link">Improve this Doc</a> |
| 151 | + </li> |
| 152 | + </ul> |
| 153 | + </div> |
| 154 | + <nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix"> |
| 155 | + <!-- <p><a class="back-to-top" href="#top">Back to top</a><p> --> |
| 156 | + </nav> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + |
| 162 | + <footer> |
| 163 | + <div class="grad-bottom"></div> |
| 164 | + <div class="footer"> |
| 165 | + <div class="container"> |
| 166 | + <span class="pull-right"> |
| 167 | + <a href="#top">Back to top</a> |
| 168 | + </span> |
| 169 | + |
| 170 | + <span>Generated by <strong>DocFX</strong></span> |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + </footer> |
| 174 | + </div> |
| 175 | + |
| 176 | + <script type="text/javascript" src="styles/docfx.vendor.js"></script> |
| 177 | + <script type="text/javascript" src="styles/docfx.js"></script> |
| 178 | + <script type="text/javascript" src="styles/main.js"></script> |
| 179 | + </body> |
| 180 | +</html> |
0 commit comments