UNPKG

@nest-kr/integration

Version:

```cli npm i @nest-kr/integration ```

76 lines (75 loc) 3.95 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 __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 }); require("reflect-metadata"); const common_1 = require("@nestjs/common"); const IntegrationEventHandlerMetadataKey_1 = require("./IntegrationEventHandlerMetadataKey"); let IntegrationEventBus = class IntegrationEventBus { constructor() { this.handlerInformations = new Map(); } handle(integrationEvent) { return __awaiter(this, void 0, void 0, function* () { const handlerInformations = this.handlerInformations.get(integrationEvent.subject) || []; if (handlerInformations.length === 0) { const handlerInformations = this.handlerInformations.get('__unhandled__') || []; yield Promise.all(handlerInformations.map(({ service, method }) => service[method].bind(service)(integrationEvent))); } else { yield Promise.all(handlerInformations.map(({ service, method }) => { return service[method].bind(service)(integrationEvent); })); } }); } handleUnsignedIntegrationEvent(unsignedIntegrationEvent) { return __awaiter(this, void 0, void 0, function* () { const handlerInformations = this.handlerInformations.get('__unsigned__') || []; yield Promise.all(handlerInformations.map(({ service, method }) => service[method].bind(service)(unsignedIntegrationEvent))); }); } register(service) { if (service instanceof Array) { service.map(item => this.registerService(item)); } else { this.registerService(service); } } registerService(service) { const metadataList = Reflect.getOwnMetadata(IntegrationEventHandlerMetadataKey_1.INTEGRATION_EVENT_HANDLER_METADATA_KEY, service.constructor) || []; metadataList.map(matadata => this.registerMetadata(service, matadata)); } registerMetadata(service, metadata) { const { subject, method } = metadata; const handlerInformationsForDomainEvent = this.handlerInformations.get(subject); const handlerInformation = { service, method }; if (handlerInformationsForDomainEvent) { handlerInformationsForDomainEvent.push(handlerInformation); } else { this.handlerInformations.set(subject, [handlerInformation]); } } }; IntegrationEventBus = __decorate([ common_1.Injectable(), __metadata("design:paramtypes", []) ], IntegrationEventBus); exports.IntegrationEventBus = IntegrationEventBus;