e1acbbde17561376cca9a9fc5e282d79e67fa9e8.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, log, warn, AsyncQueue, _crd;
  4. _export("AsyncQueue", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. __checkObsolete__ = _cc.__checkObsolete__;
  9. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  10. log = _cc.log;
  11. warn = _cc.warn;
  12. }],
  13. execute: function () {
  14. _crd = true;
  15. _cclegacy._RF.push({}, "8c48bBN521JzIxhITJunFji", "AsyncQueue", undefined);
  16. __checkObsolete__(['log', 'warn']);
  17. /**
  18. * 异步队列处理
  19. * @example
  20. var queue: AsyncQueue = new AsyncQueue();
  21. queue.push((next: NextFunction, params: any, args: any) => {
  22. oops.res.load("language/font/" + oops.language.current, next);
  23. });
  24. queue.push((next: NextFunction, params: any, args: any) => {
  25. oops.res.loadDir("common", next);
  26. });
  27. queue.complete = () => {
  28. console.log("处理完成");
  29. };
  30. queue.play();
  31. */
  32. _export("AsyncQueue", AsyncQueue = class AsyncQueue {
  33. constructor() {
  34. // 正在运行的任务
  35. this._runningAsyncTask = null;
  36. this._queues = [];
  37. // 正在执行的异步任务标识
  38. this._isProcessingTaskUUID = 0;
  39. this._enable = true;
  40. /**
  41. * 任务队列完成回调
  42. */
  43. this.complete = null;
  44. }
  45. /** 任务队列 */
  46. get queues() {
  47. return this._queues;
  48. }
  49. /** 是否开启可用 */
  50. get enable() {
  51. return this._enable;
  52. }
  53. /** 是否开启可用 */
  54. set enable(val) {
  55. if (this._enable === val) {
  56. return;
  57. }
  58. this._enable = val;
  59. if (val && this.size > 0) {
  60. this.play();
  61. }
  62. }
  63. /**
  64. * 添加一个异步任务到队列中
  65. * @param callback 回调
  66. * @param params 参数
  67. */
  68. push(callback, params) {
  69. if (params === void 0) {
  70. params = null;
  71. }
  72. var uuid = AsyncQueue._$uuid_count++;
  73. this._queues.push({
  74. uuid: uuid,
  75. callbacks: [callback],
  76. params: params
  77. });
  78. return uuid;
  79. }
  80. /**
  81. * 添加多个任务,多个任务函数会同时执行
  82. * @param params 参数据
  83. * @param callbacks 回调
  84. * @returns
  85. */
  86. pushMulti(params) {
  87. var uuid = AsyncQueue._$uuid_count++;
  88. for (var _len = arguments.length, callbacks = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  89. callbacks[_key - 1] = arguments[_key];
  90. }
  91. this._queues.push({
  92. uuid: uuid,
  93. callbacks: callbacks,
  94. params: params
  95. });
  96. return uuid;
  97. }
  98. /**
  99. * 移除一个还未执行的异步任务
  100. * @param uuid 任务唯一编号
  101. */
  102. remove(uuid) {
  103. var _this$_runningAsyncTa;
  104. if (((_this$_runningAsyncTa = this._runningAsyncTask) == null ? void 0 : _this$_runningAsyncTa.uuid) === uuid) {
  105. warn("正在执行的任务不可以移除");
  106. return;
  107. }
  108. for (var i = 0; i < this._queues.length; i++) {
  109. if (this._queues[i].uuid === uuid) {
  110. this._queues.splice(i, 1);
  111. break;
  112. }
  113. }
  114. }
  115. /** 队列长度 */
  116. get size() {
  117. return this._queues.length;
  118. }
  119. /** 是否有正在处理的任务 */
  120. get isProcessing() {
  121. return this._isProcessingTaskUUID > 0;
  122. }
  123. /** 队列是否已停止 */
  124. get isStop() {
  125. if (this._queues.length > 0) {
  126. return false;
  127. }
  128. if (this.isProcessing) {
  129. return false;
  130. }
  131. return true;
  132. }
  133. /** 正在执行的任务参数 */
  134. get runningParams() {
  135. if (this._runningAsyncTask) {
  136. return this._runningAsyncTask.params;
  137. }
  138. return null;
  139. }
  140. /** 清空队列 */
  141. clear() {
  142. this._queues = [];
  143. this._isProcessingTaskUUID = 0;
  144. this._runningAsyncTask = null;
  145. }
  146. /** 跳过当前正在执行的任务 */
  147. step() {
  148. if (this.isProcessing) {
  149. this.next(this._isProcessingTaskUUID);
  150. }
  151. }
  152. /**
  153. * 开始运行队列
  154. * @param args 参数
  155. */
  156. play(args) {
  157. var _this = this;
  158. if (args === void 0) {
  159. args = null;
  160. }
  161. if (this.isProcessing) {
  162. return;
  163. }
  164. if (!this._enable) {
  165. return;
  166. }
  167. var actionData = this._queues.shift();
  168. if (actionData) {
  169. this._runningAsyncTask = actionData;
  170. var taskUUID = actionData.uuid;
  171. this._isProcessingTaskUUID = taskUUID;
  172. var callbacks = actionData.callbacks;
  173. if (callbacks.length == 1) {
  174. var nextFunc = function nextFunc(nextArgs) {
  175. if (nextArgs === void 0) {
  176. nextArgs = null;
  177. }
  178. _this.next(taskUUID, nextArgs);
  179. };
  180. callbacks[0](nextFunc, actionData.params, args);
  181. } else {
  182. // 多个任务函数同时执行
  183. var fnum = callbacks.length;
  184. var nextArgsArr = [];
  185. var _nextFunc = function _nextFunc(nextArgs) {
  186. if (nextArgs === void 0) {
  187. nextArgs = null;
  188. }
  189. --fnum;
  190. nextArgsArr.push(nextArgs || null);
  191. if (fnum === 0) {
  192. _this.next(taskUUID, nextArgsArr);
  193. }
  194. };
  195. var knum = fnum;
  196. for (var i = 0; i < knum; i++) {
  197. callbacks[i](_nextFunc, actionData.params, args);
  198. }
  199. }
  200. } else {
  201. this._isProcessingTaskUUID = 0;
  202. this._runningAsyncTask = null;
  203. if (this.complete) {
  204. this.complete(args);
  205. }
  206. }
  207. }
  208. /**
  209. * 往队列中push一个延时任务
  210. * @param time 毫秒时间
  211. * @param callback (可选参数)时间到了之后回调
  212. */
  213. yieldTime(time, callback) {
  214. if (callback === void 0) {
  215. callback = null;
  216. }
  217. var task = function task(next, params, args) {
  218. var _t = setTimeout(() => {
  219. clearTimeout(_t);
  220. if (callback) {
  221. callback();
  222. }
  223. next(args);
  224. }, time);
  225. };
  226. this.push(task, {
  227. des: "AsyncQueue.yieldTime"
  228. });
  229. }
  230. next(taskUUID, args) {
  231. if (args === void 0) {
  232. args = null;
  233. }
  234. if (this._isProcessingTaskUUID === taskUUID) {
  235. this._isProcessingTaskUUID = 0;
  236. this._runningAsyncTask = null;
  237. this.play(args);
  238. } else {
  239. if (this._runningAsyncTask) {
  240. log(this._runningAsyncTask);
  241. }
  242. }
  243. }
  244. /**
  245. * 返回一个执行函数,执行函数调用count次后,next将触发
  246. * @param count
  247. * @param next
  248. * @return 返回一个匿名函数
  249. */
  250. static excuteTimes(count, next) {
  251. if (next === void 0) {
  252. next = null;
  253. }
  254. var fnum = count;
  255. var call = () => {
  256. --fnum;
  257. if (fnum === 0) {
  258. next && next();
  259. }
  260. };
  261. return call;
  262. }
  263. });
  264. // 任务task的唯一标识
  265. AsyncQueue._$uuid_count = 1;
  266. _cclegacy._RF.pop();
  267. _crd = false;
  268. }
  269. };
  270. });
  271. //# sourceMappingURL=e1acbbde17561376cca9a9fc5e282d79e67fa9e8.js.map