AsyncEnumerableExtensions.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. {
  31. cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
  32. return new ConfiguredAsyncEnumerator(_enumerable.GetAsyncEnumerator(cancellationToken), _continueOnCapturedContext);
  33. }
  34. IAsyncEnumerator<T> IAsyncEnumerable<T>.GetAsyncEnumerator(CancellationToken cancellationToken) =>
  35. GetAsyncEnumerator(cancellationToken);
  36. public readonly struct ConfiguredAsyncEnumerator : IAsyncEnumerator<T>
  37. {
  38. private readonly IAsyncEnumerator<T> _enumerator;
  39. private readonly bool _continueOnCapturedContext;
  40. internal ConfiguredAsyncEnumerator(IAsyncEnumerator<T> enumerator, bool continueOnCapturedContext)
  41. {
  42. _enumerator = enumerator;
  43. _continueOnCapturedContext = continueOnCapturedContext;
  44. }
  45. public ConfiguredValueTaskAwaitable<bool> MoveNextAsync() =>
  46. _enumerator.MoveNextAsync().ConfigureAwait(_continueOnCapturedContext);
  47. public T Current => _enumerator.Current;
  48. public ConfiguredValueTaskAwaitable DisposeAsync() =>
  49. _enumerator.DisposeAsync().ConfigureAwait(_continueOnCapturedContext);
  50. async ValueTask<bool> IAsyncEnumerator<T>.MoveNextAsync() =>
  51. await _enumerator.MoveNextAsync().ConfigureAwait(_continueOnCapturedContext);
  52. async ValueTask IAsyncDisposable.DisposeAsync() =>
  53. await _enumerator.DisposeAsync().ConfigureAwait(_continueOnCapturedContext);
  54. }
  55. }
  56. }
  57. }
  58. }