UNPKG

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