OnErrorResumeNext.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.Tasks;
  6. namespace System.Linq
  7. {
  8. public static partial class AsyncEnumerableEx
  9. {
  10. /// <summary>
  11. /// Concatenates the second async-enumerable sequence to the first async-enumerable sequence upon successful or exceptional termination of the first.
  12. /// </summary>
  13. /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
  14. /// <param name="first">First async-enumerable sequence whose exception (if any) is caught.</param>
  15. /// <param name="second">Second async-enumerable sequence used to produce results after the first sequence terminates.</param>
  16. /// <returns>An async-enumerable sequence that concatenates the first and second sequence, even if the first sequence terminates exceptionally.</returns>
  17. /// <exception cref="ArgumentNullException"><paramref name="first"/> or <paramref name="second"/> is null.</exception>
  18. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  19. {
  20. if (first == null)
  21. throw Error.ArgumentNull(nameof(first));
  22. if (second == null)
  23. throw Error.ArgumentNull(nameof(second));
  24. return OnErrorResumeNextCore(new[] { first, second });
  25. }
  26. /// <summary>
  27. /// Concatenates all of the specified async-enumerable sequences, even if the previous async-enumerable sequence terminated exceptionally.
  28. /// </summary>
  29. /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
  30. /// <param name="sources">Observable sequences to concatenate.</param>
  31. /// <returns>An async-enumerable sequence that concatenates the source sequences, even if a sequence terminates exceptionally.</returns>
  32. /// <exception cref="ArgumentNullException"><paramref name="sources"/> is null.</exception>
  33. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(params IAsyncEnumerable<TSource>[] sources)
  34. {
  35. if (sources == null)
  36. throw Error.ArgumentNull(nameof(sources));
  37. return OnErrorResumeNextCore(sources);
  38. }
  39. /// <summary>
  40. /// Concatenates all async-enumerable sequences in the given enumerable sequence, even if the previous async-enumerable sequence terminated exceptionally.
  41. /// </summary>
  42. /// <typeparam name="TSource">The type of the elements in the source sequences.</typeparam>
  43. /// <param name="sources">Observable sequences to concatenate.</param>
  44. /// <returns>An async-enumerable sequence that concatenates the source sequences, even if a sequence terminates exceptionally.</returns>
  45. /// <exception cref="ArgumentNullException"><paramref name="sources"/> is null.</exception>
  46. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  47. {
  48. if (sources == null)
  49. throw Error.ArgumentNull(nameof(sources));
  50. return OnErrorResumeNextCore(sources);
  51. }
  52. private static IAsyncEnumerable<TSource> OnErrorResumeNextCore<TSource>(IEnumerable<IAsyncEnumerable<TSource>> sources)
  53. {
  54. return new OnErrorResumeNextAsyncIterator<TSource>(sources);
  55. }
  56. private sealed class OnErrorResumeNextAsyncIterator<TSource> : AsyncIterator<TSource>
  57. {
  58. private readonly IEnumerable<IAsyncEnumerable<TSource>> _sources;
  59. private IAsyncEnumerator<TSource>? _enumerator;
  60. private IEnumerator<IAsyncEnumerable<TSource>>? _sourcesEnumerator;
  61. public OnErrorResumeNextAsyncIterator(IEnumerable<IAsyncEnumerable<TSource>> sources)
  62. {
  63. _sources = sources;
  64. }
  65. public override AsyncIteratorBase<TSource> Clone()
  66. {
  67. return new OnErrorResumeNextAsyncIterator<TSource>(_sources);
  68. }
  69. public override async ValueTask DisposeAsync()
  70. {
  71. if (_sourcesEnumerator != null)
  72. {
  73. _sourcesEnumerator.Dispose();
  74. _sourcesEnumerator = null;
  75. }
  76. if (_enumerator != null)
  77. {
  78. await _enumerator.DisposeAsync().ConfigureAwait(false);
  79. _enumerator = null;
  80. }
  81. await base.DisposeAsync().ConfigureAwait(false);
  82. }
  83. protected override async ValueTask<bool> MoveNextCore()
  84. {
  85. switch (_state)
  86. {
  87. case AsyncIteratorState.Allocated:
  88. _sourcesEnumerator = _sources.GetEnumerator();
  89. _state = AsyncIteratorState.Iterating;
  90. goto case AsyncIteratorState.Iterating;
  91. case AsyncIteratorState.Iterating:
  92. while (true)
  93. {
  94. if (_enumerator == null)
  95. {
  96. if (!_sourcesEnumerator!.MoveNext())
  97. {
  98. break; // while -- done, nothing else to do
  99. }
  100. _enumerator = _sourcesEnumerator.Current.GetAsyncEnumerator(_cancellationToken);
  101. }
  102. try
  103. {
  104. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  105. {
  106. _current = _enumerator.Current;
  107. return true;
  108. }
  109. }
  110. catch
  111. {
  112. // Ignore
  113. }
  114. // Done with the current one, go to the next
  115. await _enumerator.DisposeAsync().ConfigureAwait(false);
  116. _enumerator = null;
  117. }
  118. break; // case
  119. }
  120. await DisposeAsync().ConfigureAwait(false);
  121. return false;
  122. }
  123. }
  124. }
  125. }