SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.0 KiB

4 years ago
  1. namespace UnityEngine.XR.ARSubsystems
  2. {
  3. /// <summary>
  4. /// A Promise is used for operations that retrieve data asynchronously. Use this object
  5. /// to determine the status of the operation (i.e., whether it has completed), and
  6. /// the resulting data.
  7. /// </summary>
  8. /// <remarks>
  9. /// Since <see cref="Promise{T}"/> derives from <c>CustomYieldInstruction</c>,
  10. /// you can <c>yield return</c> on a Promise in a coroutine. If you prefer not
  11. /// to use the Promise as a coroutine, you may manually check <see cref="Promise{T}.keepWaiting"/>
  12. /// to determine if the operation has completed. Once the operation is complete, you can get the
  13. /// resulting value from <see cref="Promise{T}.result"/>.
  14. /// </remarks>
  15. /// <example>
  16. /// Example usage in a coroutine:
  17. /// <code>
  18. /// IEnumerator MyCoroutine()
  19. /// {
  20. /// var promise = GetDataAsync();
  21. /// yield return promise;
  22. /// Debug.LogFormat("Operation complete. Result = {0}", promise.result);
  23. /// }
  24. /// </code>
  25. /// </example>
  26. /// <typeparam name="T">The type of information the asynchronous operation retrieves.</typeparam>
  27. public abstract class Promise<T> : CustomYieldInstruction
  28. {
  29. /// <summary>
  30. /// Will return <c>true</c> as long as the operation has not yet completed.
  31. /// </summary>
  32. public override bool keepWaiting
  33. {
  34. get
  35. {
  36. OnKeepWaiting();
  37. return !m_Complete;
  38. }
  39. }
  40. /// <summary>
  41. /// The result of the asynchronous operation.
  42. /// Not valid until <see cref="keepWaiting"/> returns <c>false</c>.
  43. /// </summary>
  44. public T result { get; private set; }
  45. /// <summary>
  46. /// Creates a resolved promise, i.e., one that is already complete.
  47. /// </summary>
  48. /// <param name="result">The result of the operation.</param>
  49. /// <returns>A completed <see cref="Promise{T}"/>.</returns>
  50. public static Promise<T> CreateResolvedPromise(T result)
  51. {
  52. return new ImmediatePromise(result);
  53. }
  54. /// <summary>
  55. /// The creator of the <see cref="Promise{T}"/> should call this
  56. /// when the asynchronous operation completes.
  57. /// </summary>
  58. /// <param name="result">The result of the asychronous operation.</param>
  59. protected void Resolve(T result)
  60. {
  61. this.result = result;
  62. m_Complete = true;
  63. }
  64. /// <summary>
  65. /// Invoked whenever <see cref="keepWaiting"/> is queried.
  66. /// Implement this to perform per-frame updates.
  67. /// </summary>
  68. protected abstract void OnKeepWaiting();
  69. bool m_Complete;
  70. class ImmediatePromise : Promise<T>
  71. {
  72. protected override void OnKeepWaiting() { }
  73. public ImmediatePromise(T immediateResult)
  74. {
  75. Resolve(immediateResult);
  76. }
  77. }
  78. }
  79. }