UNPKG

mediatr-ts

Version:

Mimic the famous MediatR csharp library see: (https://github.com/jbogard/MediatR)

65 lines 3.29 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { byOrder, OrderedMappings } from "./orderedMappings.js"; /** * NotificationHandlerMappings class extends OrderedMappings and is used to * manage notification handlers for specific notification classes. * * @exports */ var NotificationHandlerMappings = /** @class */ (function (_super) { __extends(NotificationHandlerMappings, _super); function NotificationHandlerMappings() { return _super !== null && _super.apply(this, arguments) || this; } /** * It allows you to specify the order in which notification handlers should be executed for a particular * notification class. * * It takes a notification class and an array of handler classes as input and updates the order of each handler in * the array based on its index. If not found it place the order as -1. * * @param notificationClass The notification class for which to set the order. * @param handlerClasses The array of handler classes in the order in which they should be executed. */ NotificationHandlerMappings.prototype.setOrder = function (notificationClass, handlerClasses) { var all = this.getAll(notificationClass); for (var _i = 0, all_1 = all; _i < all_1.length; _i++) { var handler = all_1[_i]; handler.order = handlerClasses.indexOf(handler.handlerClass); } }; /** * Retrieves all notification handlers for a specific notification class. * If no class is provided, it returns all handlers. * If no handlers are found for the specified class, it throws an error. * * The returned handlers are sorted by their order. * * @param notificationClass The notification class for which to retrieve the handlers. * @returns The array of handler classes in the order in which they should be executed. */ NotificationHandlerMappings.prototype.getAll = function (notificationClass) { var items = this._mappings.filter(function (p) { return p.notificationClass === notificationClass || !notificationClass; }); if (items.length === 0 && notificationClass) { throw new Error("No handler found for notification ".concat(notificationClass.name, ". Remember to decorate your handler with @notificationHandler(").concat(notificationClass.name, ").")); } return items.sort(byOrder); }; return NotificationHandlerMappings; }(OrderedMappings)); export { NotificationHandlerMappings }; //# sourceMappingURL=notificationHandlerMappings.js.map