版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

74 líneas
1.8 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * Document ready state manager.
  5. */
  6. class DocumentReadyStateManager {
  7. /**
  8. * Constructor.
  9. *
  10. * @param window
  11. */
  12. constructor(window) {
  13. this.totalTasks = 0;
  14. this.readyStateCallbacks = [];
  15. this.window = null;
  16. this.timer = null;
  17. this.isComplete = false;
  18. this.window = window;
  19. }
  20. /**
  21. * Returns a promise that is fulfilled when ready state is complete.
  22. *
  23. * @returns Promise.
  24. */
  25. whenComplete() {
  26. return new Promise((resolve) => {
  27. if (this.isComplete) {
  28. resolve();
  29. }
  30. else {
  31. this.readyStateCallbacks.push(resolve);
  32. if (this.totalTasks === 0 && !this.timer) {
  33. this.timer = this.window.setTimeout(this.endTask.bind(this));
  34. }
  35. }
  36. });
  37. }
  38. /**
  39. * Starts a task.
  40. */
  41. startTask() {
  42. if (this.isComplete) {
  43. return;
  44. }
  45. if (this.timer) {
  46. this.window.clearTimeout(this.timer);
  47. this.timer = null;
  48. }
  49. this.totalTasks++;
  50. }
  51. /**
  52. * Ends a task.
  53. */
  54. endTask() {
  55. if (this.isComplete) {
  56. return;
  57. }
  58. if (this.timer) {
  59. this.window.clearTimeout(this.timer);
  60. this.timer = null;
  61. }
  62. this.totalTasks--;
  63. if (this.totalTasks <= 0) {
  64. const callbacks = this.readyStateCallbacks;
  65. this.readyStateCallbacks = [];
  66. this.isComplete = true;
  67. for (const callback of callbacks) {
  68. callback();
  69. }
  70. }
  71. }
  72. }
  73. exports.default = DocumentReadyStateManager;
  74. //# sourceMappingURL=DocumentReadyStateManager.js.map