UNPKG

@nestjstools/messaging

Version:

Simplifies asynchronous and synchronous message handling with support for buses, handlers, channels, and consumers. Build scalable, decoupled applications with ease and reliability.

47 lines 2.72 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DistributedMessageBus = void 0; const common_1 = require("@nestjs/common"); const message_response_1 = require("../message/message-response"); const message_bus_collection_1 = require("./message-bus.collection"); const routing_message_1 = require("../message/routing-message"); const message_factory_1 = require("../message/message.factory"); const normalizer_registry_1 = require("../normalizer/normalizer.registry"); let DistributedMessageBus = class DistributedMessageBus { constructor(messageBusCollection, normalizerRegistry) { this.messageBusCollection = messageBusCollection; this.normalizerRegistry = normalizerRegistry; } async dispatch(message) { if (!(message instanceof routing_message_1.RoutingMessage)) { throw new Error(`Message must be instance of ${routing_message_1.RoutingMessage.name}`); } const response = []; for (const collection of this.messageBusCollection.getAll()) { const normalizedMessage = await this.normalizerRegistry .getByName(collection.channel.config.normalizer.name) .normalize(message.message, message.messageRoutingKey); const handlerResponse = await collection.messageBus.dispatch(message_factory_1.MessageFactory.creteSealedFromMessage(normalizedMessage, message)); if (handlerResponse) { response.push(handlerResponse); } } return new message_response_1.MessageResponse(response); } }; exports.DistributedMessageBus = DistributedMessageBus; exports.DistributedMessageBus = DistributedMessageBus = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [message_bus_collection_1.MessageBusCollection, normalizer_registry_1.NormalizerRegistry]) ], DistributedMessageBus); //# sourceMappingURL=distributed-message.bus.js.map