mediatr-ts
Version:
Mimic the famous MediatR csharp library see: (https://github.com/jbogard/MediatR)
68 lines • 3.48 kB
JavaScript
;
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 __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.NotificationHandlerMappings = void 0;
var orderedMappings_js_1 = require("./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(orderedMappings_js_1.byOrder);
};
return NotificationHandlerMappings;
}(orderedMappings_js_1.OrderedMappings));
exports.NotificationHandlerMappings = NotificationHandlerMappings;
//# sourceMappingURL=notificationHandlerMappings.js.map