43279b533a6131255cceabc61de6a306ee602b05.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. var id = 0;
  67. while (true) {
  68. var used = false;
  69. for (var 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, timeUnit) {
  104. if (count === void 0) {
  105. count = 1;
  106. }
  107. if (timeUnit === void 0) {
  108. timeUnit = TimeUnit.Second;
  109. }
  110. var tid = this.getTid();
  111. this.tempList.push(this.CreatNewTimeTask(tid, callback, delay, count, timeUnit)); //增加一个任务到缓存
  112. return tid;
  113. } //
  114. CreatNewTimeTask(tid, callback, delay, count, timeUnit) {
  115. if (count === void 0) {
  116. count = 1;
  117. }
  118. if (timeUnit === void 0) {
  119. timeUnit = TimeUnit.Second;
  120. }
  121. if (timeUnit != TimeUnit.Second) {
  122. //如果单位不是秒,就全换算成秒做为单位
  123. switch (timeUnit) {
  124. case TimeUnit.Millisecond:
  125. delay *= 0.001;
  126. break;
  127. case TimeUnit.Hour:
  128. delay *= 360;
  129. break;
  130. case TimeUnit.Day:
  131. delay *= 360 * 24;
  132. break;
  133. default:
  134. console.error("Add Task TimeUnit Type Error...");
  135. break;
  136. }
  137. }
  138. return new TimeTask(tid, callback, this.nowTime + delay, delay, count);
  139. }
  140. /**
  141. * 删除一个时间任务
  142. * @param tid 时间任务id
  143. * @returns 是否删除成功
  144. */
  145. DeleteTimeTask(tid) {
  146. var exist = false;
  147. var tt;
  148. for (var i = 0; i < this.tempList.length; i++) {
  149. tt = this.tempList[i];
  150. if (tt.tid == tid) {
  151. this.tempList[i].doDelete = true;
  152. exist = true;
  153. break;
  154. }
  155. }
  156. if (!exist) {
  157. for (var _i = 0; _i < this.taskList.length; _i++) {
  158. tt = this.taskList[_i];
  159. if (tt.tid == tid) {
  160. this.taskList[_i].doDelete = true;
  161. exist = true;
  162. break;
  163. }
  164. }
  165. }
  166. return exist;
  167. }
  168. //运行调用
  169. Run(dt) {
  170. this.nowTime += dt;
  171. this._count = this.tempList.length;
  172. if (this._count > 0) {
  173. for (this._index = 0; this._index < this._count; this._index++) {
  174. if (this.tempList[this._index].doDelete) continue;
  175. this.taskList.push(this.tempList[this._index]);
  176. }
  177. this.tempList.length = 0;
  178. }
  179. this._count = this.taskList.length;
  180. if (this._count > 0) {
  181. for (this._index = 0; this._index < this._count; this._index++) {
  182. this._tt = this.taskList[this._index];
  183. if (this.nowTime < this._tt.destTime) continue;
  184. if (this._tt.doDelete) continue;
  185. this._tt.callback();
  186. if (this._tt.count == 1) {
  187. this._tt.doDelete = true;
  188. } else if (this._tt.count > 0) {
  189. this._tt.count--;
  190. this._tt.destTime += this._tt.delay; //下次执行时间
  191. } else {
  192. this._tt.destTime += this._tt.delay;
  193. }
  194. }
  195. }
  196. this.taskList = this.taskList.filter(task => !task.doDelete); //把标记为删除的真正删除
  197. } //
  198. };
  199. _cclegacy._RF.pop();
  200. _crd = false;
  201. }
  202. };
  203. });
  204. //# sourceMappingURL=43279b533a6131255cceabc61de6a306ee602b05.js.map