System.register(["cc"], function (_export, _context) { "use strict"; var _cclegacy, TimeTask, TimeJobCenter, _crd, TimeUnit; function get_new_job() { return new TimeJobCenter(); } _export("default", get_new_job); return { setters: [function (_cc) { _cclegacy = _cc.cclegacy; }], execute: function () { _crd = true; _cclegacy._RF.push({}, "29a956DSGFLW6G8399hAoz8", "TimeJobCenter", undefined); /**任务*/ TimeTask = class TimeTask { constructor(tid, callback, destTime, delay, count) { this.tid = void 0; this.destTime = void 0; //延时执行时间 this.callback = void 0; //执行方法 this.count = void 0; //执行次数 this.delay = void 0; //间隔 this.doDelete = false; this.tid = tid; this.callback = callback; this.destTime = destTime; this.delay = delay; this.count = count; } }; /**时间单位*/ _export("TimeUnit", TimeUnit = /*#__PURE__*/function (TimeUnit) { TimeUnit[TimeUnit["Millisecond"] = 0] = "Millisecond"; TimeUnit[TimeUnit["Second"] = 1] = "Second"; TimeUnit[TimeUnit["Hour"] = 2] = "Hour"; TimeUnit[TimeUnit["Day"] = 3] = "Day"; return TimeUnit; }({})); /** * 时间定时任务 * 更好管理某一模块层级的时间任务,暂停,取消等 */ TimeJobCenter = class TimeJobCenter { constructor() { this.tid = 0; this.tempList = new Array(); this.taskList = new Array(); this.nowTime = 0; // this._index = void 0; this._count = void 0; this._tt = void 0; } Dispose() { this.taskList.length = 0; this.tempList.length = 0; this.nowTime = 0; } getTid() { //安全代码以防过了 if (this.tid == Number.MAX_VALUE) { var id = 0; while (true) { var used = false; for (var index = 0; index < this.taskList.length; index++) { if (this.taskList[index].tid == id) { used = true; break; } } if (!used) { break; } else { id++; } } return id; } else { this.tid += 1; return this.tid; } } /**延迟(ms)*/ delay(ms) { return new Promise(resolve => this.AddTimeTask(resolve, ms, 1, TimeUnit.Millisecond)); } /**延迟(s)*/ delay_second(ms) { return new Promise(resolve => this.AddTimeTask(resolve, ms, 1, TimeUnit.Second)); } /** * 增加时间任务 * @param callback 执行方法 * @param delay 延迟时间 * @param count 次数 * @param timeUnit 时间单位 默认为 秒 TimeUnit.Second * @returns 时间任务id */ AddTimeTask(callback, delay, count, timeUnit) { if (count === void 0) { count = 1; } if (timeUnit === void 0) { timeUnit = TimeUnit.Second; } var tid = this.getTid(); this.tempList.push(this.CreatNewTimeTask(tid, callback, delay, count, timeUnit)); //增加一个任务到缓存 return tid; } // CreatNewTimeTask(tid, callback, delay, count, timeUnit) { if (count === void 0) { count = 1; } if (timeUnit === void 0) { timeUnit = TimeUnit.Second; } if (timeUnit != TimeUnit.Second) { //如果单位不是秒,就全换算成秒做为单位 switch (timeUnit) { case TimeUnit.Millisecond: delay *= 0.001; break; case TimeUnit.Hour: delay *= 360; break; case TimeUnit.Day: delay *= 360 * 24; break; default: console.error("Add Task TimeUnit Type Error..."); break; } } return new TimeTask(tid, callback, this.nowTime + delay, delay, count); } /** * 删除一个时间任务 * @param tid 时间任务id * @returns 是否删除成功 */ DeleteTimeTask(tid) { var exist = false; var tt; for (var i = 0; i < this.tempList.length; i++) { tt = this.tempList[i]; if (tt.tid == tid) { this.tempList[i].doDelete = true; exist = true; break; } } if (!exist) { for (var _i = 0; _i < this.taskList.length; _i++) { tt = this.taskList[_i]; if (tt.tid == tid) { this.taskList[_i].doDelete = true; exist = true; break; } } } return exist; } //运行调用 Run(dt) { this.nowTime += dt; this._count = this.tempList.length; if (this._count > 0) { for (this._index = 0; this._index < this._count; this._index++) { if (this.tempList[this._index].doDelete) continue; this.taskList.push(this.tempList[this._index]); } this.tempList.length = 0; } this._count = this.taskList.length; if (this._count > 0) { for (this._index = 0; this._index < this._count; this._index++) { this._tt = this.taskList[this._index]; if (this.nowTime < this._tt.destTime) continue; if (this._tt.doDelete) continue; this._tt.callback(); if (this._tt.count == 1) { this._tt.doDelete = true; } else if (this._tt.count > 0) { this._tt.count--; this._tt.destTime += this._tt.delay; //下次执行时间 } else { this._tt.destTime += this._tt.delay; } } } this.taskList = this.taskList.filter(task => !task.doDelete); //把标记为删除的真正删除 } // }; _cclegacy._RF.pop(); _crd = false; } }; }); //# sourceMappingURL=34df8999c17a8450e101699bd789ccbd57ce24ce.js.map