@@ -131,6 +131,138 @@ Func<Exception, Task<T>> resolutionSupplier
131
131
? resolutionSupplier ( value )
132
132
: Task . FromException < T > ( value ) ;
133
133
134
+ /// <summary>
135
+ /// Invokes <paramref name="action"/> if <paramref name="predicate"/> succeeds.
136
+ /// </summary>
137
+ /// <example>This is useful for conditionally executing a side effect.
138
+ /// <code>
139
+ /// Task.FromResult(someUrl)
140
+ /// .Then(httpClient.GetAsync)
141
+ /// .IfFulfilled(InvokeIf(
142
+ /// httpResponse => !httpResponse.IsSuccessStatusCode,
143
+ /// response => _logger.LogWarning("Got '{StatusCode}' response from server", response.StatusCode)
144
+ /// );
145
+ /// </code>
146
+ /// </example>
147
+ /// <param name="predicate">A predicate to evaluate with the <see cref="Task{T}"/>'s value.</param>
148
+ /// <param name="action">The function to invoke if <paramref name="predicate"/> returns <code>true</code>.</param>
149
+ /// <typeparam name="T">The type passed into <code>InvokeIf</code>.</typeparam>
150
+ /// <returns>A function that conditionally invokes another function.</returns>
151
+ public static Func < T , Task < T > > InvokeIf < T > (
152
+ Predicate < T > predicate ,
153
+ Action < T > action
154
+ )
155
+ {
156
+ return value =>
157
+ {
158
+ if ( predicate ( value ) )
159
+ {
160
+ action ( value ) ;
161
+ }
162
+
163
+ return Task . FromResult ( value ) ;
164
+ } ;
165
+ }
166
+
167
+ /// <summary>
168
+ /// Invokes <paramref name="func"/> if <paramref name="predicate"/> succeeds.
169
+ /// </summary>
170
+ /// <example>This is useful for conditionally executing a side effect.
171
+ /// <code>
172
+ /// Task.FromResult(someUrl)
173
+ /// .Then(httpClient.GetAsync)
174
+ /// .IfFulfilled(InvokeIf(
175
+ /// httpResponse => !httpResponse.IsSuccessStatusCode,
176
+ /// response => _logger.LogWarning("Got '{StatusCode}' response from server", response.StatusCode)
177
+ /// );
178
+ /// </code>
179
+ /// </example>
180
+ /// <param name="predicate">A predicate to evaluate with the <see cref="Task{T}"/>'s value.</param>
181
+ /// <param name="func">The function to invoke if <paramref name="predicate"/> returns <code>true</code>.</param>
182
+ /// <typeparam name="T">The type passed into <code>InvokeIf</code>.</typeparam>
183
+ /// <returns>A function that conditionally invokes another function.</returns>
184
+ public static Func < T , Task < T > > InvokeIf < T > (
185
+ Predicate < T > predicate ,
186
+ Func < T , T > func
187
+ )
188
+ {
189
+ return value =>
190
+ {
191
+ if ( predicate ( value ) )
192
+ {
193
+ return Task . FromResult ( func ( value ) ) ;
194
+ }
195
+
196
+ return Task . FromResult ( value ) ;
197
+ } ;
198
+ }
199
+
200
+ /// <summary>
201
+ /// Invokes <paramref name="func"/> if <paramref name="predicate"/> succeeds.
202
+ /// </summary>
203
+ /// <example>This is useful for conditionally executing a side effect.
204
+ /// <code>
205
+ /// Task.FromResult(someUrl)
206
+ /// .Then(httpClient.GetAsync)
207
+ /// .IfFulfilled(InvokeIf(
208
+ /// httpResponse => !httpResponse.IsSuccessStatusCode,
209
+ /// response => _logger.LogWarning("Got '{StatusCode}' response from server", response.StatusCode)
210
+ /// );
211
+ /// </code>
212
+ /// </example>
213
+ /// <param name="predicate">A predicate to evaluate with the <see cref="Task{T}"/>'s value.</param>
214
+ /// <param name="func">The function to invoke if <paramref name="predicate"/> returns <code>true</code>.</param>
215
+ /// <typeparam name="T">The type passed into <code>InvokeIf</code>.</typeparam>
216
+ /// <returns>A function that conditionally invokes another function.</returns>
217
+ public static Func < T , Task < T > > InvokeIf < T > (
218
+ Predicate < T > predicate ,
219
+ Func < T , Task < T > > func
220
+ )
221
+ {
222
+ return value =>
223
+ {
224
+ if ( predicate ( value ) )
225
+ {
226
+ return func ( value ) ;
227
+ }
228
+
229
+ return Task . FromResult ( value ) ;
230
+ } ;
231
+ }
232
+
233
+ /// <summary>
234
+ /// Invokes <paramref name="func"/> if <paramref name="predicate"/> succeeds.
235
+ /// </summary>
236
+ /// <example>This is useful for conditionally executing a side effect.
237
+ /// <code>
238
+ /// Task.FromResult(someUrl)
239
+ /// .Then(httpClient.GetAsync)
240
+ /// .IfFulfilled(InvokeIf(
241
+ /// httpResponse => !httpResponse.IsSuccessStatusCode,
242
+ /// response => _logger.LogWarning("Got '{StatusCode}' response from server", response.StatusCode)
243
+ /// );
244
+ /// </code>
245
+ /// </example>
246
+ /// <param name="predicate">A predicate to evaluate with the <see cref="Task{T}"/>'s value.</param>
247
+ /// <param name="func">The function to invoke if <paramref name="predicate"/> returns <code>true</code>.</param>
248
+ /// <typeparam name="T">The type passed into <code>InvokeIf</code>.</typeparam>
249
+ /// <returns>A function that conditionally invokes another function.</returns>
250
+ public static Func < T , Task < T > > InvokeIf < T > (
251
+ Predicate < T > predicate ,
252
+ Func < T , Task > func
253
+ )
254
+ {
255
+ return value =>
256
+ {
257
+ if ( predicate ( value ) )
258
+ {
259
+ return Task . FromResult ( value ) . Then ( func ) . Then ( _ => value ) ;
260
+ }
261
+
262
+ return Task . FromResult ( value ) ;
263
+ } ;
264
+ }
265
+
134
266
/// <summary>
135
267
/// A function that executes the <paramref name="supplier"/> after <paramref name="deferTime"/> has elapsed.
136
268
/// </summary>
0 commit comments