1 | /*! *****************************************************************************
|
---|
2 | Copyright (c) Microsoft Corporation. All rights reserved.
|
---|
3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
---|
4 | this file except in compliance with the License. You may obtain a copy of the
|
---|
5 | License at http://www.apache.org/licenses/LICENSE-2.0
|
---|
6 |
|
---|
7 | THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
---|
8 | KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
---|
9 | WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
---|
10 | MERCHANTABLITY OR NON-INFRINGEMENT.
|
---|
11 |
|
---|
12 | See the Apache Version 2.0 License for specific language governing permissions
|
---|
13 | and limitations under the License.
|
---|
14 | ***************************************************************************** */
|
---|
15 |
|
---|
16 |
|
---|
17 | "use strict";
|
---|
18 | var fs = require("fs");
|
---|
19 | function pipeExists(name) {
|
---|
20 | return fs.existsSync(name);
|
---|
21 | }
|
---|
22 | function createCancellationToken(args) {
|
---|
23 | var cancellationPipeName;
|
---|
24 | for (var i = 0; i < args.length - 1; i++) {
|
---|
25 | if (args[i] === "--cancellationPipeName") {
|
---|
26 | cancellationPipeName = args[i + 1];
|
---|
27 | break;
|
---|
28 | }
|
---|
29 | }
|
---|
30 | if (!cancellationPipeName) {
|
---|
31 | return {
|
---|
32 | isCancellationRequested: function () { return false; },
|
---|
33 | setRequest: function (_requestId) { return void 0; },
|
---|
34 | resetRequest: function (_requestId) { return void 0; }
|
---|
35 | };
|
---|
36 | }
|
---|
37 | if (cancellationPipeName.charAt(cancellationPipeName.length - 1) === "*") {
|
---|
38 | var namePrefix_1 = cancellationPipeName.slice(0, -1);
|
---|
39 | if (namePrefix_1.length === 0 || namePrefix_1.indexOf("*") >= 0) {
|
---|
40 | throw new Error("Invalid name for template cancellation pipe: it should have length greater than 2 characters and contain only one '*'.");
|
---|
41 | }
|
---|
42 | var perRequestPipeName_1;
|
---|
43 | var currentRequestId_1;
|
---|
44 | return {
|
---|
45 | isCancellationRequested: function () { return perRequestPipeName_1 !== undefined && pipeExists(perRequestPipeName_1); },
|
---|
46 | setRequest: function (requestId) {
|
---|
47 | currentRequestId_1 = requestId;
|
---|
48 | perRequestPipeName_1 = namePrefix_1 + requestId;
|
---|
49 | },
|
---|
50 | resetRequest: function (requestId) {
|
---|
51 | if (currentRequestId_1 !== requestId) {
|
---|
52 | throw new Error("Mismatched request id, expected " + currentRequestId_1 + ", actual " + requestId);
|
---|
53 | }
|
---|
54 | perRequestPipeName_1 = undefined;
|
---|
55 | }
|
---|
56 | };
|
---|
57 | }
|
---|
58 | else {
|
---|
59 | return {
|
---|
60 | isCancellationRequested: function () { return pipeExists(cancellationPipeName); },
|
---|
61 | setRequest: function (_requestId) { return void 0; },
|
---|
62 | resetRequest: function (_requestId) { return void 0; }
|
---|
63 | };
|
---|
64 | }
|
---|
65 | }
|
---|
66 | module.exports = createCancellationToken;
|
---|
67 | //# sourceMappingURL=cancellationToken.js.map |
---|