AsyncOperationBase.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. namespace YooAsset
  6. {
  7. public abstract class AsyncOperationBase : IEnumerator, IComparable<AsyncOperationBase>
  8. {
  9. // 用户请求的回调
  10. private Action<AsyncOperationBase> _callback;
  11. // 是否已经完成
  12. internal bool IsFinish = false;
  13. /// <summary>
  14. /// 所属包裹
  15. /// </summary>
  16. public string PackageName { private set; get; }
  17. /// <summary>
  18. /// 优先级
  19. /// </summary>
  20. public uint Priority { set; get; } = 0;
  21. /// <summary>
  22. /// 状态
  23. /// </summary>
  24. public EOperationStatus Status { get; protected set; } = EOperationStatus.None;
  25. /// <summary>
  26. /// 错误信息
  27. /// </summary>
  28. public string Error { get; protected set; }
  29. /// <summary>
  30. /// 处理进度
  31. /// </summary>
  32. public float Progress { get; protected set; }
  33. /// <summary>
  34. /// 是否已经完成
  35. /// </summary>
  36. public bool IsDone
  37. {
  38. get
  39. {
  40. return Status == EOperationStatus.Failed || Status == EOperationStatus.Succeed;
  41. }
  42. }
  43. /// <summary>
  44. /// 完成事件
  45. /// </summary>
  46. public event Action<AsyncOperationBase> Completed
  47. {
  48. add
  49. {
  50. if (IsDone)
  51. value.Invoke(this);
  52. else
  53. _callback += value;
  54. }
  55. remove
  56. {
  57. _callback -= value;
  58. }
  59. }
  60. /// <summary>
  61. /// 异步操作任务
  62. /// </summary>
  63. public Task Task
  64. {
  65. get
  66. {
  67. if (_taskCompletionSource == null)
  68. {
  69. _taskCompletionSource = new TaskCompletionSource<object>();
  70. if (IsDone)
  71. _taskCompletionSource.SetResult(null);
  72. }
  73. return _taskCompletionSource.Task;
  74. }
  75. }
  76. internal abstract void InternalOnStart();
  77. internal abstract void InternalOnUpdate();
  78. internal virtual void InternalOnAbort() { }
  79. internal void SetPackageName(string packageName)
  80. {
  81. PackageName = packageName;
  82. }
  83. internal void SetStart()
  84. {
  85. Status = EOperationStatus.Processing;
  86. InternalOnStart();
  87. }
  88. internal void SetFinish()
  89. {
  90. IsFinish = true;
  91. // 进度百分百完成
  92. Progress = 1f;
  93. //注意:如果完成回调内发生异常,会导致Task无限期等待
  94. _callback?.Invoke(this);
  95. if (_taskCompletionSource != null)
  96. _taskCompletionSource.TrySetResult(null);
  97. }
  98. internal void SetAbort()
  99. {
  100. if (IsDone == false)
  101. {
  102. Status = EOperationStatus.Failed;
  103. Error = "user abort";
  104. YooLogger.Warning($"Async operaiton has been abort : {this.GetType().Name}");
  105. InternalOnAbort();
  106. }
  107. }
  108. /// <summary>
  109. /// 清空完成回调
  110. /// </summary>
  111. protected void ClearCompletedCallback()
  112. {
  113. _callback = null;
  114. }
  115. #region 排序接口实现
  116. public int CompareTo(AsyncOperationBase other)
  117. {
  118. return other.Priority.CompareTo(this.Priority);
  119. }
  120. #endregion
  121. #region 异步编程相关
  122. bool IEnumerator.MoveNext()
  123. {
  124. return !IsDone;
  125. }
  126. void IEnumerator.Reset()
  127. {
  128. }
  129. object IEnumerator.Current => null;
  130. private TaskCompletionSource<object> _taskCompletionSource;
  131. #endregion
  132. }
  133. }