Async.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. namespace XGame.Framework.Asyncs
  3. {
  4. public abstract class Async : IAsync
  5. {
  6. private AsyncListenerOperation _operation;
  7. public object State { get; set; }
  8. public Exception Exception { get; set; }
  9. public bool IsCompleted { get; protected set; }
  10. public virtual float Progress { get; protected set; }
  11. public Async()
  12. {
  13. Progress = 0;
  14. _operation = new AsyncListenerOperation();
  15. }
  16. public virtual void Completed()
  17. {
  18. if (IsCompleted)
  19. {
  20. Log.Warn("Connot Completed Again!!");
  21. return;
  22. }
  23. IsCompleted = true;
  24. Progress = 1.0f;
  25. _operation.OnCompleted(this);
  26. }
  27. public IAsync On(OnAsyncCompleted action)
  28. {
  29. if (action != null)
  30. {
  31. if (IsCompleted)
  32. {
  33. action.SaveInvoke(this);
  34. }
  35. else
  36. {
  37. _operation.On(action);
  38. }
  39. }
  40. return this;
  41. }
  42. public IAsync On(IAsyncListener listener, int order = 0)
  43. {
  44. if (listener == null) return this;
  45. if (IsCompleted)
  46. {
  47. listener.SaveInvoke(this);
  48. return this;
  49. }
  50. _operation.On(listener, order);
  51. return this;
  52. }
  53. public IAsync RemoveOn(OnAsyncCompleted action)
  54. {
  55. _operation.RemoveOn(action);
  56. return this;
  57. }
  58. public IAsync RemoveOn(IAsyncListener listener)
  59. {
  60. _operation.RemoveOn(listener);
  61. return this;
  62. }
  63. public IAsync RemoveAll()
  64. {
  65. OnRemoveAll();
  66. _operation.Clear();
  67. return this;
  68. }
  69. protected virtual void OnRemoveAll()
  70. {
  71. }
  72. }
  73. }