UNPKG

c8osdkjscore

Version:
682 lines 38.6 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 c8oFullSyncTranslator_1 = require("./c8oFullSyncTranslator"); var c8oLocalCacheResponse_1 = require("./c8oLocalCacheResponse"); var c8oResponse_1 = require("./c8oResponse"); var c8oUtilsCore_1 = require("./c8oUtilsCore"); var c8oCouchBaseLiteException_1 = require("./Exception/c8oCouchBaseLiteException"); var c8oException_1 = require("./Exception/c8oException"); var c8oExceptionMessage_1 = require("./Exception/c8oExceptionMessage"); var c8oRessourceNotFoundException_1 = require("./Exception/c8oRessourceNotFoundException"); var c8oUnavailableLocalCacheException_1 = require("./Exception/c8oUnavailableLocalCacheException"); var fullSyncDatabase_1 = require("./fullSyncDatabase"); var fullSyncDeleteDocumentParameter_1 = require("./fullSyncDeleteDocumentParameter"); var fullSyncRequestable_1 = require("./fullSyncRequestable"); var fullSyncResponse_1 = require("./fullSyncResponse"); var C8oFullSync = (function () { function C8oFullSync(c8o) { this.c8o = c8o; this.fullSyncDatabaseUrlBase = c8o.endpointConvertigo + C8oFullSync.FULL_SYNC_URL_PATH; this.localSuffix = (c8o.fullSyncLocalSuffix !== null) ? c8o.fullSyncLocalSuffix : "_device"; } C8oFullSync.prototype.handleFullSyncRequest = function (_parameters, listener) { return __awaiter(this, void 0, void 0, function () { var _this = this; var parameters, val, projectParameterValue, fullSyncRequestableValue, fullSyncRequestable, databaseName, response; return __generator(this, function (_a) { parameters = {}; for (val in _parameters) { if (_parameters[val] instanceof Blob != true) { parameters[val] = JSON.parse(JSON.stringify(_parameters[val])); } else { parameters[val] = _parameters[val]; } } projectParameterValue = c8oUtilsCore_1.C8oUtilsCore.peekParameterStringValue(parameters, c8oCore_1.C8oCore.ENGINE_PARAMETER_PROJECT, true); if (projectParameterValue.indexOf(C8oFullSync.FULL_SYNC_PROJECT) !== 0) { throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.invalidParameterValue(projectParameterValue, "its don't start with" + C8oFullSync.FULL_SYNC_PROJECT)); } fullSyncRequestableValue = c8oUtilsCore_1.C8oUtilsCore.peekParameterStringValue(parameters, c8oCore_1.C8oCore.ENGINE_PARAMETER_SEQUENCE, true); if (fullSyncRequestableValue.indexOf("#") !== -1) { fullSyncRequestableValue = fullSyncRequestableValue.substring(0, fullSyncRequestableValue.indexOf("#")); } fullSyncRequestable = fullSyncRequestable_1.FullSyncRequestable.getFullSyncRequestable(fullSyncRequestableValue); if (fullSyncRequestable === null) { throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.invalidParameterValue(c8oCore_1.C8oCore.ENGINE_PARAMETER_PROJECT, c8oExceptionMessage_1.C8oExceptionMessage.unknownValue("fullSync requestable", fullSyncRequestableValue))); } databaseName = projectParameterValue.substring(C8oFullSync.FULL_SYNC_PROJECT.length); if (databaseName.length < 1) { databaseName = this.c8o.defaultDatabaseName; if (databaseName === null) { throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.invalidParameterValue(c8oCore_1.C8oCore.ENGINE_PARAMETER_PROJECT, c8oExceptionMessage_1.C8oExceptionMessage.missingValue("fullSync database name"))); } } return [2, new Promise(function (resolve, reject) { fullSyncRequestable.handleFullSyncRequest(_this, databaseName, parameters, listener).then(function (result) { response = result; if (response === null || response === undefined) { reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.couchNullResult())); } resolve(_this.handleFullSyncResponse(response, listener)); }).catch(function (error) { if (error instanceof c8oException_1.C8oException) { reject(error); } else { reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.FullSyncRequestFail(), error)); } }); })]; }); }); }; C8oFullSync.prototype.handleFullSyncResponse = function (response, listener) { return response; }; C8oFullSync.isFullSyncRequest = function (requestParameter) { if (c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(requestParameter, c8oCore_1.C8oCore.ENGINE_PARAMETER_PROJECT, false) !== null) { return c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(requestParameter, c8oCore_1.C8oCore.ENGINE_PARAMETER_PROJECT, false).indexOf(C8oFullSync.FULL_SYNC_PROJECT) === 0; } else { return false; } }; C8oFullSync.FULL_SYNC_URL_PATH = "/fullsync/"; C8oFullSync.FULL_SYNC_PROJECT = "fs://"; C8oFullSync.FULL_SYNC__ID = "_id"; C8oFullSync.FULL_SYNC__REV = "_rev"; C8oFullSync.FULL_SYNC__ATTACHMENTS = "_attachments"; return C8oFullSync; }()); exports.C8oFullSync = C8oFullSync; var C8oFullSyncCbl = (function (_super) { __extends(C8oFullSyncCbl, _super); function C8oFullSyncCbl(c8o) { var _this = _super.call(this, c8o) || this; _this.fullSyncChangeListeners = []; _this.cblChangeListeners = []; _this.fullSyncDatabases = {}; return _this; } C8oFullSyncCbl.prototype.getOrCreateFullSyncDatabase = function (databaseName) { var localDatabaseName = databaseName + this.localSuffix; if (this.fullSyncDatabases[localDatabaseName] == null) { this.fullSyncDatabases[localDatabaseName] = new fullSyncDatabase_1.C8oFullSyncDatabase(this.c8o, databaseName, this.fullSyncDatabaseUrlBase, this.localSuffix); } return this.fullSyncDatabases[localDatabaseName]; }; C8oFullSyncCbl.prototype.handleFullSyncResponse = function (response, listener) { response = _super.prototype.handleFullSyncResponse.call(this, response, listener); if (listener instanceof c8oResponse_1.C8oResponseJsonListener) { if (response instanceof fullSyncResponse_1.FullSyncDocumentOperationResponse) { return c8oFullSyncTranslator_1.C8oFullSyncTranslator.fullSyncDocumentOperationResponseToJson(response); } else if (response instanceof fullSyncResponse_1.FullSyncDefaultResponse) { return c8oFullSyncTranslator_1.C8oFullSyncTranslator.fullSyncDefaultResponseToJson(response); } else if (response instanceof Object) { return response; } else { throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.illegalArgumentIncompatibleListener(listener.toString(), typeof response)); } } }; C8oFullSyncCbl.prototype.handleGetAttachmentUrlRequest = function (fullSyncDatabaseName, docid, parameters) { var fullSyncDatabase = null; fullSyncDatabase = this.getOrCreateFullSyncDatabase(fullSyncDatabaseName); var attachmentName = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, "attachment_name", false); return new Promise(function (resolve) { fullSyncDatabase.getdatabase.getAttachment(docid, attachmentName).then(function (buffer) { resolve(buffer); }); }); }; C8oFullSyncCbl.prototype.handleGetDocumentRequest = function (fullSyncDatabaseName, docid, parameters) { var fullSyncDatabase = null; var dictDoc = {}; var param; param = parameters["attachments"] ? { attachments: true } : {}; parameters["binary"] ? param["binary"] = true : {}; fullSyncDatabase = this.getOrCreateFullSyncDatabase(fullSyncDatabaseName); return new Promise(function (resolve, reject) { fullSyncDatabase.getdatabase.get(docid, param).then(function (document) { if (document != null) { dictDoc = document; var attachments = document[C8oFullSync.FULL_SYNC__ATTACHMENTS]; if (attachments != null) { for (var _i = 0, attachments_1 = attachments; _i < attachments_1.length; _i++) { var attachmentName = attachments_1[_i]; var url = attachments.url; var attachmentDesc = attachments[attachmentName]; attachmentDesc[C8oFullSyncCbl.ATTACHMENT_PROPERTY_KEY_CONTENT_URL] = url.toString(); var dictAny = {}; dictAny[attachmentName] = attachmentDesc; dictDoc[C8oFullSyncCbl.FULL_SYNC__ATTACHMENTS] = dictAny; } } } else { throw new c8oRessourceNotFoundException_1.C8oRessourceNotFoundException((c8oExceptionMessage_1.C8oExceptionMessage.ressourceNotFound("requested document \"" + docid + "\""))); } if (dictDoc === null) { dictDoc = {}; } resolve(dictDoc); }) .catch(function (error) { reject(error); }); }); }; C8oFullSyncCbl.prototype.handleDeleteDocumentRequest = function (DatabaseName, docid, parameters) { var fullSyncDatabase = null; fullSyncDatabase = this.getOrCreateFullSyncDatabase(DatabaseName); var revParameterValue = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, fullSyncDeleteDocumentParameter_1.FullSyncDeleteDocumentParameter.REV.name, false); var documentRevision; if (revParameterValue === null) { return new Promise(function (resolve, reject) { fullSyncDatabase.getdatabase.get(docid).then(function (doc) { if (doc === null) { throw new c8oRessourceNotFoundException_1.C8oRessourceNotFoundException("Cannot find document"); } documentRevision = doc._rev; return fullSyncDatabase.getdatabase.remove(doc); }).then(function (result) { resolve(new fullSyncResponse_1.FullSyncDocumentOperationResponse(docid, documentRevision, result.ok)); }).catch(function (err) { reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.couchRequestDeleteDocument(), err)); }); }); } else { return new Promise(function (resolve, reject) { fullSyncDatabase.getdatabase.remove(docid, revParameterValue) .then(function (result) { resolve(new fullSyncResponse_1.FullSyncDocumentOperationResponse(docid, documentRevision, result.ok)); }).catch(function (err) { reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.couchRequestDeleteDocument(), err)); }); }); } }; C8oFullSyncCbl.prototype.handlePostDocumentRequest = function (databaseName, fullSyncPolicy, parameters) { var fullSyncDatabase; fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); var subkeySeparatorParameterValue = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, c8oCore_1.C8oCore.FS_SUBKEY_SEPARATOR, false); if (subkeySeparatorParameterValue == null) { subkeySeparatorParameterValue = "."; } var newProperties = {}; for (var i = 0; i < Object.keys(parameters).length; i++) { var parameterName = Object.keys(parameters)[i]; if (parameterName.indexOf("__") !== 0 && parameterName.indexOf("_use_") !== 0) { var objectParameterValue = parameters[Object.keys(parameters)[i]]; var paths = parameterName.split(subkeySeparatorParameterValue); if (paths.length > 1) { parameterName = paths[0]; var count = paths.length - 1; while (count > 0) { var tmpObject = {}; tmpObject[paths[count]] = objectParameterValue; objectParameterValue = tmpObject; count--; } var existProperty = newProperties[parameterName]; if (existProperty != null) { C8oFullSyncCbl.mergeProperties(objectParameterValue, existProperty); } } newProperties[parameterName] = objectParameterValue; } } var db = fullSyncDatabase.getdatabase; return new Promise(function (resolve, reject) { fullSyncPolicy.action(db, newProperties).then(function (createdDocument) { var fsDocOpeResp = new fullSyncResponse_1.FullSyncDocumentOperationResponse(createdDocument.id, createdDocument.rev, createdDocument.ok); resolve(fsDocOpeResp); }).catch(function (error) { reject(error); }); }); }; C8oFullSyncCbl.prototype.handlePutAttachmentRequest = function (databaseName, docid, attachmentName, attachmentType, attachmentContent) { var document = null; var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); return new Promise(function (resolve) { fullSyncDatabase.getdatabase.get(docid).then(function (result) { document = result; if (document !== null) { fullSyncDatabase.getdatabase.putAttachment(docid, attachmentName, result._rev, attachmentContent, attachmentType) .then(function (result) { resolve(new fullSyncResponse_1.FullSyncDocumentOperationResponse(result._id, result._rev, result.ok)); }).catch(function (err) { throw new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException("Unable to put the attachment " + attachmentName + " to the document " + docid + ".", err); }); } else { throw new c8oRessourceNotFoundException_1.C8oRessourceNotFoundException("Cannot find document"); } }); }); }; C8oFullSyncCbl.prototype.handleDeleteAttachmentRequest = function (databaseName, docid, attachmentName) { var document = null; var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); return new Promise(function (resolve, reject) { fullSyncDatabase.getdatabase.get(docid).then(function (result) { document = result; }).then(function () { if (document !== null) { fullSyncDatabase.getdatabase.removeAttachment(docid, attachmentName, document._rev).catch(function (err) { throw new c8oCouchBaseLiteException_1.C8oCouchBaseLiteException("Unable to delete the attachment " + attachmentName + " to the document " + docid + ".", err); }); } else { throw new c8oRessourceNotFoundException_1.C8oRessourceNotFoundException("Document do not exists"); } resolve(new fullSyncResponse_1.FullSyncDocumentOperationResponse(document._id, document._rev, true)); }) .catch(function (err) { reject(new c8oException_1.C8oException(err.message, err)); }); }); }; C8oFullSyncCbl.prototype.handleAllDocumentsRequest = function (databaseName, parameters) { var fullSyncDatabase = null; fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); return new Promise(function (resolve, reject) { fullSyncDatabase.getdatabase .allDocs(parameters) .then(function (res) { resolve(res); }) .catch(function (err) { reject(new c8oException_1.C8oException(err.stack)); }); }); }; C8oFullSyncCbl.prototype.handleGetViewRequest = function (databaseName, ddocName, viewName, parameters) { var _this = this; var fullSyncDatabase = null; fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); var attachments; var binary; var include_docs; if (parameters["attachments"] && window["cblite"] !== undefined) { attachments = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, "attachments", false); binary = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, "binary", false); include_docs = c8oUtilsCore_1.C8oUtilsCore.getParameterStringValue(parameters, "include_docs", false); } return new Promise(function (resolve, reject) { fullSyncDatabase.getdatabase.query(ddocName + "/" + viewName, parameters) .then(function (result) { if (attachments) { var array_1 = []; var len_1 = 0; for (var _i = 0, _a = result.rows; _i < _a.length; _i++) { var resp = _a[_i]; _this.handleGetDocumentRequest(databaseName, resp.id, { attachments: true, binary: true, include_docs: true, }).then(function (getResult) { array_1.push(getResult); len_1++; if (len_1 === result.rows.length) { result.rows = array_1; resolve(result); } }); } } else { resolve(result); } }).catch(function (error) { reject(new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.couchRequestGetView(), error)); }); }); }; C8oFullSyncCbl.prototype.handleSyncRequest = function (databaseName, parameters, c8oResponseListener) { var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); return fullSyncDatabase.startAllReplications(parameters, c8oResponseListener); }; C8oFullSyncCbl.prototype.handleReplicatePullRequest = function (databaseName, parameters, c8oResponseListener) { var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); return fullSyncDatabase.startPullReplication(parameters, c8oResponseListener); }; C8oFullSyncCbl.prototype.handleReplicatePushRequest = function (databaseName, parameters, c8oResponseListener) { var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); return fullSyncDatabase.startPushReplication(parameters, c8oResponseListener); }; C8oFullSyncCbl.prototype.handleResetDatabaseRequest = function (databaseName) { var _this = this; return new Promise(function (resolve) { _this.handleDestroyDatabaseRequest(databaseName).then(function () { resolve(_this.handleCreateDatabaseRequest(databaseName)); }); }); }; C8oFullSyncCbl.prototype.handleCreateDatabaseRequest = function (databaseName) { this.getOrCreateFullSyncDatabase(databaseName); return new fullSyncResponse_1.FullSyncDefaultResponse(true); }; C8oFullSyncCbl.prototype.handleBulkRequest = function (databaseName, parameters) { var _this = this; var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); return new Promise(function (resolve, reject) { fullSyncDatabase.getdatabase.load(parameters["data"], { proxy: _this.c8o.endpointConvertigo + "/fullsync/" + fullSyncDatabase.getdatabseName }).then(function () { resolve(new fullSyncResponse_1.FullSyncDefaultResponse(true)); }).catch(function (err) { reject(new c8oException_1.C8oException("Bulk Load failed", err)); }); }); }; C8oFullSyncCbl.prototype.handleInfoRequest = function (databaseName) { var fullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); return new Promise(function (resolve, reject) { fullSyncDatabase.getdatabase.info() .then(function (response) { resolve(response); }).catch(function (err) { reject(new c8oException_1.C8oException("Get info failed", err)); }); }); }; C8oFullSyncCbl.prototype.handleDestroyDatabaseRequest = function (databaseName) { var _this = this; return new Promise(function (resolve, reject) { var localDatabaseName = databaseName + _this.localSuffix; _this.getOrCreateFullSyncDatabase(databaseName).deleteDB().then(function (response) { if (_this.fullSyncDatabases[localDatabaseName] !== null) { delete _this.fullSyncDatabases[localDatabaseName]; } resolve(new fullSyncResponse_1.FullSyncDefaultResponse(response.ok)); }).catch(function (err) { reject(new c8oException_1.C8oException(err.message, err)); }); }); }; C8oFullSyncCbl.mergeProperties = function (newProperties, oldProperties) { for (var i = 0; i < Object.keys(oldProperties).length; i++) { var oldPropertyKey = Object.keys(oldProperties)[i]; var oldPropertyValue = oldProperties[Object.keys(oldProperties)[i]]; if (newProperties[oldPropertyKey] !== null && newProperties[oldPropertyKey] !== undefined) { var newDocumentValue = newProperties[oldPropertyKey]; if (Object.prototype.toString.call(newDocumentValue) === "[object Array]" && Object.prototype.toString.call(oldPropertyValue) === "[object Array]") { C8oFullSyncCbl.mergeArrayProperties(newDocumentValue, oldPropertyValue); } else if (typeof (newDocumentValue) === "object" && typeof (oldPropertyValue) === "object") { C8oFullSyncCbl.mergeProperties(newDocumentValue, oldPropertyValue); } else { } } else { newProperties[oldPropertyKey] = oldPropertyValue; } } }; C8oFullSyncCbl.mergeArrayProperties = function (newArray, oldArray) { var newArraySize = newArray.length; var oldArraySize = oldArray.length; for (var i = 0; i < oldArraySize; i++) { var newArrayValue = null; if (i < newArraySize) { newArrayValue = newArray[i]; } var oldArrayValue = oldArray[i]; if (newArrayValue !== null) { if (newArrayValue instanceof Array && oldArrayValue instanceof Array) { C8oFullSyncCbl.mergeArrayProperties(newArrayValue, oldArrayValue); } else if (typeof (newArrayValue) === "object" && typeof (oldArrayValue) === "object") { C8oFullSyncCbl.mergeProperties(newArrayValue, oldArrayValue); } else { } } else { newArray.push(oldArrayValue); } } }; C8oFullSyncCbl.prototype.getDocucmentFromDatabase = function (c8o, databaseName, documentId) { var c8oFullSyncDatabase; try { c8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); } catch (err) { throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncGetOrCreateDatabase(databaseName)); } return new Promise(function (resolve) { c8oFullSyncDatabase.getdatabase().get(documentId).then(function (result) { resolve(result); }); }); }; C8oFullSyncCbl.prototype.overrideDocument = function (document, properties, databaseName) { properties[C8oFullSync.FULL_SYNC__REV] = document._rev; var c8oFullSyncDatabase; try { c8oFullSyncDatabase = this.getOrCreateFullSyncDatabase(databaseName); } catch (err) { throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.fullSyncGetOrCreateDatabase(databaseName)); } c8oFullSyncDatabase.getdatabase().put(properties) .catch(function (err) { throw new c8oException_1.C8oException(err.message, err); }); }; C8oFullSyncCbl.prototype.getResponseFromLocalCache = function (c8oCallRequestIdentifier) { return __awaiter(this, void 0, void 0, function () { var fullSyncDatabase, localCacheDocument; return __generator(this, function (_a) { fullSyncDatabase = this.getOrCreateFullSyncDatabase(c8oCore_1.C8oCore.LOCAL_CACHE_DATABASE_NAME); localCacheDocument = null; return [2, new Promise(function (resolve, reject) { fullSyncDatabase.getdatabase.get(c8oCallRequestIdentifier).then(function (result) { localCacheDocument = result; var response = JSON.stringify(localCacheDocument[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE]); var responseType = localCacheDocument[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE_TYPE]; var expirationDate = localCacheDocument[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_EXPIRATION_DATE]; var responseString = null; var responseTypeString = null; var expirationDateNumber = -1; if (response != null) { if (typeof response === "string") { responseString = response; } else { throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.InvalidLocalCacheResponseInformation()); } } if (responseType != null) { if (typeof responseType === "string") { responseTypeString = responseType; } else { throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.InvalidLocalCacheResponseInformation()); } } if (expirationDate != null) { if (typeof expirationDate === "number") { expirationDateNumber = expirationDate; var currentTime = new Date().getTime(); if (expirationDateNumber < currentTime) { throw new c8oUnavailableLocalCacheException_1.C8oUnavailableLocalCacheException(c8oExceptionMessage_1.C8oExceptionMessage.timeToLiveExpired()); } } else { throw new c8oException_1.C8oException(c8oExceptionMessage_1.C8oExceptionMessage.InvalidLocalCacheResponseInformation()); } } resolve(new c8oLocalCacheResponse_1.C8oLocalCacheResponse(responseString, responseTypeString, expirationDateNumber)); }).catch(function (err) { if (err.status === 404) { resolve(new c8oUnavailableLocalCacheException_1.C8oUnavailableLocalCacheException(c8oExceptionMessage_1.C8oExceptionMessage.localCacheDocumentJustCreated())); } else { reject(err); } }); })]; }); }); }; C8oFullSyncCbl.prototype.saveResponseToLocalCache = function (c8oCallRequestIdentifier, localCacheResponse) { return __awaiter(this, void 0, void 0, function () { var fullSyncDatabase; return __generator(this, function (_a) { fullSyncDatabase = this.getOrCreateFullSyncDatabase(c8oCore_1.C8oCore.LOCAL_CACHE_DATABASE_NAME); return [2, new Promise(function (resolve) { fullSyncDatabase.getdatabase.get(c8oCallRequestIdentifier).then(function (localCacheDocument) { var properties = {}; properties[C8oFullSync.FULL_SYNC__ID] = c8oCallRequestIdentifier; properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE] = localCacheResponse.getResponse(); properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE_TYPE] = localCacheResponse.getResponseType(); if (localCacheResponse.getExpirationDate() > 0) { properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_EXPIRATION_DATE] = localCacheResponse.getExpirationDate(); } var currentRevision = localCacheDocument._rev; if (currentRevision !== null) { properties[C8oFullSyncCbl.FULL_SYNC__REV] = currentRevision; } fullSyncDatabase.getdatabase.put(properties).then(function (result) { resolve(result); }); }).catch(function (error) { if (error.status === 404) { var properties = {}; properties[C8oFullSync.FULL_SYNC__ID] = c8oCallRequestIdentifier; properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE] = localCacheResponse.getResponse(); properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_RESPONSE_TYPE] = localCacheResponse.getResponseType(); if (localCacheResponse.getExpirationDate() > 0) { properties[c8oCore_1.C8oCore.LOCAL_CACHE_DOCUMENT_KEY_EXPIRATION_DATE] = localCacheResponse.getExpirationDate(); } fullSyncDatabase.getdatabase.put(properties).then(function (result) { resolve(result); }); } else { resolve(error); } }); })]; }); }); }; C8oFullSyncCbl.prototype.addFullSyncChangeListener = function (db, listener) { if (db === null || db === "") { db = this.c8o.defaultDatabaseName; } var listeners = []; if (this.fullSyncChangeListeners[db] != null) { listeners[0] = this.fullSyncChangeListeners[db]; } else { listeners[0] = []; this.fullSyncChangeListeners[db] = listeners[0]; var evtHandler = this.getOrCreateFullSyncDatabase(db).getdatabase .changes({ since: "now", live: true, include_docs: true, }).on("change", function (change) { var changes = {}; var docs = []; var doc = {}; doc["id"] = change.doc._id; doc["rev"] = change.doc._rev; doc["isConflict"] = change.doc._conflicts; if (change.source != null) { doc["sourceUrl"] = change.source; } docs.push(doc); changes["changes"] = docs; for (var _i = 0, _a = listeners[0]; _i < _a.length; _i++) { var handler = _a[_i]; if (handler !== undefined) { handler.onChange(changes); } } }); this.cblChangeListeners[db] = evtHandler; } listeners[0].push(listener); }; C8oFullSyncCbl.prototype.removeFullSyncChangeListener = function (db, listener) { if (db === null || db === "") { db = this.c8o.defaultDatabaseName; } if (this.fullSyncChangeListeners[db] != null) { var listeners = this.fullSyncChangeListeners[db]; for (var list in listeners) { if (listeners[list] === listener) { delete listeners[list]; } } if (listeners.length === 0 || listeners == null) { this.getOrCreateFullSyncDatabase(db).getdatabase.cancel(); this.cblChangeListeners[db].cancel(); delete this.fullSyncChangeListeners[db]; delete this.cblChangeListeners[db]; } } }; C8oFullSyncCbl.ATTACHMENT_PROPERTY_KEY_CONTENT_URL = "content_url"; return C8oFullSyncCbl; }(C8oFullSync)); exports.C8oFullSyncCbl = C8oFullSyncCbl; //# sourceMappingURL=c8oFullSync.js.map