1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const crypto = require("crypto");
|
4 | const fs = require("fs");
|
5 | const os = require("os");
|
6 | const path = require("path");
|
7 | const FsHelper_1 = require("./FsHelper");
|
8 | class CancellationToken {
|
9 | constructor(typescript, cancellationFileName, isCancelled) {
|
10 | this.typescript = typescript;
|
11 | this.isCancelled = !!isCancelled;
|
12 | this.cancellationFileName =
|
13 | cancellationFileName || crypto.randomBytes(64).toString('hex');
|
14 | this.lastCancellationCheckTime = 0;
|
15 | }
|
16 | static createFromJSON(typescript, json) {
|
17 | return new CancellationToken(typescript, json.cancellationFileName, json.isCancelled);
|
18 | }
|
19 | toJSON() {
|
20 | return {
|
21 | cancellationFileName: this.cancellationFileName,
|
22 | isCancelled: this.isCancelled
|
23 | };
|
24 | }
|
25 | getCancellationFilePath() {
|
26 | return path.join(os.tmpdir(), this.cancellationFileName);
|
27 | }
|
28 | isCancellationRequested() {
|
29 | if (this.isCancelled) {
|
30 | return true;
|
31 | }
|
32 | const time = Date.now();
|
33 | const duration = Math.abs(time - this.lastCancellationCheckTime);
|
34 | if (duration > 10) {
|
35 |
|
36 | this.lastCancellationCheckTime = time;
|
37 | this.isCancelled = FsHelper_1.fileExistsSync(this.getCancellationFilePath());
|
38 | }
|
39 | return this.isCancelled;
|
40 | }
|
41 | throwIfCancellationRequested() {
|
42 | if (this.isCancellationRequested()) {
|
43 | throw new this.typescript.OperationCanceledException();
|
44 | }
|
45 | }
|
46 | requestCancellation() {
|
47 | fs.writeFileSync(this.getCancellationFilePath(), '');
|
48 | this.isCancelled = true;
|
49 | }
|
50 | cleanupCancellation() {
|
51 | if (this.isCancelled && FsHelper_1.fileExistsSync(this.getCancellationFilePath())) {
|
52 | fs.unlinkSync(this.getCancellationFilePath());
|
53 | this.isCancelled = false;
|
54 | }
|
55 | }
|
56 | }
|
57 | exports.CancellationToken = CancellationToken;
|
58 |
|
\ | No newline at end of file |