OnErrorResumeNext.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IAsyncEnumerable<TSource> first, IAsyncEnumerable<TSource> second)
  13. {
  14. if (first == null)
  15. {
  16. throw new ArgumentNullException(nameof(first));
  17. }
  18. if (second == null)
  19. {
  20. throw new ArgumentNullException(nameof(second));
  21. }
  22. return OnErrorResumeNext_(new[] { first, second });
  23. }
  24. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(params IAsyncEnumerable<TSource>[] sources)
  25. {
  26. if (sources == null)
  27. {
  28. throw new ArgumentNullException(nameof(sources));
  29. }
  30. return OnErrorResumeNext_(sources);
  31. }
  32. public static IAsyncEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IAsyncEnumerable<TSource>> sources)
  33. {
  34. if (sources == null)
  35. {
  36. throw new ArgumentNullException(nameof(sources));
  37. }
  38. return OnErrorResumeNext_(sources);
  39. }
  40. private static IAsyncEnumerable<TSource> OnErrorResumeNext_<TSource>(IEnumerable<IAsyncEnumerable<TSource>> sources)
  41. {
  42. return new OnErrorResumeNextAsyncIterator<TSource>(sources);
  43. }
  44. private sealed class OnErrorResumeNextAsyncIterator<TSource> : AsyncIterator<TSource>
  45. {
  46. private readonly IEnumerable<IAsyncEnumerable<TSource>> sources;
  47. private IAsyncEnumerator<TSource> enumerator;
  48. private IEnumerator<IAsyncEnumerable<TSource>> sourcesEnumerator;
  49. public OnErrorResumeNextAsyncIterator(IEnumerable<IAsyncEnumerable<TSource>> sources)
  50. {
  51. Debug.Assert(sources != null);
  52. this.sources = sources;
  53. }
  54. public override AsyncIterator<TSource> Clone()
  55. {
  56. return new OnErrorResumeNextAsyncIterator<TSource>(sources);
  57. }
  58. public override void Dispose()
  59. {
  60. if (sourcesEnumerator != null)
  61. {
  62. sourcesEnumerator.Dispose();
  63. sourcesEnumerator = null;
  64. }
  65. if (enumerator != null)
  66. {
  67. enumerator.Dispose();
  68. enumerator = null;
  69. }
  70. base.Dispose();
  71. }
  72. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  73. {
  74. switch (state)
  75. {
  76. case AsyncIteratorState.Allocated:
  77. sourcesEnumerator = sources.GetEnumerator();
  78. state = AsyncIteratorState.Iterating;
  79. goto case AsyncIteratorState.Iterating;
  80. case AsyncIteratorState.Iterating:
  81. while (true)
  82. {
  83. if (enumerator == null)
  84. {
  85. if (!sourcesEnumerator.MoveNext())
  86. {
  87. break; // while -- done, nothing else to do
  88. }
  89. enumerator = sourcesEnumerator.Current.GetEnumerator();
  90. }
  91. try
  92. {
  93. if (await enumerator.MoveNext(cancellationToken)
  94. .ConfigureAwait(false))
  95. {
  96. current = enumerator.Current;
  97. return true;
  98. }
  99. }
  100. catch
  101. {
  102. // Ignore
  103. }
  104. // Done with the current one, go to the next
  105. enumerator.Dispose();
  106. enumerator = null;
  107. }
  108. break; // case
  109. }
  110. Dispose();
  111. return false;
  112. }
  113. }
  114. }
  115. }