Return.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System.Threading;
  3. namespace Cysharp.Threading.Tasks.Linq
  4. {
  5. public static partial class UniTaskAsyncEnumerable
  6. {
  7. public static IUniTaskAsyncEnumerable<TValue> Return<TValue>(TValue value)
  8. {
  9. return new Return<TValue>(value);
  10. }
  11. }
  12. internal class Return<TValue> : IUniTaskAsyncEnumerable<TValue>
  13. {
  14. readonly TValue value;
  15. public Return(TValue value)
  16. {
  17. this.value = value;
  18. }
  19. public IUniTaskAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  20. {
  21. return new _Return(value, cancellationToken);
  22. }
  23. class _Return : IUniTaskAsyncEnumerator<TValue>
  24. {
  25. readonly TValue value;
  26. CancellationToken cancellationToken;
  27. bool called;
  28. public _Return(TValue value, CancellationToken cancellationToken)
  29. {
  30. this.value = value;
  31. this.cancellationToken = cancellationToken;
  32. this.called = false;
  33. }
  34. public TValue Current => value;
  35. public UniTask<bool> MoveNextAsync()
  36. {
  37. cancellationToken.ThrowIfCancellationRequested();
  38. if (!called)
  39. {
  40. called = true;
  41. return CompletedTasks.True;
  42. }
  43. return CompletedTasks.False;
  44. }
  45. public UniTask DisposeAsync()
  46. {
  47. return default;
  48. }
  49. }
  50. }
  51. }