Concat.cs 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /// <summary>
  12. /// Concatenates all inner async-enumerable sequences, as long as the previous async-enumerable sequence terminated successfully.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
  15. /// <param name="sources">Observable sequence of inner async-enumerable sequences.</param>
  16. /// <returns>An async-enumerable sequence that contains the elements of each observed inner sequence, in sequential order.</returns>
  17. /// <exception cref="ArgumentNullException"><paramref name="sources"/> is null.</exception>
  18. public static IAsyncEnumerable<TSource> Concat<TSource>(this IAsyncEnumerable<IAsyncEnumerable<TSource>> sources)
  19. {
  20. if (sources == null)
  21. throw Error.ArgumentNull(nameof(sources));
  22. return Core(sources);
  23. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<IAsyncEnumerable<TSource>> sources, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  24. {
  25. await foreach (var source in sources.WithCancellation(cancellationToken).ConfigureAwait(false))
  26. {
  27. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  28. {
  29. yield return item;
  30. }
  31. }
  32. }
  33. }
  34. /// <summary>
  35. /// Concatenates all async-enumerable sequences in the given enumerable sequence, as long as the previous async-enumerable sequence terminated successfully.
  36. /// </summary>
  37. /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
  38. /// <param name="sources">Observable sequences to concatenate.</param>
  39. /// <returns>An async-enumerable sequence that contains the elements of each given sequence, in sequential order.</returns>
  40. /// <exception cref="ArgumentNullException"><paramref name="sources"/> is null.</exception>
  41. public static IAsyncEnumerable<TSource> Concat<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  42. {
  43. if (sources == null)
  44. throw Error.ArgumentNull(nameof(sources));
  45. return Core(sources);
  46. static async IAsyncEnumerable<TSource> Core(IEnumerable<IAsyncEnumerable<TSource>> sources, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  47. {
  48. foreach (var source in sources)
  49. {
  50. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  51. {
  52. yield return item;
  53. }
  54. }
  55. }
  56. }
  57. /// <summary>
  58. /// Concatenates all of the specified async-enumerable sequences, as long as the previous async-enumerable sequence terminated successfully.
  59. /// </summary>
  60. /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
  61. /// <param name="sources">Observable sequences to concatenate.</param>
  62. /// <returns>An async-enumerable sequence that contains the elements of each given sequence, in sequential order.</returns>
  63. /// <exception cref="ArgumentNullException"><paramref name="sources"/> is null.</exception>
  64. public static IAsyncEnumerable<TSource> Concat<TSource>(params IAsyncEnumerable<TSource>[] sources)
  65. {
  66. if (sources == null)
  67. throw Error.ArgumentNull(nameof(sources));
  68. return Core(sources);
  69. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource>[] sources, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  70. {
  71. foreach (var source in sources)
  72. {
  73. await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
  74. {
  75. yield return item;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }