task.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * @Description: 定時執行任務
  3. * @Autor: kindring
  4. * @Date: 2021-09-02 14:51:09
  5. * @LastEditors: kindring
  6. * @LastEditTime: 2021-09-02 14:51:12
  7. * @LastDescript:
  8. */
  9. // 定义任务队列
  10. class Task {
  11. constructor() {
  12. this.taskList = new Object(null)
  13. this.taskId = 1
  14. this.preSigns = new Object(null)
  15. }
  16. // 添加任务
  17. add(fn, option) {
  18. // let baseTaskData = {
  19. // // 需要完成的前置变量条件
  20. // pres: [],
  21. // // 等待时间
  22. // waitTime: 0,
  23. // handel: ()=>{}
  24. // }
  25. // 先将任务添加到任务队列中去
  26. let id = this.taskId
  27. if (option) {
  28. if (option.pre) {
  29. if (typeof option.pre == 'string') { option.pre = [option.pre] }
  30. option.pre.forEach(val => {
  31. if (this.preSigns[val]) {
  32. this.preSigns[val].tasks.push(id)
  33. } else {
  34. this.preSigns[val] = {
  35. val: false,
  36. tasks: [id]
  37. }
  38. }
  39. })
  40. }
  41. }
  42. this.taskList[id] = {
  43. fn: fn,
  44. option: option
  45. }
  46. this.taskId++;
  47. // 尝试运行任务,
  48. this.runTask(id)
  49. return this;
  50. }
  51. change(preSign) {
  52. let preData = this.preSigns[preSign];
  53. if (preData) {
  54. preData.val = true
  55. // 尝试找出任务有问题的
  56. this.checkPre(preData);
  57. }
  58. return this;
  59. }
  60. // 运行满足条件的函数
  61. checkPre(preData) {
  62. preData.tasks.forEach(id => {
  63. if (this.taskList[id]) {
  64. this.runTask(id)
  65. }
  66. })
  67. return this;
  68. }
  69. // 直接运行,嘗試直接執行任務
  70. run() {
  71. return this;
  72. }
  73. /**
  74. * 执行指定id的任务
  75. * @param {*} id 任务id
  76. * @returns
  77. */
  78. runTask(id) {
  79. let task = this.taskList[id]
  80. // console.log(task);
  81. if (task) {
  82. try {
  83. if (task.option) {
  84. let isPass = task.option.pre.every(pre => this.preSigns[pre].val)
  85. if (isPass) {
  86. task.fn()
  87. this.removePre(task, id)
  88. .removeTask(id)
  89. }
  90. } else {
  91. //执行任务
  92. task.fn()
  93. this.removePre(task, id)
  94. .removeTask(id)
  95. }
  96. } catch (error) {
  97. this.errorHandel(error)
  98. }
  99. }
  100. return this;
  101. }
  102. removePre(task, id) {
  103. if (task.option && task.option.pre) {
  104. task.option.pre.forEach(pre => {
  105. this.preSigns[pre].tasks = this.preSigns[pre].tasks.filter(taskId => id != taskId)
  106. })
  107. }
  108. //
  109. return this
  110. }
  111. removeTask(id) {
  112. delete this.taskList[id]
  113. return this
  114. }
  115. errorHandel(err) {
  116. console.error('出現錯誤')
  117. console.error(err)
  118. return this
  119. }
  120. }
  121. let t = new Task();
  122. t.add(() => {
  123. console.log('發送消息 6')
  124. t.change('end')
  125. }, { pre: 'ok1' })
  126. t.add(() => {
  127. console.log('開始發送消息 5');
  128. t.change('ok1')
  129. }, { pre: ['pre', 'ok2'] })
  130. t.add(() => {
  131. console.log('開始 1')
  132. })
  133. t.add(() => {
  134. console.log('結束 7');
  135. }, { pre: 'end' })
  136. setTimeout(() => {
  137. console.log(`change pre wait 3`);
  138. t.change('pre')
  139. }, 1000)
  140. setTimeout(() => {
  141. console.log(`change ok wait 4`);
  142. t.change('ok2')
  143. }, 2000)
  144. console.log('執行任務 2')
  145. module.exports = Task;