1
0

Using.cs 3.5 KB

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