UNPKG

stellar-plus

Version:

beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain

77 lines (76 loc) 2.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConveyorBelt = void 0; const tslib_1 = require("tslib"); const uuid_1 = require("uuid"); const error_1 = require("../../../../stellar-plus/error"); class ConveyorBelt { constructor(args) { this.type = args.type; this.id = (0, uuid_1.v4)(); this.plugins = args.plugins; } execute(item, existingItemId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const itemId = existingItemId || (0, uuid_1.v4)(); const preProcessedItem = yield this.preProcess(item, itemId); let processedItem; try { processedItem = (yield this.process(preProcessedItem, itemId)); } catch (e) { const error = e instanceof error_1.StellarPlusError ? e : error_1.StellarPlusError.fromUnkownError(e); const processedError = yield this.processError(error, itemId); throw processedError; } const postProcessedItem = yield this.postProcess(processedItem, itemId); return postProcessedItem; }); } preProcess(item, itemId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { let preProcessedItem = item; for (const plugin of this.plugins) { if (plugin.preProcess) { preProcessedItem = (yield plugin.preProcess(preProcessedItem, this.getMeta(itemId))); } } return preProcessedItem; }); } postProcess(item, itemId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { let postProcessedItem = item; for (const plugin of this.plugins) { if (plugin.postProcess) { postProcessedItem = (yield plugin.postProcess(postProcessedItem, this.getMeta(itemId))); } } return postProcessedItem; }); } processError(error, itemId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { let processedError = error; for (const plugin of this.plugins) { if (plugin.processError) { processedError = (yield plugin.processError(processedError, this.getMeta(itemId))); } } return processedError; }); } process(_item, _itemId) { return tslib_1.__awaiter(this, void 0, void 0, function* () { throw new Error('process function not implemented'); }); } getMeta(itemId) { return { itemId, beltId: this.id, beltType: this.type, }; } } exports.ConveyorBelt = ConveyorBelt;