UNPKG

ns2-front-module-common

Version:
95 lines 4.28 kB
import { Injectable, Inject } from "@angular/core"; import { HttpService } from "./http.service"; /** * Класс для работы со справочниками */ var RefsService = (function () { function RefsService(httpService, cache) { this.httpService = httpService; this.cache = cache; } RefsService.prototype.setRefsEndPoint = function (_refsEndPoint) { this.refsEndPoint = _refsEndPoint; }; /** * Синхронизирует справочники с бэкендом * @returns {Promise<T>} */ RefsService.prototype.synchronize = function () { var _this = this; return new Promise(function (resolve) { var url = _this.refsEndPoint + 'refs/last-modified'; _this.httpService.get(url) .map(function (response) { return response.json(); }) .map(function (response) { return response.data; }) .subscribe(function (data) { var process = function (localInfo) { _this.processLastModified(localInfo, data).then(resolve).catch(resolve); }; _this.cache.get(RefsService.REFS_LAST_MODIFIED_INFO_KEY) .then(function (localInfo) { process(localInfo); }) .catch(function () { process({}); }); }, function () { resolve(); }); }); }; /** * Сравнивает локальные справочники и удаленные. * При обнаружении измененных, добавленных или удаленных справочников - удаляет все записи из кеша относящиеся к этим справочникам * * @param {any} localInfo Объект с локальными справочниками, где ключ - имя справочника, значение - timestamp последнего изменения * @param {any[]} remoteInfo Массив объектов с информацией о справочниках на сервере * @returns {Promise<[]>} */ RefsService.prototype.processLastModified = function (localInfo, remoteInfo) { var depends = []; var resultLocalInfo = {}; var updateLocalInfo = false; localInfo = localInfo || {}; // Проверяем измененные и добавленные справочники for (var _i = 0, remoteInfo_1 = remoteInfo; _i < remoteInfo_1.length; _i++) { var remoteItem = remoteInfo_1[_i]; remoteItem = remoteItem || {}; var remoteLastModified = remoteItem.updated || 0; var refName = remoteItem.class_name || null; if (!localInfo.hasOwnProperty(refName)) { updateLocalInfo = true; } else if (localInfo.hasOwnProperty(refName) && (localInfo[refName] < remoteLastModified)) { depends.push(this.cache.removeByTag(refName)); updateLocalInfo = true; } resultLocalInfo[refName] = remoteLastModified; } // Проверяем удаленные справочники var localKeys = Object.keys(localInfo); for (var i = 0; i < localKeys.length; i++) { var localRefName = localKeys[i]; if (!resultLocalInfo.hasOwnProperty(localRefName)) { depends.push(this.cache.removeByTag(localRefName)); updateLocalInfo = true; } } if (updateLocalInfo) { depends.push(this.cache.set(RefsService.REFS_LAST_MODIFIED_INFO_KEY, resultLocalInfo)); } return Promise.all(depends); }; return RefsService; }()); export { RefsService }; RefsService.REFS_LAST_MODIFIED_INFO_KEY = 'refs_last_modified_info'; RefsService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ RefsService.ctorParameters = function () { return [ { type: HttpService, }, { type: undefined, decorators: [{ type: Inject, args: ['StorageInterface',] },] }, ]; }; //# sourceMappingURL=refs.service.js.map