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 AsyncEnumerable
  10. {
  11. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  12. {
  13. if (first == null)
  14. throw new ArgumentNullException(nameof(first));
  15. if (second == null)
  16. throw new ArgumentNullException(nameof(second));
  17. return OnErrorResumeNext_(new[] { first, second });
  18. }
  19. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(params IAsyncEnumerable<TSource>[] sources)
  20. {
  21. if (sources == null)
  22. throw new ArgumentNullException(nameof(sources));
  23. return OnErrorResumeNext_(sources);
  24. }
  25. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  26. {
  27. if (sources == null)
  28. throw new ArgumentNullException(nameof(sources));
  29. return OnErrorResumeNext_(sources);
  30. }
  31. private static IAsyncEnumerable<TSource> OnErrorResumeNext_<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. this.sources = sources;
  44. }
  45. public override AsyncIterator<TSource> Clone()
  46. {
  47. return new OnErrorResumeNextAsyncIterator<TSource>(sources);
  48. }
  49. public override async Task 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 Task<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();
  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. }