Concatenate.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. var switched = false;
  22. var e = first.GetEnumerator();
  23. var cts = new CancellationTokenDisposable();
  24. var a = new AssignableDisposable
  25. {
  26. Disposable = e
  27. };
  28. var d = Disposable.Create(cts, a);
  29. var f = default(Func<CancellationToken, Task<bool>>);
  30. f = async ct =>
  31. {
  32. if (await e.MoveNext(ct)
  33. .ConfigureAwait(false))
  34. {
  35. return true;
  36. }
  37. if (switched)
  38. {
  39. return false;
  40. }
  41. switched = true;
  42. e = second.GetEnumerator();
  43. a.Disposable = e;
  44. return await f(ct)
  45. .ConfigureAwait(false);
  46. };
  47. return CreateEnumerator(
  48. f,
  49. () => e.Current,
  50. d.Dispose,
  51. e
  52. );
  53. });
  54. }
  55. public static IAsyncEnumerable<TSource> Concat<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  56. {
  57. if (sources == null)
  58. throw new ArgumentNullException(nameof(sources));
  59. return sources.Concat_();
  60. }
  61. public static IAsyncEnumerable<TSource> Concat<TSource>(params IAsyncEnumerable<TSource>[] sources)
  62. {
  63. if (sources == null)
  64. throw new ArgumentNullException(nameof(sources));
  65. return sources.Concat_();
  66. }
  67. private static IAsyncEnumerable<TSource> Concat_<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  68. {
  69. return CreateEnumerable(() =>
  70. {
  71. var se = sources.GetEnumerator();
  72. var e = default(IAsyncEnumerator<TSource>);
  73. var cts = new CancellationTokenDisposable();
  74. var a = new AssignableDisposable();
  75. var d = Disposable.Create(cts, se, a);
  76. var f = default(Func<CancellationToken, Task<bool>>);
  77. f = async ct =>
  78. {
  79. if (e == null)
  80. {
  81. var b = false;
  82. b = se.MoveNext();
  83. if (b)
  84. e = se.Current.GetEnumerator();
  85. if (!b)
  86. {
  87. return false;
  88. }
  89. a.Disposable = e;
  90. }
  91. if (await e.MoveNext(ct)
  92. .ConfigureAwait(false))
  93. {
  94. return true;
  95. }
  96. e.Dispose();
  97. e = null;
  98. return await f(ct)
  99. .ConfigureAwait(false);
  100. };
  101. return CreateEnumerator(
  102. f,
  103. () => e.Current,
  104. d.Dispose,
  105. a
  106. );
  107. });
  108. }
  109. }
  110. }