OnErrorResumeNext.cs 4.7 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 new ArgumentNullException(nameof(first));
  16. if (second == null)
  17. throw new ArgumentNullException(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 new ArgumentNullException(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 new ArgumentNullException(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. this.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(CancellationToken cancellationToken)
  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. }