Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var crypto = require("crypto");
  4. var fs = require("fs");
  5. var os = require("os");
  6. var path = require("path");
  7. var OperationCanceledException_1 = require("./OperationCanceledException");
  8. var CancellationToken = /** @class */ (function () {
  9. function CancellationToken(cancellationFileName, isCancelled) {
  10. this.isCancelled = !!isCancelled;
  11. this.cancellationFileName =
  12. cancellationFileName || crypto.randomBytes(64).toString('hex');
  13. this.lastCancellationCheckTime = 0;
  14. }
  15. CancellationToken.createFromJSON = function (json) {
  16. return new CancellationToken(json.cancellationFileName, json.isCancelled);
  17. };
  18. CancellationToken.prototype.toJSON = function () {
  19. return {
  20. cancellationFileName: this.cancellationFileName,
  21. isCancelled: this.isCancelled
  22. };
  23. };
  24. CancellationToken.prototype.getCancellationFilePath = function () {
  25. return path.join(os.tmpdir(), this.cancellationFileName);
  26. };
  27. CancellationToken.prototype.isCancellationRequested = function () {
  28. if (this.isCancelled) {
  29. return true;
  30. }
  31. var time = Date.now();
  32. var duration = Math.abs(time - this.lastCancellationCheckTime);
  33. if (duration > 10) {
  34. // check no more than once every 10ms
  35. this.lastCancellationCheckTime = time;
  36. this.isCancelled = fs.existsSync(this.getCancellationFilePath());
  37. }
  38. return this.isCancelled;
  39. };
  40. CancellationToken.prototype.throwIfCancellationRequested = function () {
  41. if (this.isCancellationRequested()) {
  42. throw new OperationCanceledException_1.OperationCanceledException();
  43. }
  44. };
  45. CancellationToken.prototype.requestCancellation = function () {
  46. fs.writeFileSync(this.getCancellationFilePath(), '');
  47. this.isCancelled = true;
  48. };
  49. CancellationToken.prototype.cleanupCancellation = function () {
  50. if (this.isCancelled && fs.existsSync(this.getCancellationFilePath())) {
  51. fs.unlinkSync(this.getCancellationFilePath());
  52. this.isCancelled = false;
  53. }
  54. };
  55. return CancellationToken;
  56. }());
  57. exports.CancellationToken = CancellationToken;