RequestJob.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using UnityEditor.PackageManager.Requests; //Request
  2. using UnityEditor.PackageManager; //StatusCode
  3. using System; //Action
  4. namespace Unity.RenderStreaming.Editor {
  5. //---------------------------------------------------------------------------------------------------------------------
  6. //Non-generics version
  7. internal class RequestJob : IRequestJob {
  8. internal RequestJob(Request req, Action onSuccess, Action onFail) {
  9. m_request = req;
  10. m_onSuccess = onSuccess;
  11. m_onFail = onFail;
  12. }
  13. //---------------------------------------------------------------------------------------------------------------------
  14. public StatusCode Update() {
  15. if (null == m_request) {
  16. OnFail();
  17. return StatusCode.Failure;
  18. }
  19. if (m_request.IsCompleted) {
  20. if (StatusCode.Success == m_request.Status ) {
  21. OnSuccess();
  22. return StatusCode.Success;
  23. } else {
  24. OnFail();
  25. return StatusCode.Failure;
  26. }
  27. }
  28. return StatusCode.InProgress;
  29. }
  30. //---------------------------------------------------------------------------------------------------------------------
  31. void OnSuccess() {
  32. if (null==m_onSuccess)
  33. return;
  34. m_onSuccess();
  35. }
  36. //---------------------------------------------------------------------------------------------------------------------
  37. void OnFail() {
  38. if (null==m_onFail)
  39. return;
  40. m_onFail();
  41. }
  42. Request m_request;
  43. Action m_onSuccess;
  44. Action m_onFail;
  45. } //end RequestJob (non-generics version)
  46. //---------------------------------------------------------------------------------------------------------------------
  47. //RequestJob (generics version)
  48. //Examples of T: PackageCollection(from ListRequest), PackageInfo (from AddRequest)
  49. internal class RequestJob<T> : IRequestJob {
  50. internal RequestJob(Request<T> req, Action<Request<T>> onSuccess, Action<Request<T>> onFail) {
  51. m_request = req;
  52. m_onSuccess = onSuccess;
  53. m_onFail = onFail;
  54. }
  55. //---------------------------------------------------------------------------------------------------------------------
  56. public StatusCode Update() {
  57. if (null == m_request) {
  58. OnFail();
  59. return StatusCode.Failure;
  60. }
  61. if (m_request.IsCompleted) {
  62. if (StatusCode.Success == m_request.Status ) {
  63. OnSuccess();
  64. return StatusCode.Success;
  65. } else {
  66. OnFail();
  67. return StatusCode.Failure;
  68. }
  69. }
  70. return StatusCode.InProgress;
  71. }
  72. //---------------------------------------------------------------------------------------------------------------------
  73. void OnSuccess() {
  74. if (null==m_onSuccess)
  75. return;
  76. m_onSuccess(m_request);
  77. }
  78. //---------------------------------------------------------------------------------------------------------------------
  79. void OnFail() {
  80. if (null==m_onFail)
  81. return;
  82. m_onFail(m_request);
  83. }
  84. //---------------------------------------------------------------------------------------------------------------------
  85. Request<T> m_request;
  86. Action<Request<T>> m_onSuccess;
  87. Action<Request<T>> m_onFail;
  88. }
  89. } //Unity.RenderStreaming.Editor