1
0

Using.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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> Using<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory) where TResource : IDisposable
  13. {
  14. if (resourceFactory == null)
  15. {
  16. throw new ArgumentNullException(nameof(resourceFactory));
  17. }
  18. if (enumerableFactory == null)
  19. {
  20. throw new ArgumentNullException(nameof(enumerableFactory));
  21. }
  22. return new UsingAsyncIterator<TSource, TResource>(resourceFactory, enumerableFactory);
  23. }
  24. private sealed class UsingAsyncIterator<TSource, TResource> : AsyncIterator<TSource> where TResource : IDisposable
  25. {
  26. private readonly Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory;
  27. private readonly Func<TResource> resourceFactory;
  28. private IAsyncEnumerable<TSource> enumerable;
  29. private IAsyncEnumerator<TSource> enumerator;
  30. private TResource resource;
  31. public UsingAsyncIterator(Func<TResource> resourceFactory, Func<TResource, IAsyncEnumerable<TSource>> enumerableFactory)
  32. {
  33. Debug.Assert(resourceFactory != null);
  34. Debug.Assert(enumerableFactory != null);
  35. this.resourceFactory = resourceFactory;
  36. this.enumerableFactory = enumerableFactory;
  37. }
  38. public override AsyncIterator<TSource> Clone()
  39. {
  40. return new UsingAsyncIterator<TSource, TResource>(resourceFactory, enumerableFactory);
  41. }
  42. public override void Dispose()
  43. {
  44. if (enumerator != null)
  45. {
  46. enumerator.Dispose();
  47. enumerator = null;
  48. }
  49. if (resource != null)
  50. {
  51. resource.Dispose();
  52. resource = default;
  53. }
  54. base.Dispose();
  55. }
  56. protected override async Task<bool> MoveNextCore(CancellationToken cancellationToken)
  57. {
  58. switch (state)
  59. {
  60. case AsyncIteratorState.Allocated:
  61. enumerator = enumerable.GetEnumerator();
  62. state = AsyncIteratorState.Iterating;
  63. goto case AsyncIteratorState.Iterating;
  64. case AsyncIteratorState.Iterating:
  65. while (await enumerator.MoveNext(cancellationToken)
  66. .ConfigureAwait(false))
  67. {
  68. current = enumerator.Current;
  69. return true;
  70. }
  71. Dispose();
  72. break;
  73. }
  74. return false;
  75. }
  76. protected override void OnGetEnumerator()
  77. {
  78. resource = resourceFactory();
  79. enumerable = enumerableFactory(resource);
  80. base.OnGetEnumerator();
  81. }
  82. }
  83. }
  84. }