UNPKG

@allgemein/eventbus

Version:
100 lines 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); class EventBusMeta { constructor() { this.$events = []; this.$types = []; } static $() { if (!this.self) { this.self = new EventBusMeta(); } return this.self; } static toNamespace(clazz) { let clazzName = null; if (!(0, lodash_1.isString)(clazz)) { clazzName = clazz.name; } else { clazzName = clazz; } return (0, lodash_1.kebabCase)(clazzName).replace(/\-/g, '_'); } getClassForNamespace(ns) { const c = this.$events.find(x => x.namespace === ns); if (c) { return c.clazz; } return null; } getNamespacesForEvent(o) { const ns = []; for (const $t of this.$events) { if (o instanceof $t.clazz) { ns.push($t.namespace); } } return (0, lodash_1.uniq)(ns); } /** * Remove subscriber information * * @param target * @param eventClass * @param method */ unregister(target, eventClass, method) { const namespace = EventBusMeta.toNamespace(eventClass); (0, lodash_1.remove)(this.$types, x => x.namespace === namespace && x.methodName === method && x.target === target); } /** * Add subsriber information * @param options */ register(options) { options.namespace = EventBusMeta.toNamespace(options.eventClass); const exists = this.$types.find(x => x.namespace === options.namespace && x.methodName === options.methodName && x.target === options.target); if (!exists) { this.$types.push(options); } this.registerEventClass(options.eventClass); } registerEventClass(clazz) { const ns = EventBusMeta.toNamespace(clazz); const e = { namespace: ns, clazz: clazz }; const exists = (0, lodash_1.find)(this.$events, x => x.namespace === ns); if (!exists) { this.$events.push(e); } return e; } findEvent(name) { return (0, lodash_1.find)(this.$events, e => e.namespace === name || e.clazz.name === name); } /** * Return subsriber informations for class of object o or driectly for object o * @param o */ getSubscriberInfo(o) { const list = []; for (let i = 0; i < this.$types.length; i++) { const $t = this.$types[i]; if (((0, lodash_1.isFunction)($t.target) && o instanceof $t.target) || $t.target === o) { list.push({ namespace: $t.namespace, method: $t.methodName, configuration: $t.configuration, configurationOptions: $t.configurationOptions }); } } return list; } } exports.default = EventBusMeta; //# sourceMappingURL=EventBusMeta.js.map