UNPKG

rokot-notification

Version:

Rokot - [Rocketmakers](http://www.rocketmakers.com/) TypeScript NodeJs Platform

75 lines (74 loc) 3.6 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 }); const decorators_1 = require("./decorators"); const common_1 = require("./common"); class NotificationDispatcher { constructor(logger, dispatchHandlers, handlerClassConstructor) { this.logger = logger; this.dispatchHandlers = dispatchHandlers; this.handlerClassConstructor = handlerClassConstructor; } dispatch(notification) { return __awaiter(this, void 0, void 0, function* () { this.logger.trace(`Dispatching notification type ${notification.type}`); this.validateMessageCore(notification); const decoration = this.findMessageHandlerDecoration(notification); const messageHandler = common_1.constructNotificationMessageHandler(decoration, this.handlerClassConstructor); const msg = yield this.validateMessage(decoration, notification, messageHandler); const collect = []; for (const dispatchHandler of this.dispatchHandlers) { let result = yield common_1.resolvePromiseOrResult(dispatchHandler.onDispatch(msg, messageHandler)); if (result) { collect.push(result); } else { collect.push({ transport: dispatchHandler.transportName }); } } return collect; }); } shutdown() { this.dispatchHandlers.forEach(dh => dh.shutdown && dh.shutdown()); } validateMessageCore(notification) { if (!notification.type || !notification.recipient) { const msg = `Invalid Message payload - missing core requirements of 'type' and 'recipient': ${JSON.stringify(notification)}`; this.logger.error(msg); throw new Error(msg); } } findMessageHandlerDecoration(notification) { const decoration = decorators_1.messageHandlerDecoration.get(notification.type); if (!decoration) { const msg = `No notification handler registered for notification type: ${notification.type}`; this.logger.error(msg); throw new Error(msg); } return decoration; } validateMessage(decoration, notification, handler) { return __awaiter(this, void 0, void 0, function* () { this.logger.trace(`Checking validation for notification type ${notification.type}`); const validators = decoration.validators; if (!validators || !validators.length) { this.logger.trace(`-- Not Required`); return notification; } for (let validate of validators) { this.logger.trace(`-- Validating with ${validate}`); notification = yield common_1.resolvePromiseOrResult(handler[validate](notification)); } return notification; }); } } exports.NotificationDispatcher = NotificationDispatcher;