UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

63 lines (62 loc) 2.81 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.pollContinuously = void 0; const Readline = require("readline"); const interaction_1 = require("../interaction"); const ctrlCPressed = Symbol("Ctrl+C was pressed by user"); function pollContinuously(executeRequest, processResponse, pollContinuously, delayBetweenRequests, progressMessage) { return __awaiter(this, void 0, void 0, function* () { let requestsDone = 0; const readline = Readline.createInterface(process.stdin, process.stdout); const waitingForCtrlC = waitForCtrlC(readline); let timeoutTimer; try { while (true) { // executing request const executionResult = yield interaction_1.out.progress(progressMessage, Promise.race([executeRequest(), waitingForCtrlC])); if (executionResult === ctrlCPressed) { // user pressed ctrl+c, exiting break; } // processing result processResponse(executionResult, requestsDone); if (!pollContinuously) { break; } requestsDone++; // waiting before next request const delayBeforeNextRequest = new Promise((resolve) => { timeoutTimer = setTimeout(() => resolve(), delayBetweenRequests); }); const waitingResult = yield Promise.race([delayBeforeNextRequest, waitingForCtrlC]); if (waitingResult === ctrlCPressed) { // user pressed ctrl+c, exiting break; } } } finally { // cancelling timeout timer clearTimeout(timeoutTimer); // closing readline interface readline.close(); } }); } exports.pollContinuously = pollContinuously; function waitForCtrlC(readline) { return new Promise((resolve) => { readline.once("SIGINT", () => { resolve(ctrlCPressed); }); }); }