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