Throw.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Threading;
  4. namespace Cysharp.Threading.Tasks.Linq
  5. {
  6. public static partial class UniTaskAsyncEnumerable
  7. {
  8. public static IUniTaskAsyncEnumerable<TValue> Throw<TValue>(Exception exception)
  9. {
  10. return new Throw<TValue>(exception);
  11. }
  12. }
  13. internal class Throw<TValue> : IUniTaskAsyncEnumerable<TValue>
  14. {
  15. readonly Exception exception;
  16. public Throw(Exception exception)
  17. {
  18. this.exception = exception;
  19. }
  20. public IUniTaskAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  21. {
  22. return new _Throw(exception, cancellationToken);
  23. }
  24. class _Throw : IUniTaskAsyncEnumerator<TValue>
  25. {
  26. readonly Exception exception;
  27. CancellationToken cancellationToken;
  28. public _Throw(Exception exception, CancellationToken cancellationToken)
  29. {
  30. this.exception = exception;
  31. this.cancellationToken = cancellationToken;
  32. }
  33. public TValue Current => default;
  34. public UniTask<bool> MoveNextAsync()
  35. {
  36. cancellationToken.ThrowIfCancellationRequested();
  37. return UniTask.FromException<bool>(exception);
  38. }
  39. public UniTask DisposeAsync()
  40. {
  41. return default;
  42. }
  43. }
  44. }
  45. }