Do.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. // REVIEW: Should we convert Task-based overloads to ValueTask?
  12. /// <summary>
  13. /// Invokes an action for each element in the async-enumerable sequence, and propagates all observer messages through the result sequence.
  14. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  15. /// </summary>
  16. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  17. /// <param name="source">Source sequence.</param>
  18. /// <param name="onNext">Action to invoke for each element in the async-enumerable sequence.</param>
  19. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  20. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> is null.</exception>
  21. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext)
  22. {
  23. if (source == null)
  24. throw Error.ArgumentNull(nameof(source));
  25. if (onNext == null)
  26. throw Error.ArgumentNull(nameof(onNext));
  27. return DoCore(source, onNext: onNext, onError: null, onCompleted: null);
  28. }
  29. /// <summary>
  30. /// Invokes an action for each element in the async-enumerable sequence and invokes an action upon graceful termination of the async-enumerable sequence.
  31. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  32. /// </summary>
  33. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  34. /// <param name="source">Source sequence.</param>
  35. /// <param name="onNext">Action to invoke for each element in the async-enumerable sequence.</param>
  36. /// <param name="onCompleted">Action to invoke upon graceful termination of the async-enumerable sequence.</param>
  37. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  38. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onCompleted"/> is null.</exception>
  39. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action onCompleted)
  40. {
  41. if (source == null)
  42. throw Error.ArgumentNull(nameof(source));
  43. if (onNext == null)
  44. throw Error.ArgumentNull(nameof(onNext));
  45. if (onCompleted == null)
  46. throw Error.ArgumentNull(nameof(onCompleted));
  47. return DoCore(source, onNext: onNext, onError: null, onCompleted: onCompleted);
  48. }
  49. /// <summary>
  50. /// Invokes an action for each element in the async-enumerable sequence and invokes an action upon exceptional termination of the async-enumerable sequence.
  51. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  52. /// </summary>
  53. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  54. /// <param name="source">Source sequence.</param>
  55. /// <param name="onNext">Action to invoke for each element in the async-enumerable sequence.</param>
  56. /// <param name="onError">Action to invoke upon exceptional termination of the async-enumerable sequence.</param>
  57. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  58. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onError"/> is null.</exception>
  59. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError)
  60. {
  61. if (source == null)
  62. throw Error.ArgumentNull(nameof(source));
  63. if (onNext == null)
  64. throw Error.ArgumentNull(nameof(onNext));
  65. if (onError == null)
  66. throw Error.ArgumentNull(nameof(onError));
  67. return DoCore(source, onNext: onNext, onError: onError, onCompleted: null);
  68. }
  69. /// <summary>
  70. /// Invokes an action for each element in the async-enumerable sequence and invokes an action upon graceful or exceptional termination of the async-enumerable sequence.
  71. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  72. /// </summary>
  73. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  74. /// <param name="source">Source sequence.</param>
  75. /// <param name="onNext">Action to invoke for each element in the async-enumerable sequence.</param>
  76. /// <param name="onError">Action to invoke upon exceptional termination of the async-enumerable sequence.</param>
  77. /// <param name="onCompleted">Action to invoke upon graceful termination of the async-enumerable sequence.</param>
  78. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  79. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onError"/> or <paramref name="onCompleted"/> is null.</exception>
  80. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception> onError, Action onCompleted)
  81. {
  82. if (source == null)
  83. throw Error.ArgumentNull(nameof(source));
  84. if (onNext == null)
  85. throw Error.ArgumentNull(nameof(onNext));
  86. if (onError == null)
  87. throw Error.ArgumentNull(nameof(onError));
  88. if (onCompleted == null)
  89. throw Error.ArgumentNull(nameof(onCompleted));
  90. return DoCore(source, onNext, onError, onCompleted);
  91. }
  92. /// <summary>
  93. /// Invokes and awaits an asynchronous action for each element in the async-enumerable sequence, and propagates all observer messages through the result sequence.
  94. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  95. /// </summary>
  96. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  97. /// <param name="source">Source sequence.</param>
  98. /// <param name="onNext">Action to invoke and await for each element in the async-enumerable sequence.</param>
  99. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  100. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> is null.</exception>
  101. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task> onNext)
  102. {
  103. if (source == null)
  104. throw Error.ArgumentNull(nameof(source));
  105. if (onNext == null)
  106. throw Error.ArgumentNull(nameof(onNext));
  107. return DoCore(source, onNext: onNext, onError: null, onCompleted: null);
  108. }
  109. /// <summary>
  110. /// Invokes and awaits an asynchronous action for each element in the async-enumerable sequence, then invokes and awaits an asynchronous an action upon graceful termination of the async-enumerable sequence.
  111. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  112. /// </summary>
  113. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  114. /// <param name="source">Source sequence.</param>
  115. /// <param name="onNext">Action to invoke and await for each element in the async-enumerable sequence.</param>
  116. /// <param name="onCompleted">Action to invoke and await upon graceful termination of the async-enumerable sequence.</param>
  117. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  118. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onCompleted"/> is null.</exception>
  119. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task> onNext, Func<Task> onCompleted)
  120. {
  121. if (source == null)
  122. throw Error.ArgumentNull(nameof(source));
  123. if (onNext == null)
  124. throw Error.ArgumentNull(nameof(onNext));
  125. if (onCompleted == null)
  126. throw Error.ArgumentNull(nameof(onCompleted));
  127. return DoCore(source, onNext: onNext, onError: null, onCompleted: onCompleted);
  128. }
  129. /// <summary>
  130. /// Invokes and awaits an asynchronous action for each element in the async-enumerable sequence, then invokes and awaits an asynchronous action upon exceptional termination of the async-enumerable sequence.
  131. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  132. /// </summary>
  133. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  134. /// <param name="source">Source sequence.</param>
  135. /// <param name="onNext">Action to invoke and await for each element in the async-enumerable sequence.</param>
  136. /// <param name="onError">Action to invoke and await upon exceptional termination of the async-enumerable sequence.</param>
  137. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  138. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onError"/> is null.</exception>
  139. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task> onNext, Func<Exception, Task> onError)
  140. {
  141. if (source == null)
  142. throw Error.ArgumentNull(nameof(source));
  143. if (onNext == null)
  144. throw Error.ArgumentNull(nameof(onNext));
  145. if (onError == null)
  146. throw Error.ArgumentNull(nameof(onError));
  147. return DoCore(source, onNext: onNext, onError: onError, onCompleted: null);
  148. }
  149. /// <summary>
  150. /// Invokes and awaits an asynchronous action for each element in the async-enumerable sequence, then invokes and awaits an asynchronous action upon graceful or exceptional termination of the async-enumerable sequence.
  151. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  152. /// </summary>
  153. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  154. /// <param name="source">Source sequence.</param>
  155. /// <param name="onNext">Action to invoke and await for each element in the async-enumerable sequence.</param>
  156. /// <param name="onError">Action to invoke and await upon exceptional termination of the async-enumerable sequence.</param>
  157. /// <param name="onCompleted">Action to invoke and await upon graceful termination of the async-enumerable sequence.</param>
  158. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  159. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onError"/> or <paramref name="onCompleted"/> is null.</exception>
  160. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task> onNext, Func<Exception, Task> onError, Func<Task> onCompleted)
  161. {
  162. if (source == null)
  163. throw Error.ArgumentNull(nameof(source));
  164. if (onNext == null)
  165. throw Error.ArgumentNull(nameof(onNext));
  166. if (onError == null)
  167. throw Error.ArgumentNull(nameof(onError));
  168. if (onCompleted == null)
  169. throw Error.ArgumentNull(nameof(onCompleted));
  170. return DoCore(source, onNext, onError, onCompleted);
  171. }
  172. #if !NO_DEEP_CANCELLATION
  173. /// <summary>
  174. /// Invokes and awaits an asynchronous (cancellable) action for each element in the async-enumerable sequence, and propagates all observer messages through the result sequence.
  175. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  176. /// </summary>
  177. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  178. /// <param name="source">Source sequence.</param>
  179. /// <param name="onNext">Action to invoke and await for each element in the async-enumerable sequence while supporting cancellation.</param>
  180. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  181. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> is null.</exception>
  182. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> onNext)
  183. {
  184. if (source == null)
  185. throw Error.ArgumentNull(nameof(source));
  186. if (onNext == null)
  187. throw Error.ArgumentNull(nameof(onNext));
  188. return DoCore(source, onNext: onNext, onError: null, onCompleted: null);
  189. }
  190. /// <summary>
  191. /// Invokes and awaits an asynchronous (cancellable) action for each element in the async-enumerable sequence, then invokes and awaits an asynchronous (cancellable) an action upon graceful termination of the async-enumerable sequence.
  192. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  193. /// </summary>
  194. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  195. /// <param name="source">Source sequence.</param>
  196. /// <param name="onNext">Action to invoke and await for each element in the async-enumerable sequence while supporting cancellation.</param>
  197. /// <param name="onCompleted">Action to invoke and await upon graceful termination of the async-enumerable sequence while supporting cancellation.</param>
  198. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  199. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onCompleted"/> is null.</exception>
  200. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> onNext, Func<CancellationToken, Task> onCompleted)
  201. {
  202. if (source == null)
  203. throw Error.ArgumentNull(nameof(source));
  204. if (onNext == null)
  205. throw Error.ArgumentNull(nameof(onNext));
  206. if (onCompleted == null)
  207. throw Error.ArgumentNull(nameof(onCompleted));
  208. return DoCore(source, onNext: onNext, onError: null, onCompleted: onCompleted);
  209. }
  210. /// <summary>
  211. /// Invokes and awaits an asynchronous (cancellable) action for each element in the async-enumerable sequence, then invokes and awaits an asynchronous (cancellable) action upon exceptional termination of the async-enumerable sequence.
  212. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  213. /// </summary>
  214. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  215. /// <param name="source">Source sequence.</param>
  216. /// <param name="onNext">Action to invoke and await for each element in the async-enumerable sequence while supporting cancellation.</param>
  217. /// <param name="onError">Action to invoke and await upon exceptional termination of the async-enumerable sequence while supporting cancellation.</param>
  218. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  219. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onError"/> is null.</exception>
  220. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> onNext, Func<Exception, CancellationToken, Task> onError)
  221. {
  222. if (source == null)
  223. throw Error.ArgumentNull(nameof(source));
  224. if (onNext == null)
  225. throw Error.ArgumentNull(nameof(onNext));
  226. if (onError == null)
  227. throw Error.ArgumentNull(nameof(onError));
  228. return DoCore(source, onNext: onNext, onError: onError, onCompleted: null);
  229. }
  230. /// <summary>
  231. /// Invokes and awaits an asynchronous (cancellable) action for each element in the async-enumerable sequence, then invokes and awaits an asynchronous (cancellable) action upon graceful or exceptional termination of the async-enumerable sequence.
  232. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  233. /// </summary>
  234. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  235. /// <param name="source">Source sequence.</param>
  236. /// <param name="onNext">Action to invoke and await for each element in the async-enumerable sequence while supporting cancellation.</param>
  237. /// <param name="onError">Action to invoke and await upon exceptional termination of the async-enumerable sequence while supporting cancellation.</param>
  238. /// <param name="onCompleted">Action to invoke and await upon graceful termination of the async-enumerable sequence while supporting cancellation.</param>
  239. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  240. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="onNext"/> or <paramref name="onError"/> or <paramref name="onCompleted"/> is null.</exception>
  241. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> onNext, Func<Exception, CancellationToken, Task> onError, Func<CancellationToken, Task> onCompleted)
  242. {
  243. if (source == null)
  244. throw Error.ArgumentNull(nameof(source));
  245. if (onNext == null)
  246. throw Error.ArgumentNull(nameof(onNext));
  247. if (onError == null)
  248. throw Error.ArgumentNull(nameof(onError));
  249. if (onCompleted == null)
  250. throw Error.ArgumentNull(nameof(onCompleted));
  251. return DoCore(source, onNext, onError, onCompleted);
  252. }
  253. #endif
  254. /// <summary>
  255. /// Invokes the observer's methods for each message in the source async-enumerable sequence.
  256. /// This method can be used for debugging, logging, etc. of query behavior by intercepting the message stream to run arbitrary actions for messages on the pipeline.
  257. /// </summary>
  258. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  259. /// <param name="source">Source sequence.</param>
  260. /// <param name="observer">Observer whose methods to invoke as part of the source sequence's observation.</param>
  261. /// <returns>The source sequence with the side-effecting behavior applied.</returns>
  262. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="observer"/> is null.</exception>
  263. public static IAsyncEnumerable<TSource> Do<TSource>(this IAsyncEnumerable<TSource> source, IObserver<TSource> observer)
  264. {
  265. if (source == null)
  266. throw Error.ArgumentNull(nameof(source));
  267. if (observer == null)
  268. throw Error.ArgumentNull(nameof(observer));
  269. return DoCore(source, new Action<TSource>(observer.OnNext), new Action<Exception>(observer.OnError), new Action(observer.OnCompleted));
  270. }
  271. private static IAsyncEnumerable<TSource> DoCore<TSource>(IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception>? onError, Action? onCompleted)
  272. {
  273. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  274. return Core(source, onNext, onError, onCompleted);
  275. // TODO: Can remove local function.
  276. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Action<TSource> onNext, Action<Exception>? onError, Action? onCompleted, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  277. #else
  278. return AsyncEnumerable.Create(Core);
  279. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  280. #endif
  281. {
  282. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  283. while (true)
  284. {
  285. TSource item;
  286. try
  287. {
  288. if (!await e.MoveNextAsync())
  289. {
  290. break;
  291. }
  292. item = e.Current;
  293. onNext(item);
  294. }
  295. catch (OperationCanceledException)
  296. {
  297. throw;
  298. }
  299. catch (Exception ex) when (onError != null)
  300. {
  301. onError(ex);
  302. throw;
  303. }
  304. yield return item;
  305. }
  306. onCompleted?.Invoke();
  307. }
  308. }
  309. private static IAsyncEnumerable<TSource> DoCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, Task> onNext, Func<Exception, Task>? onError, Func<Task>? onCompleted)
  310. {
  311. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  312. return Core(source, onNext, onError, onCompleted);
  313. // TODO: Can remove local function.
  314. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, Task> onNext, Func<Exception, Task>? onError, Func<Task>? onCompleted, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  315. #else
  316. return AsyncEnumerable.Create(Core);
  317. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  318. #endif
  319. {
  320. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  321. while (true)
  322. {
  323. TSource item;
  324. try
  325. {
  326. if (!await e.MoveNextAsync())
  327. {
  328. break;
  329. }
  330. item = e.Current;
  331. await onNext(item).ConfigureAwait(false);
  332. }
  333. catch (OperationCanceledException)
  334. {
  335. throw;
  336. }
  337. catch (Exception ex) when (onError != null)
  338. {
  339. await onError(ex).ConfigureAwait(false);
  340. throw;
  341. }
  342. yield return item;
  343. }
  344. if (onCompleted != null)
  345. {
  346. await onCompleted().ConfigureAwait(false);
  347. }
  348. }
  349. }
  350. #if !NO_DEEP_CANCELLATION
  351. private static IAsyncEnumerable<TSource> DoCore<TSource>(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> onNext, Func<Exception, CancellationToken, Task>? onError, Func<CancellationToken, Task>? onCompleted)
  352. {
  353. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  354. return Core(source, onNext, onError, onCompleted);
  355. // TODO: Can remove local function.
  356. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, Task> onNext, Func<Exception, CancellationToken, Task>? onError, Func<CancellationToken, Task>? onCompleted, [System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  357. #else
  358. return AsyncEnumerable.Create(Core);
  359. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  360. #endif
  361. {
  362. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  363. while (true)
  364. {
  365. TSource item;
  366. try
  367. {
  368. if (!await e.MoveNextAsync())
  369. {
  370. break;
  371. }
  372. item = e.Current;
  373. await onNext(item, cancellationToken).ConfigureAwait(false);
  374. }
  375. catch (OperationCanceledException)
  376. {
  377. throw;
  378. }
  379. catch (Exception ex) when (onError != null)
  380. {
  381. await onError(ex, cancellationToken).ConfigureAwait(false);
  382. throw;
  383. }
  384. yield return item;
  385. }
  386. if (onCompleted != null)
  387. {
  388. await onCompleted(cancellationToken).ConfigureAwait(false);
  389. }
  390. }
  391. }
  392. #endif
  393. }
  394. }