-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExtensions._.cs
266 lines (218 loc) · 6.66 KB
/
Extensions._.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Open.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace Open.Threading.Dataflow;
public static partial class DataFlowExtensions
{
public static Task CompleteAsync(this IDataflowBlock source)
{
source.Complete();
return source.Completion;
}
public static ISourceBlock<T> Buffer<T>(this ISourceBlock<T> source, DataflowBlockOptions? dataflowBlockOptions = null)
{
if (source is null)
throw new NullReferenceException();
Contract.EndContractBlock();
var output = dataflowBlockOptions is null
? new BufferBlock<T>()
: new BufferBlock<T>(dataflowBlockOptions);
_ = source.LinkToWithCompletion(output);
return output;
}
public static ISourceBlock<T> BufferMany<T>(this ISourceBlock<T[]> source, ExecutionDataflowBlockOptions? dataflowBlockOptions = null)
{
if (source is null)
throw new NullReferenceException();
Contract.EndContractBlock();
var output = dataflowBlockOptions is null
? new TransformManyBlock<T[], T>(t => t)
: new TransformManyBlock<T[], T>(t => t, dataflowBlockOptions);
_ = source.LinkToWithCompletion(output);
return output;
}
public static ISourceBlock<T[]> Batch<T>(this ISourceBlock<T> source,
int batchSize,
GroupingDataflowBlockOptions? dataflowBlockOptions = null)
{
if (source is null)
throw new NullReferenceException();
Contract.EndContractBlock();
var batchBlock = dataflowBlockOptions is null
? new BatchBlock<T>(batchSize)
: new BatchBlock<T>(batchSize, dataflowBlockOptions);
_ = source.LinkToWithCompletion(batchBlock);
return batchBlock;
}
public static int ToTargetBlock<T>(this IEnumerable<T> source,
ITargetBlock<T> target)
{
if (source is null)
throw new NullReferenceException();
if (target is null)
throw new ArgumentNullException(nameof(target));
Contract.EndContractBlock();
var count = 0;
foreach (var entry in source)
{
if (!target.Post(entry))
break;
count++;
}
return count;
}
public static Task<int> ToTargetBlockAsync<T>(this IEnumerable<T> source,
ITargetBlock<T> target,
CancellationToken cancellationToken = default)
{
if (source is null)
throw new NullReferenceException();
if (target is null)
throw new ArgumentNullException(nameof(target));
Contract.EndContractBlock();
return source is ICollection<T> c && c.Count == 0
? Task.FromResult(0)
: ToTargetBlockAsyncCore();
async Task<int> ToTargetBlockAsyncCore()
{
var count = 0;
foreach (var entry in source)
{
if (cancellationToken.IsCancellationRequested)
break;
if (!target.Post(entry) && !await target.SendAsync(entry, cancellationToken).ConfigureAwait(false))
break;
count++;
}
cancellationToken.ThrowIfCancellationRequested();
return count;
}
}
public static async Task<List<T>> ToListAsync<T>(this IReceivableSourceBlock<T> source,
CancellationToken cancellationToken = default)
{
if (source is null)
throw new NullReferenceException();
Contract.EndContractBlock();
var result = new List<T>();
do
{
while (!cancellationToken.IsCancellationRequested
&& source.TryReceive(null, out var e))
{
result.Add(e);
}
}
while (!cancellationToken.IsCancellationRequested
&& await source.OutputAvailableAsync(cancellationToken).ConfigureAwait(false));
cancellationToken.ThrowIfCancellationRequested();
return result;
}
public static ISourceBlock<T> AsBufferBlock<T>(this IEnumerable<T> source,
int capacity = DataflowBlockOptions.Unbounded,
CancellationToken cancellationToken = default)
{
if (source is null)
throw new NullReferenceException();
Contract.EndContractBlock();
var buffer = new BufferBlock<T>(new DataflowBlockOptions()
{
BoundedCapacity = capacity,
CancellationToken = cancellationToken
});
var result = ToTargetBlockAsync(source, buffer, cancellationToken);
if (result.IsCompleted && !result.IsFaulted && !result.IsCanceled)
{
buffer.Complete();
}
else
{
_ = CallCompleteWhenFinished();
async Task CallCompleteWhenFinished()
{
_ = await result.ConfigureAwait(false);
buffer.Complete();
}
}
return buffer;
}
public static async Task<int> AllLinesTo(this TextReader source,
ITargetBlock<string> target,
bool completeAndAwait = false,
CancellationToken cancellationToken = default)
{
if (source is null)
throw new NullReferenceException();
Contract.EndContractBlock();
var count = 0;
string? line;
while (!cancellationToken.IsCancellationRequested
&& (line = await source.ReadLineAsync().ConfigureAwait(false)) is not null)
{
if (!target.Post(line) && !await target.SendAsync(line, cancellationToken).ConfigureAwait(false))
break;
count++;
}
cancellationToken.ThrowIfCancellationRequested();
if (completeAndAwait)
await target.CompleteAsync().ConfigureAwait(false);
return count;
}
public static async Task<int> AllLinesTo<T>(this TextReader source,
ITargetBlock<T> target,
Func<string, T> transform,
bool completeAndAwait = false,
CancellationToken cancellationToken = default)
{
if (source is null)
throw new NullReferenceException();
Contract.EndContractBlock();
var count = 0;
string? line;
while (!cancellationToken.IsCancellationRequested
&& (line = await source.ReadLineAsync().ConfigureAwait(false)) is not null)
{
var e = transform(line);
if (!target.Post(e) && !await target.SendAsync(e, cancellationToken).ConfigureAwait(false))
break;
count++;
}
cancellationToken.ThrowIfCancellationRequested();
if (completeAndAwait)
await target.CompleteAsync().ConfigureAwait(false);
return count;
}
public static T OnComplete<T>(this T source, Action oncomplete)
where T : IDataflowBlock
{
_ = source.Completion.ContinueWith(_ => oncomplete());
return source;
}
public static T OnComplete<T>(this T source, Action<Task> oncomplete)
where T : IDataflowBlock
{
_ = source.Completion.ContinueWith(oncomplete);
return source;
}
public static T OnCompletedSuccessfully<T>(this T source, Action oncomplete)
where T : IDataflowBlock
{
_ = source.Completion.OnFullfilled(oncomplete);
return source;
}
public static T OnFault<T>(this T source, Action<Exception> onfault)
where T : IDataflowBlock
{
_ = source.Completion.OnFaulted(ex => onfault(ex.InnerException ?? ex));
return source;
}
public static void Fault(this IDataflowBlock target, string message)
=> target.Fault(new Exception(message));
public static void Fault(this IDataflowBlock target, string message, Exception innerException)
=> target.Fault(new Exception(message, innerException));
}