Concatenate.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerable
  12. {
  13. public static IAsyncEnumerable<TSource> Concat<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  14. {
  15. if (first == null)
  16. throw new ArgumentNullException(nameof(first));
  17. if (second == null)
  18. throw new ArgumentNullException(nameof(second));
  19. return CreateEnumerable(
  20. () =>
  21. {
  22. var switched = false;
  23. var e = first.GetEnumerator();
  24. var cts = new CancellationTokenDisposable();
  25. var a = new AssignableDisposable
  26. {
  27. Disposable = e
  28. };
  29. var d = Disposable.Create(cts, a);
  30. var f = default(Func<CancellationToken, Task<bool>>);
  31. f = async ct =>
  32. {
  33. if (await e.MoveNext(ct)
  34. .ConfigureAwait(false))
  35. {
  36. return true;
  37. }
  38. if (switched)
  39. {
  40. return false;
  41. }
  42. switched = true;
  43. e = second.GetEnumerator();
  44. a.Disposable = e;
  45. return await f(ct)
  46. .ConfigureAwait(false);
  47. };
  48. return CreateEnumerator(
  49. f,
  50. () => e.Current,
  51. d.Dispose,
  52. e
  53. );
  54. });
  55. }
  56. public static IAsyncEnumerable<TSource> Concat<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  57. {
  58. if (sources == null)
  59. throw new ArgumentNullException(nameof(sources));
  60. return sources.Concat_();
  61. }
  62. public static IAsyncEnumerable<TSource> Concat<TSource>(params IAsyncEnumerable<TSource>[] sources)
  63. {
  64. if (sources == null)
  65. throw new ArgumentNullException(nameof(sources));
  66. return sources.Concat_();
  67. }
  68. private static IAsyncEnumerable<TSource> Concat_<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  69. {
  70. return CreateEnumerable(
  71. () =>
  72. {
  73. var se = sources.GetEnumerator();
  74. var e = default(IAsyncEnumerator<TSource>);
  75. var cts = new CancellationTokenDisposable();
  76. var a = new AssignableDisposable();
  77. var d = Disposable.Create(cts, se, a);
  78. var f = default(Func<CancellationToken, Task<bool>>);
  79. f = async ct =>
  80. {
  81. if (e == null)
  82. {
  83. var b = false;
  84. b = se.MoveNext();
  85. if (b)
  86. e = se.Current.GetEnumerator();
  87. if (!b)
  88. {
  89. return false;
  90. }
  91. a.Disposable = e;
  92. }
  93. if (await e.MoveNext(ct)
  94. .ConfigureAwait(false))
  95. {
  96. return true;
  97. }
  98. e.Dispose();
  99. e = null;
  100. return await f(ct)
  101. .ConfigureAwait(false);
  102. };
  103. return CreateEnumerator(
  104. f,
  105. () => e.Current,
  106. d.Dispose,
  107. a
  108. );
  109. });
  110. }
  111. }
  112. }