ETCancellationTokenSource.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Collections.Generic;
  2. namespace ET
  3. {
  4. [ObjectSystem]
  5. public class ETCancellationTokenSourceAwakeSystem: AwakeSystem<ETCancellationTokenSource>
  6. {
  7. public override void Awake(ETCancellationTokenSource self)
  8. {
  9. }
  10. }
  11. [ObjectSystem]
  12. public class ETCancellationTokenSourceAwake2System: AwakeSystem<ETCancellationTokenSource, long>
  13. {
  14. public override void Awake(ETCancellationTokenSource self, long afterTimeCancel)
  15. {
  16. self.CancelAfter(afterTimeCancel).Coroutine();
  17. }
  18. }
  19. [ObjectSystem]
  20. public class ETCancellationTokenSourceDestroySystem: DestroySystem<ETCancellationTokenSource>
  21. {
  22. public override void Destroy(ETCancellationTokenSource self)
  23. {
  24. self.cancellationTokens.Clear();
  25. }
  26. }
  27. public class ETCancellationTokenSource: Entity
  28. {
  29. public readonly List<ETCancellationToken> cancellationTokens = new List<ETCancellationToken>();
  30. public void Cancel()
  31. {
  32. foreach (ETCancellationToken token in this.cancellationTokens)
  33. {
  34. token.Cancel();
  35. }
  36. this.Dispose();
  37. }
  38. public async ETVoid CancelAfter(long afterTimeCancel)
  39. {
  40. long instanceId = this.InstanceId;
  41. await TimerComponent.Instance.WaitAsync(afterTimeCancel);
  42. if (this.InstanceId != instanceId)
  43. {
  44. return;
  45. }
  46. this.Dispose();
  47. }
  48. public ETCancellationToken Token
  49. {
  50. get
  51. {
  52. ETCancellationToken etCancellationToken = EntityFactory.Create<ETCancellationToken>(this.Domain);
  53. this.cancellationTokens.Add(etCancellationToken);
  54. etCancellationToken.Parent = this;
  55. return etCancellationToken;
  56. }
  57. }
  58. }
  59. }