AsyncEnumerableExtensions.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.Runtime.CompilerServices;
  6. namespace System.Threading.Tasks
  7. {
  8. namespace System.Threading.Tasks
  9. {
  10. public static class AsyncEnumerableExtensions
  11. {
  12. public static ConfiguredAsyncEnumerable<T> ConfigureAwait<T>(this IAsyncEnumerable<T> enumerable, bool continueOnCapturedContext)
  13. {
  14. if (enumerable == null)
  15. throw Error.ArgumentNull(nameof(enumerable));
  16. return new ConfiguredAsyncEnumerable<T>(enumerable, continueOnCapturedContext);
  17. }
  18. // REVIEW: Explicit implementation of the interfaces allows for composition with other "modifier operators" such as WithCancellation.
  19. // We expect that the "await foreach" statement will bind to the public struct methods, thus avoiding boxing.
  20. public readonly struct ConfiguredAsyncEnumerable<T> : IAsyncEnumerable<T>
  21. {
  22. private readonly IAsyncEnumerable<T> _enumerable;
  23. private readonly bool _continueOnCapturedContext;
  24. internal ConfiguredAsyncEnumerable(IAsyncEnumerable<T> enumerable, bool continueOnCapturedContext)
  25. {
  26. _enumerable = enumerable;
  27. _continueOnCapturedContext = continueOnCapturedContext;
  28. }
  29. public ConfiguredAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken) =>
  30. new ConfiguredAsyncEnumerator(_enumerable.GetAsyncEnumerator(cancellationToken), _continueOnCapturedContext);
  31. IAsyncEnumerator<T> IAsyncEnumerable<T>.GetAsyncEnumerator(CancellationToken cancellationToken) =>
  32. GetAsyncEnumerator(cancellationToken);
  33. public readonly struct ConfiguredAsyncEnumerator : IAsyncEnumerator<T>
  34. {
  35. private readonly IAsyncEnumerator<T> _enumerator;
  36. private readonly bool _continueOnCapturedContext;
  37. internal ConfiguredAsyncEnumerator(IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext)
  38. {
  39. _enumerator = enumerator;
  40. _continueOnCapturedContext = continueOnCapturedContext;
  41. }
  42. public ConfiguredValueTaskAwaitable<bool> MoveNextAsync() =>
  43. _enumerator.MoveNextAsync().ConfigureAwait(_continueOnCapturedContext);
  44. public T Current => _enumerator.Current;
  45. public ConfiguredValueTaskAwaitable DisposeAsync() =>
  46. _enumerator.DisposeAsync().ConfigureAwait(_continueOnCapturedContext);
  47. async ValueTask<bool> IAsyncEnumerator<T>.MoveNextAsync() =>
  48. await _enumerator.MoveNextAsync().ConfigureAwait(_continueOnCapturedContext);
  49. async ValueTask IAsyncDisposable.DisposeAsync() =>
  50. await _enumerator.DisposeAsync().ConfigureAwait(_continueOnCapturedContext);
  51. }
  52. }
  53. }
  54. }
  55. }