CancellationToken.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. var __importStar = (this && this.__importStar) || function (mod) {
  6. if (mod && mod.__esModule) return mod;
  7. var result = {};
  8. if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
  9. result["default"] = mod;
  10. return result;
  11. };
  12. Object.defineProperty(exports, "__esModule", { value: true });
  13. var crypto_1 = __importDefault(require("crypto"));
  14. var fs = __importStar(require("fs"));
  15. var os = __importStar(require("os"));
  16. var path = __importStar(require("path"));
  17. var FsHelper_1 = require("./FsHelper");
  18. var CancellationToken = /** @class */ (function () {
  19. function CancellationToken(typescript, cancellationFileName, isCancelled) {
  20. this.typescript = typescript;
  21. this.isCancelled = !!isCancelled;
  22. this.cancellationFileName =
  23. cancellationFileName || crypto_1.default.randomBytes(64).toString('hex');
  24. this.lastCancellationCheckTime = 0;
  25. }
  26. CancellationToken.createFromJSON = function (typescript, json) {
  27. return new CancellationToken(typescript, json.cancellationFileName, json.isCancelled);
  28. };
  29. CancellationToken.prototype.toJSON = function () {
  30. return {
  31. cancellationFileName: this.cancellationFileName,
  32. isCancelled: this.isCancelled
  33. };
  34. };
  35. CancellationToken.prototype.getCancellationFilePath = function () {
  36. return path.join(os.tmpdir(), this.cancellationFileName);
  37. };
  38. CancellationToken.prototype.isCancellationRequested = function () {
  39. if (this.isCancelled) {
  40. return true;
  41. }
  42. var time = Date.now();
  43. var duration = Math.abs(time - this.lastCancellationCheckTime);
  44. if (duration > 10) {
  45. // check no more than once every 10 ms
  46. this.lastCancellationCheckTime = time;
  47. this.isCancelled = FsHelper_1.fileExistsSync(this.getCancellationFilePath());
  48. }
  49. return this.isCancelled;
  50. };
  51. CancellationToken.prototype.throwIfCancellationRequested = function () {
  52. if (this.isCancellationRequested()) {
  53. throw new this.typescript.OperationCanceledException();
  54. }
  55. };
  56. CancellationToken.prototype.requestCancellation = function () {
  57. fs.writeFileSync(this.getCancellationFilePath(), '');
  58. this.isCancelled = true;
  59. };
  60. CancellationToken.prototype.cleanupCancellation = function () {
  61. if (this.isCancelled && FsHelper_1.fileExistsSync(this.getCancellationFilePath())) {
  62. fs.unlinkSync(this.getCancellationFilePath());
  63. this.isCancelled = false;
  64. }
  65. };
  66. return CancellationToken;
  67. }());
  68. exports.CancellationToken = CancellationToken;
  69. //# sourceMappingURL=CancellationToken.js.map