rokot-notification
Version:
Rokot - [Rocketmakers](http://www.rocketmakers.com/) TypeScript NodeJs Platform
84 lines (83 loc) • 3.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
require("reflect-metadata");
const metadataKeys_1 = require("./metadataKeys");
class MessageHandlerRegistry {
constructor() {
this.items = [];
}
get(type) {
const item = this.items.filter(i => i.notificationType === type);
if (item.length) {
return item[0];
}
}
getAll() {
return this.items;
}
register(notificationType, name, group, HandlerClass, transports, validators, testCases, templates) {
this.items.push({ notificationType, name, group, HandlerClass, transports, validators, testCases, templates });
}
}
const registry = new MessageHandlerRegistry();
exports.messageHandlerDecoration = registry;
function getMessageValidators(target) {
return Reflect.getMetadata(metadataKeys_1.MetadataKeys.templateMessageValidators, target) || [];
}
function getNotificationTestCases(target) {
return Reflect.getMetadata(metadataKeys_1.MetadataKeys.notificationTestCase, target) || [];
}
function getCompilers(target) {
return Reflect.getMetadata(metadataKeys_1.MetadataKeys.templateCompiler, target) || [];
}
class Notification {
handler(type, name = `MessageHandler-${type}`, group = "") {
return function (target) {
const frequencies = Reflect.getMetadata(metadataKeys_1.MetadataKeys.frequency, target.prototype);
const validators = getMessageValidators(target.prototype);
const testCases = getNotificationTestCases(target.prototype);
const templates = getCompilers(target.prototype);
registry.register(type, name, group, target, frequencies, validators, testCases, templates);
};
}
template() {
return (target, methodName, descriptor) => {
const compilers = getCompilers(target);
compilers.push(methodName);
Reflect.defineMetadata(metadataKeys_1.MetadataKeys.templateCompiler, compilers, target);
};
}
transformTemplate(notification, func) {
const tp = Reflect.getMetadata(metadataKeys_1.MetadataKeys.templateProcessor, func);
if (!tp) {
throw new Error("Is the function being passed in decorated with @notification.template()");
}
return tp(notification);
}
testCase() {
return (target, methodName, descriptor) => {
const testCases = getNotificationTestCases(target);
testCases.push(methodName);
Reflect.defineMetadata(metadataKeys_1.MetadataKeys.notificationTestCase, testCases, target);
};
}
validator() {
return (target, methodName, descriptor) => {
const templateMessageValidators = getMessageValidators(target);
templateMessageValidators.push(methodName);
Reflect.defineMetadata(metadataKeys_1.MetadataKeys.templateMessageValidators, templateMessageValidators, target);
};
}
transport(...availableFrequencies) {
if (!availableFrequencies.length) {
availableFrequencies.push("immediate");
}
return function (target, methodName, descriptor) {
const frequencies = Reflect.getMetadata(metadataKeys_1.MetadataKeys.frequency, target) || [];
frequencies.push({ methodName, defaultFrequency: availableFrequencies[0], availableFrequencies });
Reflect.defineMetadata(metadataKeys_1.MetadataKeys.frequency, frequencies, target);
};
}
}
exports.Notification = Notification;
exports.notifications = new Notification();