UNPKG

acelga-bus

Version:

An extensible typescript message bus with support for middlewares

70 lines 2.84 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); class BulkDispatcher { constructor(dispatcher, scheduler, pipelineFactory, errorLogger) { this.dispatcher = dispatcher; this.scheduler = scheduler; this.pipelineFactory = pipelineFactory; this.errorLogger = errorLogger; } on(eventType, callback) { return this.dispatcher.on(eventType, callback); } onAny(callback) { return this.dispatcher.onAny(callback); } trigger(events) { return __awaiter(this, void 0, void 0, function* () { try { const plan = this.scheduler.schedule(events); const pipelinesPromises = plan.plan.map(pipelinePlan => this.executePipelinePlan(pipelinePlan)); const results = yield this.mapPipelinePromises(pipelinesPromises); const errors = this.getAllErrors(results); if (errors.length) { return Promise.reject(errors); } } catch (error) { return Promise.reject(error); } }); } getAllErrors(results) { const errors = []; results.forEach(result => { if (Array.isArray(result)) { result.forEach(error => errors.push(error)); } }); return errors; } executePipelinePlan(pipelinePlan) { const pipeline = this.pipelineFactory(this.dispatcher); if (pipelinePlan.preserveOrder) return pipeline.executeStopOnError(pipelinePlan.payloads); return pipeline.executeContinueOnError(pipelinePlan.payloads); } mapPipelinePromises(pipelines) { const results = []; const pipelinesPromises = pipelines.map((pipeline, index) => { return pipeline .then(result => results[index] = result) .catch((error) => this.errorLogger(error)); }); return Promise.all(pipelinesPromises) .then(() => results); } off(eventType, callback) { this.dispatcher.off(eventType, callback); } } exports.default = BulkDispatcher; //# sourceMappingURL=bulkDispatcher.js.map