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