123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- 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
|