UNPKG

@allgemein/eventbus

Version:
139 lines 5.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EventBus = void 0; const EventBusMeta_1 = require("./EventBusMeta"); const EventChannel_1 = require("./EventChannel"); const EventBusConfiguration_1 = require("./EventBusConfiguration"); const CryptUtils_1 = require("../utils/CryptUtils"); const EventBusAdapterFactory_1 = require("../adapter/EventBusAdapterFactory"); const lodash_1 = require("lodash"); const Constants_1 = require("../Constants"); const DEFAULT_OPTIONS = { name: 'default', adapter: 'default', extra: { maxListener: 1000 } }; class EventBus { constructor() { this.configurations = {}; this.inc = 0; this.channels = {}; this.nodeId = CryptUtils_1.CryptUtils.shorthash(Date.now() + CryptUtils_1.CryptUtils.random(8)); this.addConfiguration(DEFAULT_OPTIONS); } static get namespaces() { return Object.keys(this.$().channels); } static $() { if (!this.self) { this.self = new EventBus(); } return this.self; } static async register(o) { // support multiple subsriber in one class const infos = EventBusMeta_1.default.$().getSubscriberInfo(o); if ((0, lodash_1.isEmpty)(infos)) { throw new Error('registration went wrong'); } for (const info of infos) { const channel = await this.$().getOrCreateChannel(info.namespace, info.configuration, info.configurationOptions); await channel.register(o, info.method, this.$().nodeId); } } static async unregister(o) { const infos = EventBusMeta_1.default.$().getSubscriberInfo(o); if (!(0, lodash_1.isEmpty)(infos)) { for (const info of infos) { let channel = await this.$().getOrCreateChannel(info.namespace); channel.unregister(o); if (channel.size === 0) { channel = this.$().channels[info.namespace]; await channel.close(); delete this.$().channels[info.namespace]; } } } } static postOnChannel(namespace, o, options) { return new Promise(async (resolve, reject) => { const channel = await this.$().getOrCreateChannel(namespace, options && options.configuration ? options.configuration : 'default', options && options.configurationOptions ? options.configurationOptions : null); try { const res = await channel.post(o, options); resolve(res); } catch (e) { reject(e); } }); } static post(o, options) { // TODO check is supported type? let info = EventBusMeta_1.default.$().getNamespacesForEvent(o); if ((0, lodash_1.isEmpty)(info)) { const eventDef = EventBusMeta_1.default.$().registerEventClass(o.constructor); info = [eventDef.namespace]; } if (info.length) { this.$().inc++; } const promises = []; for (const _namespace of info) { promises.push(this.postOnChannel(_namespace, o, options)); } return Promise.all(promises); } static postAndForget(o, options) { options = options || {}; (0, lodash_1.set)(options, Constants_1.K_TTL, 0); return this.post(o, options); } /** * cls must inherit the AbstractEventBusAdapter class * * @param cls * @param name */ static registerAdapter(cls, name) { EventBusAdapterFactory_1.EventBusAdapterFactory.$().register(cls); } static unregisterAdapter(name_or_cls) { EventBusAdapterFactory_1.EventBusAdapterFactory.$().unregister(name_or_cls); } addConfiguration(cfg) { const cfgImpl = new EventBusConfiguration_1.EventBusConfiguration(this, cfg); this.configurations[cfgImpl.name] = cfgImpl; return cfgImpl; } getConfiguration(name = 'default') { if (!this.configurations[name]) { throw new Error('no configuration for adapter ' + name); } return this.configurations[name]; } getChannel(name) { if (!this.channels[name]) { return null; } return this.channels[name]; } async getOrCreateChannel(name, configName, configOpts) { if (!this.channels[name]) { const config = this.getConfiguration(configName); const clazz = EventBusMeta_1.default.$().getClassForNamespace(name); const adapter = config.createAdapter(this.nodeId, name, clazz, configOpts); const channel = new EventChannel_1.EventChannel(this.nodeId, name, adapter); this.channels[name] = channel; } return this.channels[name]; } async shutdown() { await Promise.all((0, lodash_1.values)(this.channels).map(c => c.close())); EventBus.self = null; } } exports.EventBus = EventBus; EventBus.self = null; //# sourceMappingURL=EventBus.js.map