UNPKG

test-ic-wallet-middleware-icrc

Version:
103 lines (102 loc) 4.04 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ServiceLocalCache = void 0; const common_1 = require("@ic-wallet-middleware/common"); const typedi_1 = require("typedi"); let ServiceLocalCache = class ServiceLocalCache { identifierService; logger; storage; constructor(logger, identifierService, storage) { this.identifierService = identifierService; this.logger = logger; this.storage = storage; } getService(serviceId) { return this.getServiceInternal(serviceId); } setService(service) { const key = this.getKey(service.servicePrincipal); this.storage.setItem(key, (0, common_1.jsonStringify)(service)); } getServiceAsset(serviceId, ledgerAddress) { const model = this.getServiceInternal(serviceId); if (model) { const asset = model.assets.find((a) => a.ledgerAddress === ledgerAddress); return asset; } return undefined; } setServiceAsset(serviceId, asset) { const service = this.getServiceInternal(serviceId); if (service) { const assetCache = service.assets.find((a) => a.ledgerAddress === asset.ledgerAddress); if (assetCache) { assetCache.deposit = asset.deposit; } else { service.assets.push(asset); } this.setService(service); } } getAllCredits(serviceId) { const key = this.getKeyCredits(serviceId); const value = this.storage.getItem(key); if (value) { try { const model = (0, common_1.jsonParse)(value); return model; } catch (e) { this.logger.logError(e, "Wrong local cache data", [value]); return undefined; } } return undefined; } setCredits(serviceId, credits) { const key = this.getKeyCredits(serviceId); this.storage.setItem(key, (0, common_1.jsonStringify)(credits)); } getServiceInternal(serviceId) { const key = this.getKey(serviceId); const value = this.storage.getItem(key); if (value) { try { const model = (0, common_1.jsonParse)(value); return model; } catch (e) { this.logger.logError(e, "Wrong local cache data", [value]); return undefined; } } return undefined; } getKey(serviceId) { return `${this.identifierService.getPrincipal()}-supported-assets-${serviceId}`; } getKeyCredits(serviceId) { return `${this.identifierService.getPrincipal()}-credits-${serviceId}`; } }; exports.ServiceLocalCache = ServiceLocalCache; exports.ServiceLocalCache = ServiceLocalCache = __decorate([ (0, typedi_1.Service)(), __param(0, (0, typedi_1.Inject)("ILogger")), __param(2, (0, typedi_1.Inject)("IStorage")), __metadata("design:paramtypes", [Object, common_1.IdentifierService, Object]) ], ServiceLocalCache);