版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

120 строки
3.0 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * Handles async tasks.
  5. */
  6. class AsyncTaskManager {
  7. constructor() {
  8. this.runningTasks = {};
  9. this.runningTimers = [];
  10. this.queue = [];
  11. }
  12. /**
  13. * Returns a promise that is fulfilled when async tasks are complete.
  14. * This method is not part of the HTML standard.
  15. *
  16. * @returns Promise.
  17. */
  18. async whenComplete() {
  19. return new Promise((resolve, reject) => {
  20. const timerID = global.setTimeout(() => {
  21. this.endTimer(timerID);
  22. }, 0);
  23. this.startTimer(timerID);
  24. this.queue.push({ resolve, reject });
  25. });
  26. }
  27. /**
  28. * Ends all tasks.
  29. *
  30. * @param [error] Error.
  31. */
  32. cancelAll(error) {
  33. const runningTimers = this.runningTimers;
  34. const runningTasks = this.runningTasks;
  35. const promises = this.queue;
  36. this.runningTasks = {};
  37. this.runningTimers = [];
  38. this.queue = [];
  39. for (const timer of runningTimers) {
  40. global.clearTimeout(timer);
  41. }
  42. for (const key of Object.keys(runningTasks)) {
  43. runningTasks[key]();
  44. }
  45. for (const promise of promises) {
  46. if (error) {
  47. promise.reject(error);
  48. }
  49. else {
  50. promise.resolve();
  51. }
  52. }
  53. }
  54. /**
  55. * Starts a timer.
  56. *
  57. * @param timerID Timer ID.
  58. */
  59. startTimer(timerID) {
  60. this.runningTimers.push(timerID);
  61. }
  62. /**
  63. * Ends a timer.
  64. *
  65. * @param timerID Timer ID.
  66. */
  67. endTimer(timerID) {
  68. const index = this.runningTimers.indexOf(timerID);
  69. if (index !== -1) {
  70. this.runningTimers.splice(index, 1);
  71. }
  72. if (!Object.keys(this.runningTasks).length && !this.runningTimers.length) {
  73. this.cancelAll();
  74. }
  75. }
  76. /**
  77. * Starts an async task.
  78. *
  79. * @param abortHandler Abort handler.
  80. * @returns Task ID.
  81. */
  82. startTask(abortHandler) {
  83. const taskID = this.newTaskID();
  84. this.runningTasks[taskID] = abortHandler ? abortHandler : () => { };
  85. return taskID;
  86. }
  87. /**
  88. * Ends an async task.
  89. *
  90. * @param taskID Task ID.
  91. */
  92. endTask(taskID) {
  93. if (this.runningTasks[taskID]) {
  94. delete this.runningTasks[taskID];
  95. if (!Object.keys(this.runningTasks).length && !this.runningTimers.length) {
  96. this.cancelAll();
  97. }
  98. }
  99. }
  100. /**
  101. * Returns the amount of running tasks.
  102. *
  103. * @returns Count.
  104. */
  105. getTaskCount() {
  106. return Object.keys(this.runningTasks).length;
  107. }
  108. /**
  109. * Returns a new task ID.
  110. *
  111. * @returns Task ID.
  112. */
  113. newTaskID() {
  114. this.constructor.taskID++;
  115. return this.constructor.taskID;
  116. }
  117. }
  118. exports.default = AsyncTaskManager;
  119. AsyncTaskManager.taskID = 0;
  120. //# sourceMappingURL=AsyncTaskManager.js.map