Never.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Threading;
  2. namespace Cysharp.Threading.Tasks.Linq
  3. {
  4. public static partial class UniTaskAsyncEnumerable
  5. {
  6. public static IUniTaskAsyncEnumerable<T> Never<T>()
  7. {
  8. return Cysharp.Threading.Tasks.Linq.Never<T>.Instance;
  9. }
  10. }
  11. internal class Never<T> : IUniTaskAsyncEnumerable<T>
  12. {
  13. public static readonly IUniTaskAsyncEnumerable<T> Instance = new Never<T>();
  14. Never()
  15. {
  16. }
  17. public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  18. {
  19. return new _Never(cancellationToken);
  20. }
  21. class _Never : IUniTaskAsyncEnumerator<T>
  22. {
  23. CancellationToken cancellationToken;
  24. public _Never(CancellationToken cancellationToken)
  25. {
  26. this.cancellationToken = cancellationToken;
  27. }
  28. public T Current => default;
  29. public UniTask<bool> MoveNextAsync()
  30. {
  31. var tcs = new UniTaskCompletionSource<bool>();
  32. cancellationToken.Register(state =>
  33. {
  34. var task = (UniTaskCompletionSource<bool>)state;
  35. task.TrySetCanceled(cancellationToken);
  36. }, tcs);
  37. return tcs.Task;
  38. }
  39. public UniTask DisposeAsync()
  40. {
  41. return default;
  42. }
  43. }
  44. }
  45. }