Never.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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();
  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(CancellationToken token) : IAsyncEnumerator<TValue>
  26. {
  27. private readonly CancellationToken _token = token;
  28. private CancellationTokenRegistration _registration;
  29. private bool _once;
  30. public TValue Current => throw new InvalidOperationException();
  31. public ValueTask DisposeAsync()
  32. {
  33. _registration.Dispose();
  34. return default;
  35. }
  36. public ValueTask<bool> MoveNextAsync()
  37. {
  38. if (_once)
  39. {
  40. return new ValueTask<bool>(false);
  41. }
  42. _once = true;
  43. var task = new TaskCompletionSource<bool>();
  44. _registration = _token.Register(state => ((TaskCompletionSource<bool>)state!).TrySetCanceled(_token), task);
  45. return new ValueTask<bool>(task.Task);
  46. }
  47. }
  48. }
  49. }
  50. }