progress.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var progress_exports = {};
  20. __export(progress_exports, {
  21. ProgressController: () => ProgressController,
  22. isAbortError: () => isAbortError,
  23. raceUncancellableOperationWithCleanup: () => raceUncancellableOperationWithCleanup
  24. });
  25. module.exports = __toCommonJS(progress_exports);
  26. var import_errors = require("./errors");
  27. var import_utils = require("../utils");
  28. var import_manualPromise = require("../utils/isomorphic/manualPromise");
  29. class ProgressController {
  30. constructor(metadata, onCallLog) {
  31. this._forceAbortPromise = new import_manualPromise.ManualPromise();
  32. this._donePromise = new import_manualPromise.ManualPromise();
  33. this._state = "before";
  34. this.metadata = metadata || { id: "", startTime: 0, endTime: 0, type: "Internal", method: "", params: {}, log: [], internal: true };
  35. this._onCallLog = onCallLog;
  36. this._forceAbortPromise.catch((e) => null);
  37. }
  38. async abort(error) {
  39. if (this._state === "running") {
  40. error[kAbortErrorSymbol] = true;
  41. this._state = { error };
  42. this._forceAbortPromise.reject(error);
  43. }
  44. await this._donePromise;
  45. }
  46. async run(task, timeout) {
  47. (0, import_utils.assert)(this._state === "before");
  48. this._state = "running";
  49. const progress = {
  50. log: (message) => {
  51. if (this._state === "running")
  52. this.metadata.log.push(message);
  53. this._onCallLog?.(message);
  54. },
  55. metadata: this.metadata,
  56. race: (promise) => {
  57. const promises = Array.isArray(promise) ? promise : [promise];
  58. return Promise.race([...promises, this._forceAbortPromise]);
  59. },
  60. wait: async (timeout2) => {
  61. let timer2;
  62. const promise = new Promise((f) => timer2 = setTimeout(f, timeout2));
  63. return progress.race(promise).finally(() => clearTimeout(timer2));
  64. }
  65. };
  66. let timer;
  67. if (timeout) {
  68. const timeoutError = new import_errors.TimeoutError(`Timeout ${timeout}ms exceeded.`);
  69. timer = setTimeout(() => {
  70. if (this._state === "running") {
  71. timeoutError[kAbortErrorSymbol] = true;
  72. this._state = { error: timeoutError };
  73. this._forceAbortPromise.reject(timeoutError);
  74. }
  75. }, timeout);
  76. }
  77. try {
  78. const result = await task(progress);
  79. this._state = "finished";
  80. return result;
  81. } catch (error) {
  82. this._state = { error };
  83. throw error;
  84. } finally {
  85. clearTimeout(timer);
  86. this._donePromise.resolve();
  87. }
  88. }
  89. }
  90. const kAbortErrorSymbol = Symbol("kAbortError");
  91. function isAbortError(error) {
  92. return !!error[kAbortErrorSymbol];
  93. }
  94. async function raceUncancellableOperationWithCleanup(progress, run, cleanup) {
  95. let aborted = false;
  96. try {
  97. return await progress.race(run().then(async (t) => {
  98. if (aborted)
  99. await cleanup(t);
  100. return t;
  101. }));
  102. } catch (error) {
  103. aborted = true;
  104. throw error;
  105. }
  106. }
  107. // Annotate the CommonJS export names for ESM import in node:
  108. 0 && (module.exports = {
  109. ProgressController,
  110. isAbortError,
  111. raceUncancellableOperationWithCleanup
  112. });