Using.cs 3.5 KB

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