d1c6250fd5c984c31bfbba3e31ba75993a1af01d.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 = null) {
  69. let uuid = AsyncQueue._$uuid_count++;
  70. this._queues.push({
  71. uuid: uuid,
  72. callbacks: [callback],
  73. params: params
  74. });
  75. return uuid;
  76. }
  77. /**
  78. * 添加多个任务,多个任务函数会同时执行
  79. * @param params 参数据
  80. * @param callbacks 回调
  81. * @returns
  82. */
  83. pushMulti(params, ...callbacks) {
  84. let uuid = AsyncQueue._$uuid_count++;
  85. this._queues.push({
  86. uuid: uuid,
  87. callbacks: callbacks,
  88. params: params
  89. });
  90. return uuid;
  91. }
  92. /**
  93. * 移除一个还未执行的异步任务
  94. * @param uuid 任务唯一编号
  95. */
  96. remove(uuid) {
  97. var _this$_runningAsyncTa;
  98. if (((_this$_runningAsyncTa = this._runningAsyncTask) == null ? void 0 : _this$_runningAsyncTa.uuid) === uuid) {
  99. warn("正在执行的任务不可以移除");
  100. return;
  101. }
  102. for (let i = 0; i < this._queues.length; i++) {
  103. if (this._queues[i].uuid === uuid) {
  104. this._queues.splice(i, 1);
  105. break;
  106. }
  107. }
  108. }
  109. /** 队列长度 */
  110. get size() {
  111. return this._queues.length;
  112. }
  113. /** 是否有正在处理的任务 */
  114. get isProcessing() {
  115. return this._isProcessingTaskUUID > 0;
  116. }
  117. /** 队列是否已停止 */
  118. get isStop() {
  119. if (this._queues.length > 0) {
  120. return false;
  121. }
  122. if (this.isProcessing) {
  123. return false;
  124. }
  125. return true;
  126. }
  127. /** 正在执行的任务参数 */
  128. get runningParams() {
  129. if (this._runningAsyncTask) {
  130. return this._runningAsyncTask.params;
  131. }
  132. return null;
  133. }
  134. /** 清空队列 */
  135. clear() {
  136. this._queues = [];
  137. this._isProcessingTaskUUID = 0;
  138. this._runningAsyncTask = null;
  139. }
  140. /** 跳过当前正在执行的任务 */
  141. step() {
  142. if (this.isProcessing) {
  143. this.next(this._isProcessingTaskUUID);
  144. }
  145. }
  146. /**
  147. * 开始运行队列
  148. * @param args 参数
  149. */
  150. play(args = null) {
  151. if (this.isProcessing) {
  152. return;
  153. }
  154. if (!this._enable) {
  155. return;
  156. }
  157. let actionData = this._queues.shift();
  158. if (actionData) {
  159. this._runningAsyncTask = actionData;
  160. let taskUUID = actionData.uuid;
  161. this._isProcessingTaskUUID = taskUUID;
  162. let callbacks = actionData.callbacks;
  163. if (callbacks.length == 1) {
  164. let nextFunc = (nextArgs = null) => {
  165. this.next(taskUUID, nextArgs);
  166. };
  167. callbacks[0](nextFunc, actionData.params, args);
  168. } else {
  169. // 多个任务函数同时执行
  170. let fnum = callbacks.length;
  171. let nextArgsArr = [];
  172. let nextFunc = (nextArgs = null) => {
  173. --fnum;
  174. nextArgsArr.push(nextArgs || null);
  175. if (fnum === 0) {
  176. this.next(taskUUID, nextArgsArr);
  177. }
  178. };
  179. let knum = fnum;
  180. for (let i = 0; i < knum; i++) {
  181. callbacks[i](nextFunc, actionData.params, args);
  182. }
  183. }
  184. } else {
  185. this._isProcessingTaskUUID = 0;
  186. this._runningAsyncTask = null;
  187. if (this.complete) {
  188. this.complete(args);
  189. }
  190. }
  191. }
  192. /**
  193. * 往队列中push一个延时任务
  194. * @param time 毫秒时间
  195. * @param callback (可选参数)时间到了之后回调
  196. */
  197. yieldTime(time, callback = null) {
  198. let task = function (next, params, args) {
  199. let _t = setTimeout(() => {
  200. clearTimeout(_t);
  201. if (callback) {
  202. callback();
  203. }
  204. next(args);
  205. }, time);
  206. };
  207. this.push(task, {
  208. des: "AsyncQueue.yieldTime"
  209. });
  210. }
  211. next(taskUUID, args = null) {
  212. if (this._isProcessingTaskUUID === taskUUID) {
  213. this._isProcessingTaskUUID = 0;
  214. this._runningAsyncTask = null;
  215. this.play(args);
  216. } else {
  217. if (this._runningAsyncTask) {
  218. log(this._runningAsyncTask);
  219. }
  220. }
  221. }
  222. /**
  223. * 返回一个执行函数,执行函数调用count次后,next将触发
  224. * @param count
  225. * @param next
  226. * @return 返回一个匿名函数
  227. */
  228. static excuteTimes(count, next = null) {
  229. let fnum = count;
  230. let call = () => {
  231. --fnum;
  232. if (fnum === 0) {
  233. next && next();
  234. }
  235. };
  236. return call;
  237. }
  238. });
  239. // 任务task的唯一标识
  240. AsyncQueue._$uuid_count = 1;
  241. _cclegacy._RF.pop();
  242. _crd = false;
  243. }
  244. };
  245. });
  246. //# sourceMappingURL=d1c6250fd5c984c31bfbba3e31ba75993a1af01d.js.map