UNPKG

c8osdkjscore

Version:
292 lines 19.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [0, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var c8oCore_1 = require("./c8oCore"); var c8oFullSync_1 = require("./c8oFullSync"); var c8oLocalCache_1 = require("./c8oLocalCache"); var c8oLocalCacheResponse_1 = require("./c8oLocalCacheResponse"); var c8oResponse_1 = require("./c8oResponse"); var c8oTranslator_1 = require("./c8oTranslator"); var c8oUtilsCore_1 = require("./c8oUtilsCore"); var c8oException_1 = require("./Exception/c8oException"); var c8oExceptionMessage_1 = require("./Exception/c8oExceptionMessage"); var c8oHttpRequestException_1 = require("./Exception/c8oHttpRequestException"); var c8oUnavailableLocalCacheException_1 = require("./Exception/c8oUnavailableLocalCacheException"); var C8oCallTask = (function () { function C8oCallTask(c8o, parameters, c8oResponseListener, c8oExceptionListener) { this.c8o = c8o; this.parameters = parameters; this.c8oResponseListener = c8oResponseListener; this.c8oExceptionListener = c8oExceptionListener; c8o.log.logMethodCall("C8oCallTask", parameters, c8oResponseListener, c8oExceptionListener); } Object.defineProperty(C8oCallTask.prototype, "parameters", { get: function () { return this._parameters; }, set: function (value) { this._parameters = value; }, enumerable: true, configurable: true }); C8oCallTask.prototype.run = function () { var _this = this; try { this.handleRequest().then(function (response) { _this.handleResponse(response); }).catch(function (error) { _this.c8oExceptionListener.onException(error, _this.parameters); }); } catch (error) { this.c8oExceptionListener.onException(error, this.parameters); } }; C8oCallTask.prototype.executeFromLive = function () { delete this.parameters[c8oCore_1.C8oCore.FS_LIVE]; this.parameters[c8oCore_1.C8oCore.ENGINE_PARAMETER_FROM_LIVE] = true; this.run(); }; C8oCallTask.prototype.handleRequest = function () { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { return [2, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { var _this = this; var isFullSyncRequest, liveid, dbName, responseType_1, c8oCallRequestIdentifier_1, localCache_1, localCacheEnabled_1, result, localCacheResponse, error_1, params, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 9, , 10]); isFullSyncRequest = c8oFullSync_1.C8oFullSync.isFullSyncRequest(this.parameters); if (!isFullSyncRequest) return [3, 2]; this.c8o.log._debug("Is FullSync request"); liveid = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(this.parameters, c8oCore_1.C8oCore.FS_LIVE, false); if (liveid !== null) { dbName = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(this.parameters, c8oCore_1.C8oCore.ENGINE_PARAMETER_PROJECT, true).substring(c8oFullSync_1.C8oFullSync.FULL_SYNC_PROJECT.length); this.c8o.addLive(liveid, dbName, this); } return [4, this.c8o.c8oFullSync.handleFullSyncRequest(this.parameters, this.c8oResponseListener) .then(function (result) { resolve(result); }) .catch(function (error) { if (error instanceof c8oException_1.C8oException) { reject(error); } else { reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.handleFullSyncRequest(), error)); } })]; case 1: _a.sent(); return [3, 8]; case 2: responseType_1 = ""; if (this.c8oResponseListener instanceof c8oResponse_1.C8oResponseJsonListener) { responseType_1 = c8oCore_1.C8oCore.RESPONSE_TYPE_JSON; } else { reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.wrongListener(this.c8oResponseListener))); } c8oCallRequestIdentifier_1 = null; localCache_1 = c8oUtilsCore_1.C8oUtilsCore.getParameterObjectValue(this.parameters, c8oLocalCache_1.C8oLocalCache.PARAM, false); localCacheEnabled_1 = false; if (!(localCache_1 != null)) return [3, 6]; if (!(localCacheEnabled_1 !== undefined)) return [3, 6]; delete this.parameters[c8oLocalCache_1.C8oLocalCache.PARAM]; localCacheEnabled_1 = localCache_1.enabled; if (!localCacheEnabled_1) return [3, 6]; try { c8oCallRequestIdentifier_1 = c8oUtilsCore_1.C8oUtilsCore.identifyC8oCallRequest(this.parameters, responseType_1); } catch (error) { reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.serializeC8oCallRequest(), error)); } if (!localCache_1.priority.isAvailable) return [3, 6]; _a.label = 3; case 3: _a.trys.push([3, 5, , 6]); return [4, this.c8o.c8oFullSync.getResponseFromLocalCache(c8oCallRequestIdentifier_1)]; case 4: result = _a.sent(); if (result instanceof c8oUnavailableLocalCacheException_1.C8oUnavailableLocalCacheException) { } else { localCacheResponse = result; if (!localCacheResponse.isExpired()) { if (responseType_1 === c8oCore_1.C8oCore.RESPONSE_TYPE_JSON) { resolve(c8oTranslator_1.C8oTranslator.stringToJSON(localCacheResponse.getResponse())); return [2]; } } } return [3, 6]; case 5: error_1 = _a.sent(); if (error_1 instanceof c8oUnavailableLocalCacheException_1.C8oUnavailableLocalCacheException) { } else { reject(error_1); } return [3, 6]; case 6: this.parameters[c8oCore_1.C8oCore.ENGINE_PARAMETER_DEVICE_UUID] = this.c8o.deviceUUID; this.c8oCallUrl = this.c8o.endpoint + "/." + responseType_1; params = new Object(); params = Object.assign(params, this.parameters); return [4, this.c8o.httpInterface.handleRequest(this.c8oCallUrl, params, this.c8oResponseListener).catch(function (error) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: if (!localCacheEnabled_1) return [3, 2]; return [4, this.c8o.c8oFullSync.getResponseFromLocalCache(c8oCallRequestIdentifier_1).then(function (localCacheResponse) { try { if (!localCacheResponse.isExpired()) { if (responseType_1 === c8oCore_1.C8oCore.RESPONSE_TYPE_JSON) { resolve(c8oTranslator_1.C8oTranslator.stringToJSON(localCacheResponse.getResponse())); } } } catch (error) { } })]; case 1: _a.sent(); _a.label = 2; case 2: if (error.status === 0) { reject(new c8oHttpRequestException_1.C8oHttpRequestException("ERR_INTERNET_DISCONNECTED", error)); } else { reject(error); } return [2]; } }); }); }).then(function (result) { return __awaiter(_this, void 0, void 0, function () { var response_1, responseString, expirationDate, localCacheResponse, p1, error_3; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!(result !== undefined)) return [3, 5]; responseString = void 0; if (this.c8oResponseListener instanceof c8oResponse_1.C8oResponseJsonListener) { try { try { responseString = result; } catch (error) { reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.parseInputStreamToString(), error)); } response_1 = result; } catch (error) { reject(error); } } else { reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.wrongListener(this.c8oResponseListener))); } if (!localCacheEnabled_1) return [3, 4]; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); expirationDate = -1; if (localCache_1.ttl > 0) { expirationDate = localCache_1.ttl + (new Date).getTime(); } localCacheResponse = new c8oLocalCacheResponse_1.C8oLocalCacheResponse(responseString, responseType_1, expirationDate); return [4, this.c8o.c8oFullSync.saveResponseToLocalCache(c8oCallRequestIdentifier_1, localCacheResponse)]; case 2: p1 = _a.sent(); Promise.all([p1]) .then(function () { resolve(response_1); }); return [3, 4]; case 3: error_3 = _a.sent(); reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.saveResponseToLocalCache())); return [3, 4]; case 4: resolve(response_1); _a.label = 5; case 5: return [2]; } }); }); })]; case 7: _a.sent(); _a.label = 8; case 8: return [3, 10]; case 9: error_2 = _a.sent(); reject(error_2); return [3, 10]; case 10: return [2]; } }); }); })]; }); }); }; C8oCallTask.prototype.handleResponse = function (result) { try { if (this.c8oResponseListener === null) { return; } else if (result instanceof Error || result instanceof c8oException_1.C8oException) { this.c8o.handleCallException(this.c8oExceptionListener, this.parameters, result); } else if (result instanceof Object) { this.c8o.log.logC8oCallJSONResponse(result, this.c8oCallUrl, this.parameters); this.c8oResponseListener.onJsonResponse(result, this.parameters); } else { this.c8o.handleCallException(this.c8oExceptionListener, this.parameters, new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.wrongResult(result))); } } catch (error) { this.c8o.handleCallException(this.c8oExceptionListener, this.parameters, error); } }; return C8oCallTask; }()); exports.C8oCallTask = C8oCallTask; //# sourceMappingURL=c8oCallTask.js.map