123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- import { Framework } from "../Framework";
- export type NextFunction = (nextArgs?: any) => void;
- export type AsyncCallback = (next: NextFunction, params: any, args: any) => void;
- interface AsyncTask {
-
- uuid: number;
-
- callbacks: Array<AsyncCallback>;
-
- params: any
- }
- export class AsyncQueue {
- private _runningAsyncTask: AsyncTask = null;
- private static _$uuid_count: number = 1;
- private _queues: Array<AsyncTask> = [];
- complete: Function | null = null;
- get queues(): Array<AsyncTask> {
- return this._queues;
- }
- private _isProcessingTaskUUID: number = 0;
- private _enable: boolean = true;
-
- get enable() {
- return this._enable;
- }
-
- set enable(val: boolean) {
- if (this._enable !== val) {
- this._enable = val;
- if (val && this.size > 0) {
- this.play();
- }
- }
- }
-
- push(callback: AsyncCallback, params: any = null) {
- let uuid = ++AsyncQueue._$uuid_count;
- this._queues.push({
- uuid: uuid,
- callbacks: [callback],
- params: params
- })
- return uuid;
- }
-
- pushMulti(params: any, ...callbacks: AsyncCallback[]) {
- let uuid = ++AsyncQueue._$uuid_count;
- this._queues.push({
- uuid: uuid,
- callbacks: callbacks,
- params: params
- })
- return uuid;
- }
-
- remove(uuid: number) {
- if (this._runningAsyncTask?.uuid === uuid) {
- console.error("正在执行的任务不可以移除");
- return;
- }
- for (let i = 0; i < this._queues.length; ++i) {
- if (this._queues[i].uuid === uuid) {
- this._queues.splice(i, 1);
- break;
- }
- }
- }
-
- get size(): number {
- return this._queues.length;
- }
-
- get isProcessing(): boolean {
- return this._isProcessingTaskUUID > 0;
- }
-
- get isStop(): boolean {
- return (this._queues.length > 0 || this.isProcessing) ? false : true;
- }
-
- get runningParams() {
- return this._runningAsyncTask ? this._runningAsyncTask.params : null;
- }
-
- clear() {
- this._queues = [];
- this._isProcessingTaskUUID = 0;
- this._runningAsyncTask = null;
- }
- protected _next_task(taskUUID: number, args: any = null) {
- if (this._isProcessingTaskUUID === taskUUID) {
- this._isProcessingTaskUUID = 0;
- this._runningAsyncTask = null;
- this.play(args);
- } else {
- if (this._runningAsyncTask) {
- console.log(this._runningAsyncTask);
- }
- }
- }
-
- step() {
- (this.isProcessing) && this._next_task(this._isProcessingTaskUUID);
- }
-
- play(args: any = null) {
- if (this.isProcessing || !this._enable) {
- return;
- }
- let actionData: AsyncTask = this._queues.shift()!;
- if (actionData) {
- this._runningAsyncTask = actionData;
- let taskUUID: number = actionData.uuid;
- this._isProcessingTaskUUID = taskUUID;
- let callbacks: Array<AsyncCallback> = actionData.callbacks;
- if (callbacks.length == 1) {
- let nextFunc: NextFunction = (nextArgs: any = null) => {
- this._next_task(taskUUID, nextArgs);
- }
- callbacks[0](nextFunc, actionData.params, args);
- } else {
-
- let fnum: number = callbacks.length;
- let nextArgsArr: any[] = [];
- let nextFunc: NextFunction = (nextArgs: any = null) => {
- --fnum;
- nextArgsArr.push(nextArgs || null);
- if (fnum === 0) {
- this._next_task(taskUUID, nextArgsArr);
- }
- }
- let knum = fnum;
- for (let i = 0; i < knum; ++i) {
- callbacks[i](nextFunc, actionData.params, args);
- }
- }
- } else {
- this._isProcessingTaskUUID = 0;
- this._runningAsyncTask = null;
- (this.complete) && this.complete(args);
- }
- }
-
- yieldTime(time: number, callback: Function = null) {
- let task = function (next: Function, params: any, args: any) {
- Framework.time.scheduleOnce(() => {
- callback && callback();
- next(args);
- }, time);
- }
- this.push(task, { des: "AsyncQueue.yieldTime" });
- }
-
- static excuteTimes(count: number, next: Function = null) {
- let fnum: number = count;
- let tempCall = () => {
- --fnum;
- if (fnum === 0) {
- next && next();
- }
- }
- return tempCall;
- }
- }
|