UNPKG

2.89 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5var __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};
12Object.defineProperty(exports, "__esModule", { value: true });
13var crypto_1 = __importDefault(require("crypto"));
14var fs = __importStar(require("fs"));
15var os = __importStar(require("os"));
16var path = __importStar(require("path"));
17var FsHelper_1 = require("./FsHelper");
18var 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 10ms
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}());
68exports.CancellationToken = CancellationToken;
69//# sourceMappingURL=CancellationToken.js.map
\No newline at end of file