OnErrorResumeNext.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerableEx
  10. {
  11. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  12. {
  13. if (first == null)
  14. throw Error.ArgumentNull(nameof(first));
  15. if (second == null)
  16. throw Error.ArgumentNull(nameof(second));
  17. return OnErrorResumeNextCore(new[] { first, second });
  18. }
  19. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(params IAsyncEnumerable<TSource>[] sources)
  20. {
  21. if (sources == null)
  22. throw Error.ArgumentNull(nameof(sources));
  23. return OnErrorResumeNextCore(sources);
  24. }
  25. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  26. {
  27. if (sources == null)
  28. throw Error.ArgumentNull(nameof(sources));
  29. return OnErrorResumeNextCore(sources);
  30. }
  31. private static IAsyncEnumerable<TSource> OnErrorResumeNextCore<TSource>(IEnumerable<IAsyncEnumerable<TSource>> sources)
  32. {
  33. return new OnErrorResumeNextAsyncIterator<TSource>(sources);
  34. }
  35. private sealed class OnErrorResumeNextAsyncIterator<TSource> : AsyncIterator<TSource>
  36. {
  37. private readonly IEnumerable<IAsyncEnumerable<TSource>> _sources;
  38. private IAsyncEnumerator<TSource>? _enumerator;
  39. private IEnumerator<IAsyncEnumerable<TSource>>? _sourcesEnumerator;
  40. public OnErrorResumeNextAsyncIterator(IEnumerable<IAsyncEnumerable<TSource>> sources)
  41. {
  42. Debug.Assert(sources != null);
  43. _sources = sources;
  44. }
  45. public override AsyncIteratorBase<TSource> Clone()
  46. {
  47. return new OnErrorResumeNextAsyncIterator<TSource>(_sources);
  48. }
  49. public override async ValueTask DisposeAsync()
  50. {
  51. if (_sourcesEnumerator != null)
  52. {
  53. _sourcesEnumerator.Dispose();
  54. _sourcesEnumerator = null;
  55. }
  56. if (_enumerator != null)
  57. {
  58. await _enumerator.DisposeAsync().ConfigureAwait(false);
  59. _enumerator = null;
  60. }
  61. await base.DisposeAsync().ConfigureAwait(false);
  62. }
  63. protected override async ValueTask<bool> MoveNextCore()
  64. {
  65. switch (_state)
  66. {
  67. case AsyncIteratorState.Allocated:
  68. _sourcesEnumerator = _sources.GetEnumerator();
  69. _state = AsyncIteratorState.Iterating;
  70. goto case AsyncIteratorState.Iterating;
  71. case AsyncIteratorState.Iterating:
  72. while (true)
  73. {
  74. if (_enumerator == null)
  75. {
  76. if (!_sourcesEnumerator!.MoveNext())
  77. {
  78. break; // while -- done, nothing else to do
  79. }
  80. _enumerator = _sourcesEnumerator.Current.GetAsyncEnumerator(_cancellationToken);
  81. }
  82. try
  83. {
  84. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  85. {
  86. _current = _enumerator.Current;
  87. return true;
  88. }
  89. }
  90. catch
  91. {
  92. // Ignore
  93. }
  94. // Done with the current one, go to the next
  95. await _enumerator.DisposeAsync().ConfigureAwait(false);
  96. _enumerator = null;
  97. }
  98. break; // case
  99. }
  100. await DisposeAsync().ConfigureAwait(false);
  101. return false;
  102. }
  103. }
  104. }
  105. }