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
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.
12 See the Apache Version 2.0 License for specific language governing permissions
13 and limitations under the License.
14 ***************************************************************************** */
18 var fs = require("fs");
19 function pipeExists(name) {
20 return fs.existsSync(name);
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];
30 if (!cancellationPipeName) {
32 isCancellationRequested: function () { return false; },
33 setRequest: function (_requestId) { return void 0; },
34 resetRequest: function (_requestId) { return void 0; }
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 '*'.");
42 var perRequestPipeName_1;
43 var currentRequestId_1;
45 isCancellationRequested: function () { return perRequestPipeName_1 !== undefined && pipeExists(perRequestPipeName_1); },
46 setRequest: function (requestId) {
47 currentRequestId_1 = requestId;
48 perRequestPipeName_1 = namePrefix_1 + requestId;
50 resetRequest: function (requestId) {
51 if (currentRequestId_1 !== requestId) {
52 throw new Error("Mismatched request id, expected " + currentRequestId_1 + ", actual " + requestId);
54 perRequestPipeName_1 = undefined;
60 isCancellationRequested: function () { return pipeExists(cancellationPipeName); },
61 setRequest: function (_requestId) { return void 0; },
62 resetRequest: function (_requestId) { return void 0; }
66 module.exports = createCancellationToken;
67 //# sourceMappingURL=cancellationToken.js.map