Defer.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. using static System.Linq.AsyncEnumerable;
  9. namespace System.Linq
  10. {
  11. public static partial class AsyncEnumerableEx
  12. {
  13. public static IAsyncEnumerable<TSource> Defer<TSource>(Func<IAsyncEnumerable<TSource>> factory)
  14. {
  15. if (factory == null)
  16. throw Error.ArgumentNull(nameof(factory));
  17. return CreateEnumerable(ct => factory().GetAsyncEnumerator(ct));
  18. }
  19. public static IAsyncEnumerable<TSource> Defer<TSource>(Func<Task<IAsyncEnumerable<TSource>>> factory)
  20. {
  21. if (factory == null)
  22. throw Error.ArgumentNull(nameof(factory));
  23. return new AnonymousAsyncEnumerableWithTask<TSource>(async ct => (await factory().ConfigureAwait(false)).GetAsyncEnumerator(ct));
  24. }
  25. private sealed class AnonymousAsyncEnumerableWithTask<T> : IAsyncEnumerable<T>
  26. {
  27. private readonly Func<CancellationToken, Task<IAsyncEnumerator<T>>> _getEnumerator;
  28. public AnonymousAsyncEnumerableWithTask(Func<CancellationToken, Task<IAsyncEnumerator<T>>> getEnumerator)
  29. {
  30. Debug.Assert(getEnumerator != null);
  31. _getEnumerator = getEnumerator;
  32. }
  33. public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken) => new Enumerator(_getEnumerator, cancellationToken);
  34. private sealed class Enumerator : IAsyncEnumerator<T>
  35. {
  36. private Func<CancellationToken, Task<IAsyncEnumerator<T>>> _getEnumerator;
  37. private readonly CancellationToken _cancellationToken;
  38. private IAsyncEnumerator<T> _enumerator;
  39. public Enumerator(Func<CancellationToken, Task<IAsyncEnumerator<T>>> getEnumerator, CancellationToken cancellationToken)
  40. {
  41. Debug.Assert(getEnumerator != null);
  42. _getEnumerator = getEnumerator;
  43. _cancellationToken = cancellationToken;
  44. }
  45. public T Current
  46. {
  47. get
  48. {
  49. if (_enumerator == null)
  50. throw new InvalidOperationException();
  51. return _enumerator.Current;
  52. }
  53. }
  54. public async ValueTask DisposeAsync()
  55. {
  56. var old = Interlocked.Exchange(ref _enumerator, DisposedEnumerator.Instance);
  57. if (_enumerator != null)
  58. {
  59. await _enumerator.DisposeAsync().ConfigureAwait(false);
  60. }
  61. }
  62. public ValueTask<bool> MoveNextAsync()
  63. {
  64. if (_enumerator == null)
  65. {
  66. return InitAndMoveNextAsync();
  67. }
  68. return _enumerator.MoveNextAsync();
  69. }
  70. private async ValueTask<bool> InitAndMoveNextAsync()
  71. {
  72. try
  73. {
  74. _enumerator = await _getEnumerator(_cancellationToken).ConfigureAwait(false);
  75. }
  76. catch (Exception ex)
  77. {
  78. _enumerator = Throw<T>(ex).GetAsyncEnumerator(_cancellationToken);
  79. throw;
  80. }
  81. finally
  82. {
  83. _getEnumerator = null;
  84. }
  85. return await _enumerator.MoveNextAsync().ConfigureAwait(false);
  86. }
  87. private sealed class DisposedEnumerator : IAsyncEnumerator<T>
  88. {
  89. public static readonly DisposedEnumerator Instance = new DisposedEnumerator();
  90. public T Current => throw new ObjectDisposedException("this");
  91. public ValueTask DisposeAsync() => default;
  92. public ValueTask<bool> MoveNextAsync() => throw new ObjectDisposedException("this");
  93. }
  94. }
  95. }
  96. }
  97. }