745960c08f2ea47190520a8dce54e888cde023cc.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, TimeTask, TimeJobCenter, _crd, TimeUnit;
  4. function get_new_job() {
  5. return new TimeJobCenter();
  6. }
  7. _export("default", get_new_job);
  8. return {
  9. setters: [function (_cc) {
  10. _cclegacy = _cc.cclegacy;
  11. }],
  12. execute: function () {
  13. _crd = true;
  14. _cclegacy._RF.push({}, "29a956DSGFLW6G8399hAoz8", "TimeJobCenter", undefined);
  15. /**任务*/
  16. TimeTask = class TimeTask {
  17. constructor(tid, callback, destTime, delay, count) {
  18. this.tid = void 0;
  19. this.destTime = void 0;
  20. //延时执行时间
  21. this.callback = void 0;
  22. //执行方法
  23. this.count = void 0;
  24. //执行次数
  25. this.delay = void 0;
  26. //间隔
  27. this.doDelete = false;
  28. this.tid = tid;
  29. this.callback = callback;
  30. this.destTime = destTime;
  31. this.delay = delay;
  32. this.count = count;
  33. }
  34. };
  35. /**时间单位*/
  36. _export("TimeUnit", TimeUnit = /*#__PURE__*/function (TimeUnit) {
  37. TimeUnit[TimeUnit["Millisecond"] = 0] = "Millisecond";
  38. TimeUnit[TimeUnit["Second"] = 1] = "Second";
  39. TimeUnit[TimeUnit["Hour"] = 2] = "Hour";
  40. TimeUnit[TimeUnit["Day"] = 3] = "Day";
  41. return TimeUnit;
  42. }({}));
  43. /**
  44. * 时间定时任务
  45. * 更好管理某一模块层级的时间任务,暂停,取消等
  46. */
  47. TimeJobCenter = class TimeJobCenter {
  48. constructor() {
  49. this.tid = 0;
  50. this.tempList = new Array();
  51. this.taskList = new Array();
  52. this.nowTime = 0;
  53. //
  54. this._index = void 0;
  55. this._count = void 0;
  56. this._tt = void 0;
  57. }
  58. Dispose() {
  59. this.taskList.length = 0;
  60. this.tempList.length = 0;
  61. this.nowTime = 0;
  62. }
  63. getTid() {
  64. //安全代码以防过了
  65. if (this.tid == Number.MAX_VALUE) {
  66. let id = 0;
  67. while (true) {
  68. let used = false;
  69. for (let index = 0; index < this.taskList.length; index++) {
  70. if (this.taskList[index].tid == id) {
  71. used = true;
  72. break;
  73. }
  74. }
  75. if (!used) {
  76. break;
  77. } else {
  78. id++;
  79. }
  80. }
  81. return id;
  82. } else {
  83. this.tid += 1;
  84. return this.tid;
  85. }
  86. }
  87. /**延迟(ms)*/
  88. delay(ms) {
  89. return new Promise(resolve => this.AddTimeTask(resolve, ms, 1, TimeUnit.Millisecond));
  90. }
  91. /**延迟(s)*/
  92. delay_second(ms) {
  93. return new Promise(resolve => this.AddTimeTask(resolve, ms, 1, TimeUnit.Second));
  94. }
  95. /**
  96. * 增加时间任务
  97. * @param callback 执行方法
  98. * @param delay 延迟时间
  99. * @param count 次数
  100. * @param timeUnit 时间单位 默认为 秒 TimeUnit.Second
  101. * @returns 时间任务id
  102. */
  103. AddTimeTask(callback, delay, count = 1, timeUnit = TimeUnit.Second) {
  104. let tid = this.getTid();
  105. this.tempList.push(this.CreatNewTimeTask(tid, callback, delay, count, timeUnit)); //增加一个任务到缓存
  106. return tid;
  107. } //
  108. CreatNewTimeTask(tid, callback, delay, count = 1, timeUnit = TimeUnit.Second) {
  109. if (timeUnit != TimeUnit.Second) {
  110. //如果单位不是秒,就全换算成秒做为单位
  111. switch (timeUnit) {
  112. case TimeUnit.Millisecond:
  113. delay *= 0.001;
  114. break;
  115. case TimeUnit.Hour:
  116. delay *= 360;
  117. break;
  118. case TimeUnit.Day:
  119. delay *= 360 * 24;
  120. break;
  121. default:
  122. console.error("Add Task TimeUnit Type Error...");
  123. break;
  124. }
  125. }
  126. return new TimeTask(tid, callback, this.nowTime + delay, delay, count);
  127. }
  128. /**
  129. * 删除一个时间任务
  130. * @param tid 时间任务id
  131. * @returns 是否删除成功
  132. */
  133. DeleteTimeTask(tid) {
  134. let exist = false;
  135. let tt;
  136. for (let i = 0; i < this.tempList.length; i++) {
  137. tt = this.tempList[i];
  138. if (tt.tid == tid) {
  139. this.tempList[i].doDelete = true;
  140. exist = true;
  141. break;
  142. }
  143. }
  144. if (!exist) {
  145. for (let i = 0; i < this.taskList.length; i++) {
  146. tt = this.taskList[i];
  147. if (tt.tid == tid) {
  148. this.taskList[i].doDelete = true;
  149. exist = true;
  150. break;
  151. }
  152. }
  153. }
  154. return exist;
  155. }
  156. //运行调用
  157. Run(dt) {
  158. this.nowTime += dt;
  159. this._count = this.tempList.length;
  160. if (this._count > 0) {
  161. for (this._index = 0; this._index < this._count; this._index++) {
  162. if (this.tempList[this._index].doDelete) continue;
  163. this.taskList.push(this.tempList[this._index]);
  164. }
  165. this.tempList.length = 0;
  166. }
  167. this._count = this.taskList.length;
  168. if (this._count > 0) {
  169. for (this._index = 0; this._index < this._count; this._index++) {
  170. this._tt = this.taskList[this._index];
  171. if (this.nowTime < this._tt.destTime) continue;
  172. if (this._tt.doDelete) continue;
  173. this._tt.callback();
  174. if (this._tt.count == 1) {
  175. this._tt.doDelete = true;
  176. } else if (this._tt.count > 0) {
  177. this._tt.count--;
  178. this._tt.destTime += this._tt.delay; //下次执行时间
  179. } else {
  180. this._tt.destTime += this._tt.delay;
  181. }
  182. }
  183. }
  184. this.taskList = this.taskList.filter(task => !task.doDelete); //把标记为删除的真正删除
  185. } //
  186. };
  187. _cclegacy._RF.pop();
  188. _crd = false;
  189. }
  190. };
  191. });
  192. //# sourceMappingURL=745960c08f2ea47190520a8dce54e888cde023cc.js.map