Extensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using UnityAsync.Awaiters;
  8. using Object = UnityEngine.Object;
  9. namespace UnityAsync
  10. {
  11. public static class Extensions
  12. {
  13. /// <summary>
  14. /// Link the <see cref="UnityAsync.IAwaitInstruction"/>'s lifespan to a <see cref="UnityEngine.Object"/> and
  15. /// configure the type of update cycle it should be evaluated on.
  16. /// </summary>
  17. /// <returns>A continuation with updated params.</returns>
  18. public static AwaitInstructionAwaiter<T> ConfigureAwait<T>(this T i, Object parent, FrameScheduler scheduler) where T : IAwaitInstruction
  19. => new AwaitInstructionAwaiter<T>(i, parent, scheduler);
  20. /// <summary>
  21. /// Link the <see cref="UnityAsync.IAwaitInstruction"/>'s lifespan to a <see cref="UnityEngine.Object"/>.
  22. /// </summary>
  23. /// <returns>A continuation with updated params.</returns>
  24. public static AwaitInstructionAwaiter<T> ConfigureAwait<T>(this T i, Object parent) where T : IAwaitInstruction
  25. => new AwaitInstructionAwaiter<T>(i, parent, FrameScheduler.Update);
  26. /// <summary>
  27. /// Configure the type of update cycle it should be evaluated on.
  28. /// </summary>
  29. /// <returns>A continuation with updated params.</returns>
  30. public static AwaitInstructionAwaiter<T> ConfigureAwait<T>(this T i, FrameScheduler scheduler) where T : IAwaitInstruction
  31. => new AwaitInstructionAwaiter<T>(i, scheduler);
  32. /// <summary>
  33. /// Link the <see cref="UnityAsync.IAwaitInstruction"/>'s lifespan to a
  34. /// <see cref="System.Threading.CancellationToken"/> and configure the type of update cycle it should be
  35. /// evaluated on.
  36. /// </summary>
  37. /// <returns>A continuation with updated params.</returns>
  38. public static AwaitInstructionAwaiter<T> ConfigureAwait<T>(this T i, CancellationToken cancellationToken, FrameScheduler scheduler) where T : IAwaitInstruction
  39. => new AwaitInstructionAwaiter<T>(i, cancellationToken, scheduler);
  40. /// <summary>
  41. /// Link the <see cref="UnityAsync.IAwaitInstruction"/>'s lifespan to a <see cref="System.Threading.CancellationToken"/>.
  42. /// </summary>
  43. /// <returns>A continuation with updated params.</returns>
  44. public static AwaitInstructionAwaiter<T> ConfigureAwait<T>(this T i, CancellationToken cancellationToken) where T : struct, IAwaitInstruction
  45. => new AwaitInstructionAwaiter<T>(i, cancellationToken, FrameScheduler.Update);
  46. /// <summary>
  47. /// Encapsulate the <see cref="System.Threading.Tasks.Task"/> in a <see cref="UnityEngine.CustomYieldInstruction"/>
  48. /// so that it can be yielded in an IEnumerator coroutine.
  49. /// </summary>
  50. public static TaskYieldInstruction AsYieldInstruction(this Task t) => new TaskYieldInstruction(t);
  51. /// <summary>
  52. /// Encapsulate the <see cref="System.Threading.Tasks.Task{TResult}"/> in a <see cref="UnityEngine.CustomYieldInstruction"/>
  53. /// so that it can be yielded in an IEnumerator coroutine. The result can be obtained through
  54. /// <see cref="TaskYieldInstruction{T}.Current"/> after yielding.
  55. /// </summary>
  56. public static TaskYieldInstruction<T> AsYieldInstruction<T>(this Task<T> t) => new TaskYieldInstruction<T>(t);
  57. public static SynchronizationContextAwaiter GetAwaiter(this SynchronizationContext s) => new SynchronizationContextAwaiter(s);
  58. public static IEnumeratorAwaiter GetAwaiter(this IEnumerator e) => new IEnumeratorAwaiter(e);
  59. public static YieldInstructionAwaiter GetAwaiter(this YieldInstruction y) => new YieldInstructionAwaiter(y);
  60. public static ResourceRequestAwaiter GetAwaiter(this ResourceRequest r) => new ResourceRequestAwaiter(r);
  61. public static AsyncOperationAwaiter GetAwaiter(this AsyncOperation r) => new AsyncOperationAwaiter(r);
  62. public static AwaitInstructionAwaiter<T> GetAwaiter<T>(this T i) where T : struct, IAwaitInstruction => new AwaitInstructionAwaiter<T>(i);
  63. public static AwaitInstructionAwaiter<T> GetAwaiter<T>(in this AwaitInstructionAwaiter<T> a) where T : IAwaitInstruction => a;
  64. }
  65. }