AsyncEnumerable.WithCancellation.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. => new WithCancellationAsyncEnumerator(_source.GetAsyncEnumerator(_cancellationToken));
  33. IAsyncEnumerator<T> IAsyncEnumerable<T>.GetAsyncEnumerator(CancellationToken cancellationToken)
  34. => GetAsyncEnumerator(cancellationToken);
  35. public readonly struct WithCancellationAsyncEnumerator : IAsyncEnumerator<T>
  36. {
  37. private readonly IAsyncEnumerator<T> _enumerator;
  38. public WithCancellationAsyncEnumerator(IAsyncEnumerator<T> enumerator) => _enumerator = enumerator;
  39. public T Current => _enumerator.Current;
  40. public ValueTask DisposeAsync() => _enumerator.DisposeAsync();
  41. public ValueTask<bool> MoveNextAsync() => _enumerator.MoveNextAsync();
  42. }
  43. }
  44. }
  45. }