| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- System.register(["cc"], function (_export, _context) {
- "use strict";
- var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, log, warn, AsyncQueue, _crd;
- _export("AsyncQueue", void 0);
- return {
- setters: [function (_cc) {
- _cclegacy = _cc.cclegacy;
- __checkObsolete__ = _cc.__checkObsolete__;
- __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
- log = _cc.log;
- warn = _cc.warn;
- }],
- execute: function () {
- _crd = true;
- _cclegacy._RF.push({}, "8c48bBN521JzIxhITJunFji", "AsyncQueue", undefined);
- __checkObsolete__(['log', 'warn']);
- /**
- * 异步队列处理
- * @example
- var queue: AsyncQueue = new AsyncQueue();
- queue.push((next: NextFunction, params: any, args: any) => {
- oops.res.load("language/font/" + oops.language.current, next);
- });
- queue.push((next: NextFunction, params: any, args: any) => {
- oops.res.loadDir("common", next);
- });
- queue.complete = () => {
- console.log("处理完成");
- };
- queue.play();
- */
- _export("AsyncQueue", AsyncQueue = class AsyncQueue {
- constructor() {
- // 正在运行的任务
- this._runningAsyncTask = null;
- this._queues = [];
- // 正在执行的异步任务标识
- this._isProcessingTaskUUID = 0;
- this._enable = true;
- /**
- * 任务队列完成回调
- */
- this.complete = null;
- }
- /** 任务队列 */
- get queues() {
- return this._queues;
- }
- /** 是否开启可用 */
- get enable() {
- return this._enable;
- }
- /** 是否开启可用 */
- set enable(val) {
- if (this._enable === val) {
- return;
- }
- this._enable = val;
- if (val && this.size > 0) {
- this.play();
- }
- }
- /**
- * 添加一个异步任务到队列中
- * @param callback 回调
- * @param params 参数
- */
- push(callback, params) {
- if (params === void 0) {
- params = null;
- }
- var uuid = AsyncQueue._$uuid_count++;
- this._queues.push({
- uuid: uuid,
- callbacks: [callback],
- params: params
- });
- return uuid;
- }
- /**
- * 添加多个任务,多个任务函数会同时执行
- * @param params 参数据
- * @param callbacks 回调
- * @returns
- */
- pushMulti(params) {
- var uuid = AsyncQueue._$uuid_count++;
- for (var _len = arguments.length, callbacks = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
- callbacks[_key - 1] = arguments[_key];
- }
- this._queues.push({
- uuid: uuid,
- callbacks: callbacks,
- params: params
- });
- return uuid;
- }
- /**
- * 移除一个还未执行的异步任务
- * @param uuid 任务唯一编号
- */
- remove(uuid) {
- var _this$_runningAsyncTa;
- if (((_this$_runningAsyncTa = this._runningAsyncTask) == null ? void 0 : _this$_runningAsyncTa.uuid) === uuid) {
- warn("正在执行的任务不可以移除");
- return;
- }
- for (var i = 0; i < this._queues.length; i++) {
- if (this._queues[i].uuid === uuid) {
- this._queues.splice(i, 1);
- break;
- }
- }
- }
- /** 队列长度 */
- get size() {
- return this._queues.length;
- }
- /** 是否有正在处理的任务 */
- get isProcessing() {
- return this._isProcessingTaskUUID > 0;
- }
- /** 队列是否已停止 */
- get isStop() {
- if (this._queues.length > 0) {
- return false;
- }
- if (this.isProcessing) {
- return false;
- }
- return true;
- }
- /** 正在执行的任务参数 */
- get runningParams() {
- if (this._runningAsyncTask) {
- return this._runningAsyncTask.params;
- }
- return null;
- }
- /** 清空队列 */
- clear() {
- this._queues = [];
- this._isProcessingTaskUUID = 0;
- this._runningAsyncTask = null;
- }
- /** 跳过当前正在执行的任务 */
- step() {
- if (this.isProcessing) {
- this.next(this._isProcessingTaskUUID);
- }
- }
- /**
- * 开始运行队列
- * @param args 参数
- */
- play(args) {
- var _this = this;
- if (args === void 0) {
- args = null;
- }
- if (this.isProcessing) {
- return;
- }
- if (!this._enable) {
- return;
- }
- var actionData = this._queues.shift();
- if (actionData) {
- this._runningAsyncTask = actionData;
- var taskUUID = actionData.uuid;
- this._isProcessingTaskUUID = taskUUID;
- var callbacks = actionData.callbacks;
- if (callbacks.length == 1) {
- var nextFunc = function nextFunc(nextArgs) {
- if (nextArgs === void 0) {
- nextArgs = null;
- }
- _this.next(taskUUID, nextArgs);
- };
- callbacks[0](nextFunc, actionData.params, args);
- } else {
- // 多个任务函数同时执行
- var fnum = callbacks.length;
- var nextArgsArr = [];
- var _nextFunc = function _nextFunc(nextArgs) {
- if (nextArgs === void 0) {
- nextArgs = null;
- }
- --fnum;
- nextArgsArr.push(nextArgs || null);
- if (fnum === 0) {
- _this.next(taskUUID, nextArgsArr);
- }
- };
- var knum = fnum;
- for (var i = 0; i < knum; i++) {
- callbacks[i](_nextFunc, actionData.params, args);
- }
- }
- } else {
- this._isProcessingTaskUUID = 0;
- this._runningAsyncTask = null;
- if (this.complete) {
- this.complete(args);
- }
- }
- }
- /**
- * 往队列中push一个延时任务
- * @param time 毫秒时间
- * @param callback (可选参数)时间到了之后回调
- */
- yieldTime(time, callback) {
- if (callback === void 0) {
- callback = null;
- }
- var task = function task(next, params, args) {
- var _t = setTimeout(() => {
- clearTimeout(_t);
- if (callback) {
- callback();
- }
- next(args);
- }, time);
- };
- this.push(task, {
- des: "AsyncQueue.yieldTime"
- });
- }
- next(taskUUID, args) {
- if (args === void 0) {
- args = null;
- }
- if (this._isProcessingTaskUUID === taskUUID) {
- this._isProcessingTaskUUID = 0;
- this._runningAsyncTask = null;
- this.play(args);
- } else {
- if (this._runningAsyncTask) {
- log(this._runningAsyncTask);
- }
- }
- }
- /**
- * 返回一个执行函数,执行函数调用count次后,next将触发
- * @param count
- * @param next
- * @return 返回一个匿名函数
- */
- static excuteTimes(count, next) {
- if (next === void 0) {
- next = null;
- }
- var fnum = count;
- var call = () => {
- --fnum;
- if (fnum === 0) {
- next && next();
- }
- };
- return call;
- }
- });
- // 任务task的唯一标识
- AsyncQueue._$uuid_count = 1;
- _cclegacy._RF.pop();
- _crd = false;
- }
- };
- });
- //# sourceMappingURL=e1acbbde17561376cca9a9fc5e282d79e67fa9e8.js.map
|