manualPromise.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ManualPromise = exports.LongStandingScope = void 0;
  6. var _stackTrace = require("./stackTrace");
  7. let _Symbol$species, _Symbol$toStringTag;
  8. /**
  9. * Copyright (c) Microsoft Corporation.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. _Symbol$species = Symbol.species;
  24. _Symbol$toStringTag = Symbol.toStringTag;
  25. class ManualPromise extends Promise {
  26. constructor() {
  27. let resolve;
  28. let reject;
  29. super((f, r) => {
  30. resolve = f;
  31. reject = r;
  32. });
  33. this._resolve = void 0;
  34. this._reject = void 0;
  35. this._isDone = void 0;
  36. this._isDone = false;
  37. this._resolve = resolve;
  38. this._reject = reject;
  39. }
  40. isDone() {
  41. return this._isDone;
  42. }
  43. resolve(t) {
  44. this._isDone = true;
  45. this._resolve(t);
  46. }
  47. reject(e) {
  48. this._isDone = true;
  49. this._reject(e);
  50. }
  51. static get [_Symbol$species]() {
  52. return Promise;
  53. }
  54. get [_Symbol$toStringTag]() {
  55. return 'ManualPromise';
  56. }
  57. }
  58. exports.ManualPromise = ManualPromise;
  59. class LongStandingScope {
  60. constructor() {
  61. this._terminateError = void 0;
  62. this._closeError = void 0;
  63. this._terminatePromises = new Map();
  64. this._isClosed = false;
  65. }
  66. reject(error) {
  67. this._isClosed = true;
  68. this._terminateError = error;
  69. for (const p of this._terminatePromises.keys()) p.resolve(error);
  70. }
  71. close(error) {
  72. this._isClosed = true;
  73. this._closeError = error;
  74. for (const [p, frames] of this._terminatePromises) p.resolve(cloneError(error, frames));
  75. }
  76. isClosed() {
  77. return this._isClosed;
  78. }
  79. static async raceMultiple(scopes, promise) {
  80. return Promise.race(scopes.map(s => s.race(promise)));
  81. }
  82. async race(promise) {
  83. return this._race(Array.isArray(promise) ? promise : [promise], false);
  84. }
  85. async safeRace(promise, defaultValue) {
  86. return this._race([promise], true, defaultValue);
  87. }
  88. async _race(promises, safe, defaultValue) {
  89. const terminatePromise = new ManualPromise();
  90. const frames = (0, _stackTrace.captureRawStack)();
  91. if (this._terminateError) terminatePromise.resolve(this._terminateError);
  92. if (this._closeError) terminatePromise.resolve(cloneError(this._closeError, frames));
  93. this._terminatePromises.set(terminatePromise, frames);
  94. try {
  95. return await Promise.race([terminatePromise.then(e => safe ? defaultValue : Promise.reject(e)), ...promises]);
  96. } finally {
  97. this._terminatePromises.delete(terminatePromise);
  98. }
  99. }
  100. }
  101. exports.LongStandingScope = LongStandingScope;
  102. function cloneError(error, frames) {
  103. const clone = new Error();
  104. clone.name = error.name;
  105. clone.message = error.message;
  106. clone.stack = [error.name + ':' + error.message, ...frames].join('\n');
  107. return clone;
  108. }