TimeJobCenter.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**任务*/
  2. class TimeTask
  3. {
  4. public tid:number;
  5. public destTime:number;//延时执行时间
  6. public callback:Function;//执行方法
  7. public count:number;//执行次数
  8. public delay:number;//间隔
  9. public doDelete:boolean=false;
  10. constructor( tid:number,callback:Function, destTime:number,delay:number,count:number)
  11. {
  12. this.tid = tid;
  13. this.callback = callback;
  14. this.destTime = destTime;
  15. this.delay = delay;
  16. this.count = count;
  17. }
  18. }
  19. /**时间单位*/
  20. export enum TimeUnit
  21. {
  22. Millisecond,
  23. Second,
  24. Hour,
  25. Day
  26. }
  27. /**
  28. * 时间定时任务
  29. * 更好管理某一模块层级的时间任务,暂停,取消等
  30. */
  31. class TimeJobCenter {
  32. private tid:number = 0;
  33. private tempList:Array<TimeTask> = new Array<TimeTask>();
  34. private taskList:Array<TimeTask> = new Array<TimeTask>();
  35. private nowTime:number=0;
  36. public Dispose():void
  37. {
  38. this.taskList.length=0;
  39. this.tempList.length=0;
  40. this.nowTime=0;
  41. }
  42. private getTid():number
  43. {
  44. //安全代码以防过了
  45. if (this.tid==Number.MAX_VALUE)
  46. {
  47. let id = 0;
  48. while (true)
  49. {
  50. let used = false;
  51. for (let index = 0; index < this.taskList.length; index++)
  52. {
  53. if (this.taskList[index].tid == id)
  54. {
  55. used = true;
  56. break;
  57. }
  58. }
  59. if (!used)
  60. {
  61. break;
  62. }
  63. else
  64. {
  65. id++;
  66. }
  67. }
  68. return id;
  69. }else{
  70. this.tid += 1;
  71. return this.tid;
  72. }
  73. }
  74. /**延迟(ms)*/
  75. public delay(ms: number): Promise<void> {
  76. return new Promise(resolve => this.AddTimeTask(resolve, ms,1,TimeUnit.Millisecond));
  77. }
  78. /**延迟(s)*/
  79. public delay_second(ms: number): Promise<void> {
  80. return new Promise(resolve => this.AddTimeTask(resolve, ms,1,TimeUnit.Second));
  81. }
  82. /**
  83. * 增加时间任务
  84. * @param callback 执行方法
  85. * @param delay 延迟时间
  86. * @param count 次数
  87. * @param timeUnit 时间单位 默认为 秒 TimeUnit.Second
  88. * @returns 时间任务id
  89. */
  90. public AddTimeTask(callback:Function, delay:number,count:number = 1, timeUnit:TimeUnit = TimeUnit.Second):number
  91. {
  92. let tid = this.getTid();
  93. this.tempList.push(this.CreatNewTimeTask(tid, callback, delay, count, timeUnit));//增加一个任务到缓存
  94. return tid;
  95. }
  96. //
  97. private CreatNewTimeTask(tid:number, callback:Function, delay:number,count:number = 1, timeUnit:TimeUnit = TimeUnit.Second):TimeTask
  98. {
  99. if (timeUnit != TimeUnit.Second)
  100. {
  101. //如果单位不是秒,就全换算成秒做为单位
  102. switch (timeUnit)
  103. {
  104. case TimeUnit.Millisecond:
  105. delay *=0.001;
  106. break;
  107. case TimeUnit.Hour:
  108. delay *= 360;
  109. break;
  110. case TimeUnit.Day:
  111. delay *= 360 * 24;
  112. break;
  113. default:
  114. console.error("Add Task TimeUnit Type Error...");
  115. break;
  116. }
  117. }
  118. return new TimeTask(tid, callback,this.nowTime + delay, delay, count);
  119. }
  120. /**
  121. * 删除一个时间任务
  122. * @param tid 时间任务id
  123. * @returns 是否删除成功
  124. */
  125. public DeleteTimeTask(tid:number):boolean
  126. {
  127. let exist = false;
  128. let tt: TimeTask;
  129. for (let i = 0; i < this.tempList.length; i++)
  130. {
  131. tt = this.tempList[i];
  132. if (tt.tid == tid)
  133. {
  134. this.tempList[i].doDelete=true;
  135. exist = true;
  136. break;
  137. }
  138. }
  139. if (!exist)
  140. {
  141. for (let i = 0; i < this.taskList.length; i++)
  142. {
  143. tt = this.taskList[i];
  144. if (tt.tid == tid)
  145. {
  146. this.taskList[i].doDelete=true;
  147. exist = true;
  148. break;
  149. }
  150. }
  151. }
  152. return exist;
  153. }
  154. //
  155. private _index:number;
  156. private _count:number;
  157. private _tt:TimeTask;
  158. //运行调用
  159. public Run(dt:number):void
  160. {
  161. this.nowTime +=dt;
  162. this._count=this.tempList.length;
  163. if (this._count> 0)
  164. {
  165. for (this._index = 0;this._index < this._count; this._index++)
  166. {
  167. if(this.tempList[this._index].doDelete)continue;
  168. this.taskList.push(this.tempList[this._index]);
  169. }
  170. this.tempList.length=0;
  171. }
  172. this._count=this.taskList.length;
  173. if(this._count>0){
  174. for (this._index = 0; this._index <this._count; this._index++)
  175. {
  176. this._tt = this.taskList[this._index];
  177. if (this.nowTime < this._tt.destTime) continue;
  178. if (this._tt.doDelete) continue;
  179. this._tt.callback();
  180. if (this._tt.count == 1)
  181. {
  182. this._tt.doDelete=true;
  183. }
  184. else if (this._tt.count > 0)
  185. {
  186. this._tt.count--;
  187. this._tt.destTime += this._tt.delay;//下次执行时间
  188. }else{
  189. this._tt.destTime += this._tt.delay;
  190. }
  191. }
  192. }
  193. this.taskList = this.taskList.filter(task => !task.doDelete);//把标记为删除的真正删除
  194. }
  195. //
  196. }
  197. export default function get_new_job():TimeJobCenter { return new TimeJobCenter();}