Samwey—–优雅封装Promise------JTCounter、JTTaskCounter

在上一次的JTLocker再进行继承封装,想实现的效果就是可以做一个异步任务对列计数器

module com 
{
    /**
     * 计数器
     */
    export class JTCounter extends JTLocker implements JTICounter
    {
        /*
        *失败次数
        */
        protected _failCount:number = 0;
        /*
        *成功次数
        */
        protected _succeedCount:number = 0;
        /*
        *已经完成的次数
        */
        protected _lockedCount:number = 0;

        constructor()
        {
            super();
        }

        /**
         * 释放锁
         */
        public unlock(args?:any):void
        {
            super.unlock(args);
            super.clear();
            this._succeedCount ++;
            this._lockedCount ++;
        }

        /**
         * 强制性解锁--取消锁
         */
        public kill(args?:any):void
        {
            super.kill(args);
            super.clear();
            this._failCount ++;
            this._lockedCount ++;
        }

        public release(): void 
        {
            this.recycle();
        }

        /**
         * 成功的次数
         */
        public get succeedCount():number
        {
            return this._succeedCount;
        }
        /**
         * 失败的次数
         */
        public get failCount():number
        {
            return this._failCount;
        }

        public recycle() 
        {
            super.recycle();
            this._failCount = this._lockedCount = this._succeedCount = 0;
        }

        /**
         * 重置
         */
        public reset():void
        {
            this.recycle();
        }

        /**
         * 当前执行的次数
         */
        public get lockedCount():number
        {
            return this._lockedCount;
        }
   
    }
}

JTICounter —接口

/*
* name;
*/

module com
{
    export interface JTICounter extends JTILocker
    {
       succeedCount:number
        failCount:number    
        reset():void;
        lockedCount:number;     
    }
}

   JTTaskCounter 实现
  module com
  {
    /**
     * 任务计数器
     */

    export abstract class JTTaskCounter extends JTCounter
    {
        /*
         * 总共需要执行多少次
         */
        private _totalCount:number = 0;
        constructor()
        {
            super();
        }

        /**
         * 设置总共需要执行多少次
         * @param totalCount 总次数
         */
        public setTotalCount(totalCount:number):void
        {
            this._totalCount = totalCount;
        }

        /**
         * 总次数
         */

        public get totalCount():number
        {
            return this._totalCount;
        }

        public recycle()
        {
            super.recycle();
            this._totalCount = 0;
        }

        /**
         * 是否完成
         * 只要失败和成功总次数不等于totalCount时,返回false
         * 否则返回true
         */
        public get completed():boolean
        {
           if (this._totalCount != this._lockedCount)return false;
            else
            {
                if (this._totalCount == this._succeedCount) return true;
                else
                {
                    info("the task execute fail count:  " +  this._failCount);
                }
            }
            return true;
        }

        /**
         * 执行进度的百分比,返回小数点后四位
         */
        public get progress():number
        {
            return parseFloat((this._lockedCount / this._totalCount).toFixed(4));
        }
    }
 }

JTITaskCounter 接口

概要
  • 此本文本将被隐藏
 module com
  {
    export interface JTITaskCounter extends JTICounter
    {
        setTotalCount(totalCount:number):void
        totalCount:number     
        completed:boolean
       reset():void;
    }


}

扩展实现异步对列,不局限于引擎