@allgemein/eventbus
Version:
67 lines • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EventBusAdapterFactory = void 0;
const DefaultEventBusAdapter_1 = require("./default/DefaultEventBusAdapter");
const Constants_1 = require("../Constants");
const lodash_1 = require("lodash");
/**
*
* Initialization
*
*/
class EventBusAdapterFactory {
constructor() {
this.busTypes = {};
}
static $() {
if (!this.$self) {
this.$self = new EventBusAdapterFactory();
}
return this.$self;
}
// private constructor() {
// this.register(NsqdEventBusAdapter);
// this.register(RedisEventBusAdapter);
// this.register(MqttEventBusAdapter);
// }
register(clazz, name) {
if (!name) {
name = (0, lodash_1.get)(clazz, Constants_1.K_ADAPTER_NAME, null);
}
if (!name) {
throw new Error('no name given for the adapter class');
}
this.busTypes[name] = clazz;
}
unregister(cls) {
for (const k of (0, lodash_1.keys)(this.busTypes)) {
if ((0, lodash_1.isString)(cls)) {
if (k === cls) {
delete this.busTypes[k];
break;
}
}
else if ((0, lodash_1.isFunction)(cls)) {
if (this.busTypes[k] === cls) {
delete this.busTypes[k];
}
}
}
}
create(type, nodeId, name, clazz, options) {
if ((0, lodash_1.isString)(type)) {
if ((0, lodash_1.has)(this.busTypes, type)) {
const _type = this.busTypes[type];
return Reflect.construct(_type, [nodeId, name, clazz, options]);
}
else {
return new DefaultEventBusAdapter_1.DefaultEventBusAdapter(nodeId, name, clazz, options);
}
}
else {
return Reflect.construct(type, [nodeId, name, clazz, options]);
}
}
}
exports.EventBusAdapterFactory = EventBusAdapterFactory;
//# sourceMappingURL=EventBusAdapterFactory.js.map