AsyncEnumerable.WithCancellation.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. // REVIEW: [LDM-2018-11-28] Should return type be a struct or just the interface type? Should this live in the System.Linq namespace or in System.Collections.Generic?
  12. public static WithCancellationTokenAsyncEnumerable<T> WithCancellation<T>(this IAsyncEnumerable<T> source, CancellationToken cancellationToken)
  13. {
  14. if (source == null)
  15. throw Error.ArgumentNull(nameof(source));
  16. return new WithCancellationTokenAsyncEnumerable<T>(source, cancellationToken);
  17. }
  18. // REVIEW: Explicit implementation of the interfaces allows for composition with other "modifier operators" such as ConfigureAwait.
  19. // We expect that the "await foreach" statement will bind to the public struct methods, thus avoiding boxing.
  20. public readonly struct WithCancellationTokenAsyncEnumerable<T> : IAsyncEnumerable<T>
  21. {
  22. private readonly IAsyncEnumerable<T> _source;
  23. private readonly CancellationToken _cancellationToken;
  24. public WithCancellationTokenAsyncEnumerable(IAsyncEnumerable<T> source, CancellationToken cancellationToken)
  25. {
  26. _source = source;
  27. _cancellationToken = cancellationToken;
  28. }
  29. // REVIEW: Should we simply ignore the second cancellation token or should we link the two?
  30. // REVIEW: [LDM-2018-11-28] Should we have eager cancellation here too?
  31. public WithCancellationAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken)
  32. {
  33. CancellationToken token;
  34. if (cancellationToken == default)
  35. {
  36. token = _cancellationToken;
  37. }
  38. else if (_cancellationToken == default)
  39. {
  40. token = cancellationToken;
  41. }
  42. else
  43. {
  44. token = CancellationTokenSource.CreateLinkedTokenSource(_cancellationToken, cancellationToken).Token;
  45. }
  46. return new WithCancellationAsyncEnumerator(_source.GetAsyncEnumerator(token));
  47. }
  48. IAsyncEnumerator<T> IAsyncEnumerable<T>.GetAsyncEnumerator(CancellationToken cancellationToken)
  49. => GetAsyncEnumerator(cancellationToken);
  50. public readonly struct WithCancellationAsyncEnumerator : IAsyncEnumerator<T>
  51. {
  52. private readonly IAsyncEnumerator<T> _enumerator;
  53. public WithCancellationAsyncEnumerator(IAsyncEnumerator<T> enumerator) => _enumerator = enumerator;
  54. public T Current => _enumerator.Current;
  55. public ValueTask DisposeAsync() => _enumerator.DisposeAsync();
  56. public ValueTask<bool> MoveNextAsync() => _enumerator.MoveNextAsync();
  57. }
  58. }
  59. }
  60. }