Deferred.js 1021 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Deferred = void 0;
  4. const TimeoutError_1 = require("./TimeoutError");
  5. class Deferred {
  6. constructor() {
  7. this._promise = new Promise((resolve, reject) => {
  8. this._reject = reject;
  9. this._resolve = resolve;
  10. });
  11. }
  12. registerTimeout(timeoutInMillis, callback) {
  13. if (this._timeout)
  14. return;
  15. this._timeout = setTimeout(() => {
  16. callback();
  17. this.reject(new TimeoutError_1.TimeoutError('Operation timeout'));
  18. }, timeoutInMillis);
  19. }
  20. _clearTimeout() {
  21. if (!this._timeout)
  22. return;
  23. clearTimeout(this._timeout);
  24. }
  25. resolve(value) {
  26. this._clearTimeout();
  27. this._resolve(value);
  28. }
  29. reject(error) {
  30. this._clearTimeout();
  31. this._reject(error);
  32. }
  33. promise() {
  34. return this._promise;
  35. }
  36. }
  37. exports.Deferred = Deferred;
  38. //# sourceMappingURL=Deferred.js.map