IUniTaskAsyncEnumerable.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Threading;
  5. namespace Cysharp.Threading.Tasks
  6. {
  7. public interface IUniTaskAsyncEnumerable<out T>
  8. {
  9. IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default);
  10. }
  11. public interface IUniTaskAsyncEnumerator<out T> : IUniTaskAsyncDisposable
  12. {
  13. T Current { get; }
  14. UniTask<bool> MoveNextAsync();
  15. }
  16. public interface IUniTaskAsyncDisposable
  17. {
  18. UniTask DisposeAsync();
  19. }
  20. public interface IUniTaskOrderedAsyncEnumerable<TElement> : IUniTaskAsyncEnumerable<TElement>
  21. {
  22. IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, TKey> keySelector, IComparer<TKey> comparer, bool descending);
  23. IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending);
  24. IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, CancellationToken, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending);
  25. }
  26. public interface IConnectableUniTaskAsyncEnumerable<out T> : IUniTaskAsyncEnumerable<T>
  27. {
  28. IDisposable Connect();
  29. }
  30. // don't use AsyncGrouping.
  31. //public interface IUniTaskAsyncGrouping<out TKey, out TElement> : IUniTaskAsyncEnumerable<TElement>
  32. //{
  33. // TKey Key { get; }
  34. //}
  35. public static class UniTaskAsyncEnumerableExtensions
  36. {
  37. public static UniTaskCancelableAsyncEnumerable<T> WithCancellation<T>(this IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
  38. {
  39. return new UniTaskCancelableAsyncEnumerable<T>(source, cancellationToken);
  40. }
  41. }
  42. [StructLayout(LayoutKind.Auto)]
  43. public readonly struct UniTaskCancelableAsyncEnumerable<T>
  44. {
  45. private readonly IUniTaskAsyncEnumerable<T> enumerable;
  46. private readonly CancellationToken cancellationToken;
  47. internal UniTaskCancelableAsyncEnumerable(IUniTaskAsyncEnumerable<T> enumerable, CancellationToken cancellationToken)
  48. {
  49. this.enumerable = enumerable;
  50. this.cancellationToken = cancellationToken;
  51. }
  52. public Enumerator GetAsyncEnumerator()
  53. {
  54. return new Enumerator(enumerable.GetAsyncEnumerator(cancellationToken));
  55. }
  56. [StructLayout(LayoutKind.Auto)]
  57. public readonly struct Enumerator
  58. {
  59. private readonly IUniTaskAsyncEnumerator<T> enumerator;
  60. internal Enumerator(IUniTaskAsyncEnumerator<T> enumerator)
  61. {
  62. this.enumerator = enumerator;
  63. }
  64. public T Current => enumerator.Current;
  65. public UniTask<bool> MoveNextAsync()
  66. {
  67. return enumerator.MoveNextAsync();
  68. }
  69. public UniTask DisposeAsync()
  70. {
  71. return enumerator.DisposeAsync();
  72. }
  73. }
  74. }
  75. }