progress.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ProgressController = void 0;
  6. var _errors = require("./errors");
  7. var _utils = require("../utils");
  8. var _manualPromise = require("../utils/manualPromise");
  9. /**
  10. * Copyright (c) Microsoft Corporation.
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. class ProgressController {
  25. constructor(metadata, sdkObject) {
  26. this._forceAbortPromise = new _manualPromise.ManualPromise();
  27. // Cleanups to be run only in the case of abort.
  28. this._cleanups = [];
  29. this._logName = 'api';
  30. this._state = 'before';
  31. this._deadline = 0;
  32. this._timeout = 0;
  33. this.metadata = void 0;
  34. this.instrumentation = void 0;
  35. this.sdkObject = void 0;
  36. this.metadata = metadata;
  37. this.sdkObject = sdkObject;
  38. this.instrumentation = sdkObject.instrumentation;
  39. this._forceAbortPromise.catch(e => null); // Prevent unhandled promise rejection.
  40. }
  41. setLogName(logName) {
  42. this._logName = logName;
  43. }
  44. abort(error) {
  45. this._forceAbortPromise.reject(error);
  46. }
  47. async run(task, timeout) {
  48. var _this$sdkObject$attri;
  49. if (timeout) {
  50. this._timeout = timeout;
  51. this._deadline = timeout ? (0, _utils.monotonicTime)() + timeout : 0;
  52. }
  53. (0, _utils.assert)(this._state === 'before');
  54. this._state = 'running';
  55. (_this$sdkObject$attri = this.sdkObject.attribution.context) === null || _this$sdkObject$attri === void 0 ? void 0 : _this$sdkObject$attri._activeProgressControllers.add(this);
  56. const progress = {
  57. log: message => {
  58. progress.logEntry({
  59. message
  60. });
  61. },
  62. logEntry: entry => {
  63. if ('message' in entry) {
  64. const message = entry.message;
  65. if (this._state === 'running') this.metadata.log.push(message);
  66. // Note: we might be sending logs after progress has finished, for example browser logs.
  67. this.instrumentation.onCallLog(this.sdkObject, this.metadata, this._logName, message);
  68. }
  69. },
  70. timeUntilDeadline: () => this._deadline ? this._deadline - (0, _utils.monotonicTime)() : 2147483647,
  71. // 2^31-1 safe setTimeout in Node.
  72. isRunning: () => this._state === 'running',
  73. cleanupWhenAborted: cleanup => {
  74. if (this._state === 'running') this._cleanups.push(cleanup);else runCleanup(cleanup);
  75. },
  76. throwIfAborted: () => {
  77. if (this._state === 'aborted') throw new AbortedError();
  78. },
  79. beforeInputAction: async element => {
  80. await this.instrumentation.onBeforeInputAction(this.sdkObject, this.metadata, element);
  81. },
  82. metadata: this.metadata
  83. };
  84. const timeoutError = new _errors.TimeoutError(`Timeout ${this._timeout}ms exceeded.`);
  85. const timer = setTimeout(() => this._forceAbortPromise.reject(timeoutError), progress.timeUntilDeadline());
  86. try {
  87. const promise = task(progress);
  88. const result = await Promise.race([promise, this._forceAbortPromise]);
  89. this._state = 'finished';
  90. return result;
  91. } catch (e) {
  92. this._state = 'aborted';
  93. await Promise.all(this._cleanups.splice(0).map(runCleanup));
  94. throw e;
  95. } finally {
  96. var _this$sdkObject$attri2;
  97. (_this$sdkObject$attri2 = this.sdkObject.attribution.context) === null || _this$sdkObject$attri2 === void 0 ? void 0 : _this$sdkObject$attri2._activeProgressControllers.delete(this);
  98. clearTimeout(timer);
  99. }
  100. }
  101. }
  102. exports.ProgressController = ProgressController;
  103. async function runCleanup(cleanup) {
  104. try {
  105. await cleanup();
  106. } catch (e) {}
  107. }
  108. class AbortedError extends Error {}