OnErrorResumeNext.cs 4.5 KB

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