OnErrorResumeNext.cs 4.6 KB

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