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