Never.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 AsyncEnumerableEx
  10. {
  11. /// <summary>
  12. /// Returns a non-terminating async-enumerable sequence, which can be used to denote an infinite duration (e.g. when using reactive joins).
  13. /// </summary>
  14. /// <typeparam name="TValue">The type used for the <see cref="IAsyncEnumerable{T}"/> type parameter of the resulting sequence.</typeparam>
  15. /// <returns>An async-enumerable sequence whose consumers will never resume after awaiting <see cref="IAsyncEnumerator{T}.MoveNextAsync"/>.</returns>
  16. public static IAsyncEnumerable<TValue> Never<TValue>() => NeverAsyncEnumerable<TValue>.Instance;
  17. private sealed class NeverAsyncEnumerable<TValue> : IAsyncEnumerable<TValue>
  18. {
  19. internal static readonly NeverAsyncEnumerable<TValue> Instance = new NeverAsyncEnumerable<TValue>();
  20. public IAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken)
  21. {
  22. cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
  23. return new NeverAsyncEnumerator(cancellationToken);
  24. }
  25. private sealed class NeverAsyncEnumerator : IAsyncEnumerator<TValue>
  26. {
  27. private readonly CancellationToken _token;
  28. private CancellationTokenRegistration _registration;
  29. private bool _once;
  30. public NeverAsyncEnumerator(CancellationToken token) => _token = token;
  31. public TValue Current => throw new InvalidOperationException();
  32. public ValueTask DisposeAsync()
  33. {
  34. _registration.Dispose();
  35. return default;
  36. }
  37. public ValueTask<bool> MoveNextAsync()
  38. {
  39. if (_once)
  40. {
  41. return new ValueTask<bool>(false);
  42. }
  43. _once = true;
  44. var task = new TaskCompletionSource<bool>();
  45. _registration = _token.Register(state => ((TaskCompletionSource<bool>)state).TrySetCanceled(_token), task);
  46. return new ValueTask<bool>(task.Task);
  47. }
  48. }
  49. }
  50. }
  51. }