task.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * @Description: 定期執行任務
  3. * @Autor: kindring
  4. * @Date: 2021-09-02 14:24:58
  5. * @LastEditors: kindring
  6. * @LastEditTime: 2021-10-26 19:44:52
  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. recover(preSign) {
  61. let preData = this.preSigns[preSign];
  62. if (preData) {
  63. preData.val = false
  64. }
  65. return this;
  66. }
  67. checkPre(preData) {
  68. preData.tasks.forEach(id => {
  69. if (this.taskList[id]) {
  70. this.runTask(id)
  71. }
  72. })
  73. return this;
  74. }
  75. // 直接运行,嘗試直接執行任務
  76. run() {
  77. return this;
  78. }
  79. /**
  80. * 执行指定id的任务
  81. * @param {*} id 任务id
  82. * @returns
  83. */
  84. runTask(id) {
  85. let task = this.taskList[id]
  86. // console.log(task);
  87. if (task) {
  88. try {
  89. if (task.option) {
  90. let isPass = task.option.pre.every(pre => this.preSigns[pre].val)
  91. if (isPass) {
  92. task.fn()
  93. this.removePre(task, id)
  94. .removeTask(id)
  95. }
  96. } else {
  97. //执行任务
  98. task.fn()
  99. this.removePre(task, id)
  100. .removeTask(id)
  101. }
  102. } catch (error) {
  103. this.errorHandel(error)
  104. }
  105. }
  106. return this;
  107. }
  108. removePre(task, id) {
  109. if (task.option && task.option.pre) {
  110. task.option.pre.forEach(pre => {
  111. this.preSigns[pre].tasks = this.preSigns[pre].tasks.filter(taskId => id != taskId)
  112. })
  113. }
  114. //
  115. return this
  116. }
  117. removeTask(id) {
  118. delete this.taskList[id]
  119. return this
  120. }
  121. errorHandel(err) {
  122. console.error('出現錯誤')
  123. console.error(err)
  124. return this
  125. }
  126. }
  127. // 循环
  128. function forEach(arr, n = 0, fn) {
  129. if (arr[n] === undefined) {
  130. return;
  131. }
  132. fn(arr[n], n);
  133. forEach(arr, n + 1, fn);
  134. setTimeout(() => {
  135. }, 0)
  136. }
  137. let arr = [1, 2, 3, 4, 5, 6, 1]
  138. forEach(arr, 0, function(val1, index1) {
  139. forEach(arr, 0, function(val2, index2) {
  140. if (val2 == val1 && index1 != index2) {
  141. console.log('有一样的值')
  142. }
  143. })
  144. })
  145. // export default Task;