Finally.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 AsyncEnumerableEx
  11. {
  12. public static IAsyncEnumerable<TSource> Finally<TSource>(this IAsyncEnumerable<TSource> source, Action finallyAction)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. if (finallyAction == null)
  17. throw new ArgumentNullException(nameof(finallyAction));
  18. return new FinallyAsyncIterator<TSource>(source, finallyAction);
  19. }
  20. public static IAsyncEnumerable<TSource> Finally<TSource>(this IAsyncEnumerable<TSource> source, Func<Task> finallyAction)
  21. {
  22. if (source == null)
  23. throw new ArgumentNullException(nameof(source));
  24. if (finallyAction == null)
  25. throw new ArgumentNullException(nameof(finallyAction));
  26. return new FinallyAsyncIteratorWithTask<TSource>(source, finallyAction);
  27. }
  28. private sealed class FinallyAsyncIterator<TSource> : AsyncIterator<TSource>
  29. {
  30. private readonly Action _finallyAction;
  31. private readonly IAsyncEnumerable<TSource> _source;
  32. private IAsyncEnumerator<TSource> _enumerator;
  33. private bool _finallyOnce;
  34. public FinallyAsyncIterator(IAsyncEnumerable<TSource> source, Action finallyAction)
  35. {
  36. Debug.Assert(source != null);
  37. Debug.Assert(finallyAction != null);
  38. _source = source;
  39. _finallyAction = finallyAction;
  40. }
  41. public override AsyncIterator<TSource> Clone()
  42. {
  43. return new FinallyAsyncIterator<TSource>(_source, _finallyAction);
  44. }
  45. public override async ValueTask DisposeAsync()
  46. {
  47. if (_enumerator != null)
  48. {
  49. await _enumerator.DisposeAsync().ConfigureAwait(false);
  50. _enumerator = null;
  51. }
  52. // Run the _finallyAction even if
  53. // the consumer did not call MoveNextAsync
  54. if (!_finallyOnce)
  55. {
  56. _finallyOnce = true;
  57. _finallyAction();
  58. }
  59. await base.DisposeAsync().ConfigureAwait(false);
  60. }
  61. protected override async ValueTask<bool> MoveNextCore(CancellationToken cancellationToken)
  62. {
  63. switch (state)
  64. {
  65. case AsyncIteratorState.Allocated:
  66. _enumerator = _source.GetAsyncEnumerator(cancellationToken);
  67. state = AsyncIteratorState.Iterating;
  68. goto case AsyncIteratorState.Iterating;
  69. case AsyncIteratorState.Iterating:
  70. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  71. {
  72. current = _enumerator.Current;
  73. return true;
  74. }
  75. await DisposeAsync().ConfigureAwait(false);
  76. break;
  77. }
  78. return false;
  79. }
  80. }
  81. private sealed class FinallyAsyncIteratorWithTask<TSource> : AsyncIterator<TSource>
  82. {
  83. private readonly Func<Task> _finallyAction;
  84. private readonly IAsyncEnumerable<TSource> _source;
  85. private IAsyncEnumerator<TSource> _enumerator;
  86. private bool _finallyOnce;
  87. public FinallyAsyncIteratorWithTask(IAsyncEnumerable<TSource> source, Func<Task> finallyAction)
  88. {
  89. Debug.Assert(source != null);
  90. Debug.Assert(finallyAction != null);
  91. _source = source;
  92. _finallyAction = finallyAction;
  93. }
  94. public override AsyncIterator<TSource> Clone()
  95. {
  96. return new FinallyAsyncIteratorWithTask<TSource>(_source, _finallyAction);
  97. }
  98. public override async ValueTask DisposeAsync()
  99. {
  100. if (_enumerator != null)
  101. {
  102. await _enumerator.DisposeAsync().ConfigureAwait(false);
  103. _enumerator = null;
  104. }
  105. // Await the _finallyAction even if
  106. // the consumer did not call MoveNextAsync
  107. if (!_finallyOnce)
  108. {
  109. _finallyOnce = true;
  110. await _finallyAction().ConfigureAwait(false);
  111. }
  112. await base.DisposeAsync().ConfigureAwait(false);
  113. }
  114. protected override async ValueTask<bool> MoveNextCore(CancellationToken cancellationToken)
  115. {
  116. switch (state)
  117. {
  118. case AsyncIteratorState.Allocated:
  119. _enumerator = _source.GetAsyncEnumerator(cancellationToken);
  120. state = AsyncIteratorState.Iterating;
  121. goto case AsyncIteratorState.Iterating;
  122. case AsyncIteratorState.Iterating:
  123. if (await _enumerator.MoveNextAsync().ConfigureAwait(false))
  124. {
  125. current = _enumerator.Current;
  126. return true;
  127. }
  128. await DisposeAsync().ConfigureAwait(false);
  129. break;
  130. }
  131. return false;
  132. }
  133. }
  134. }
  135. }