OnErrorResumeNext.cs 4.5 KB

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