UNPKG

asposewordscloud

Version:
106 lines (105 loc) 4.76 kB
"use strict"; /* * -------------------------------------------------------------------------------- * <copyright company="Aspose" file="jobHandler.ts"> * Copyright (c) 2026 Aspose.Words for Cloud * </copyright> * <summary> * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * </summary> * -------------------------------------------------------------------------------- */ 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.JobHandler = void 0; const model = require("./model/model"); const requestHelper_1 = require("./internal/requestHelper"); class JobHandler { constructor(api, request, info) { this.api = api; this.request = request; this.info = info; } get status() { var _a, _b; return ((_a = this.info) === null || _a === void 0 ? void 0 : _a.status) !== undefined && ((_b = this.info) === null || _b === void 0 ? void 0 : _b.status) !== null ? String(this.info.status) : ""; } get message() { var _a; return ((_a = this.info) === null || _a === void 0 ? void 0 : _a.message) || ""; } get result() { return this.resolvedResult; } update() { var _a; return __awaiter(this, void 0, void 0, function* () { if (!((_a = this.info) === null || _a === void 0 ? void 0 : _a.jobId)) { throw new Error("Invalid job id."); } const parts = yield (0, requestHelper_1.callJobResult)(this.api.configuration, this.info.jobId); if (parts.length >= 1) { this.info = model.deserializeObject(parts[0].body.toString("utf8"), "JobInfo"); if (parts.length >= 2 && this.isSucceeded()) { this.resolvedResult = (0, requestHelper_1.deserializeHttpResponsePart)(this.request, parts[1]); } } return this.resolvedResult; }); } waitResult(updateIntervalMs = 3000) { return __awaiter(this, void 0, void 0, function* () { while (this.isQueued() || this.isProcessing()) { yield this.delay(updateIntervalMs); yield this.update(); } if (this.isSucceeded() && this.resolvedResult === undefined) { yield this.update(); } if (!this.isSucceeded()) { throw new Error(`Job failed with status "${this.status}" - "${this.message}".`); } return this.resolvedResult; }); } isQueued() { return this.status.toLowerCase() === "queued"; } isProcessing() { return this.status.toLowerCase() === "processing"; } isSucceeded() { const status = this.status.toLowerCase(); return status === "succeded" || status === "succeeded"; } delay(timeoutMs) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => setTimeout(resolve, timeoutMs)); }); } } exports.JobHandler = JobHandler;