UNPKG

ngx-message-bus

Version:

Messaging system for angular 11.x application. It allows communication between components and modules. Direct messages, messages by types and broadcasting.

271 lines (259 loc) 11.2 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) : typeof define === 'function' && define.amd ? define('ngx-message-bus', ['exports', '@angular/core'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['ngx-message-bus'] = {}, global.ng.core)); }(this, (function (exports, i0) { 'use strict'; var Connection = /** @class */ (function () { function Connection(hub, subscriberId) { this.hub = hub; this.subscriberId = subscriberId; } Connection.prototype.getHubName = function () { return this.hub.getHubName(); }; Connection.prototype.getSubscriberId = function () { return this.subscriberId.toString(); }; Connection.prototype.off = function (subscription) { this.hub.removeListener(this.subscriptionToListener(subscription)); }; Connection.prototype.offBroadcast = function () { this.hub.removeBroadcastListener(this.getSubscriberId()); }; Connection.prototype.on = function (subscription) { this.hub.addListener(this.subscriptionToListener(subscription)); }; Connection.prototype.onBroadcast = function (callBack) { this.hub.addBroadcastListener({ subscriberId: this.subscriberId, callback: callBack, groupId: null }); }; Connection.prototype.post = function (message) { message.publisherId = this.subscriberId; message.timeGenerated = new Date(); this.hub.post(message); }; Connection.prototype.broadcast = function (data) { var message = { publisherId: this.subscriberId, payload: data, timeGenerated: new Date(), groupId: null, metadata: null, recipientIds: null }; this.hub.broadcast(message); }; Connection.prototype.subscriptionToListener = function (subscription) { return { subscriberId: this.getSubscriberId(), groupId: subscription.groupId, callback: subscription.callback }; }; return Connection; }()); (function (ErrorHandlingEnum) { ErrorHandlingEnum[ErrorHandlingEnum["None"] = 0] = "None"; ErrorHandlingEnum[ErrorHandlingEnum["Throw"] = 1] = "Throw"; ErrorHandlingEnum[ErrorHandlingEnum["Log"] = 3] = "Log"; })(exports.ErrorHandlingEnum || (exports.ErrorHandlingEnum = {})); var Hub = /** @class */ (function () { function Hub(hubName, errorHanndling) { this.errorHanndling = errorHanndling; this.subscribers = []; this.listeners = []; this.broadcastListeners = []; this.hubName = hubName; } Hub.prototype.connect = function (connection) { this.subscribers.push(connection); }; Hub.prototype.disconnect = function (connection) { if (!this.subscribers || this.subscribers.length === 0) { return; } var subscriberIndex = this.subscribers.findIndex(function (sub) { return sub.getSubscriberId() === connection.getSubscriberId(); }); if (subscriberIndex >= 0) { this.subscribers.splice(subscriberIndex, 1); } if (this.isAnyListener()) { var listenerIndex = -1; while ((listenerIndex = this.listeners.findIndex(function (l) { return l.subscriberId.toString() === connection.getSubscriberId(); })) >= 0) { this.listeners.splice(listenerIndex, 1); } } if (this.isAnyBroadcastListener()) { var broadcastListenerIndex = -1; while ((broadcastListenerIndex = this.broadcastListeners .findIndex(function (l) { return l.subscriberId.toString() === connection.getSubscriberId(); })) >= 0) { this.broadcastListeners.splice(broadcastListenerIndex, 1); } } }; Hub.prototype.getActiveConnections = function () { return this.subscribers.length; }; Hub.prototype.addListener = function (listener) { if (!this.listeners.find(function (l) { return l.subscriberId.toString() === listener.subscriberId.toString() && l.groupId.toString() === listener.groupId.toString(); })) { this.listeners.push(listener); } }; Hub.prototype.addBroadcastListener = function (listener) { if (!this.broadcastListeners.find(function (l) { return l.subscriberId.toString() === listener.subscriberId.toString(); })) { this.broadcastListeners.push(listener); } }; Hub.prototype.removeListener = function (listener) { if (!this.isAnyListener()) { return; } var listenerToRemoveIndex = this.listeners .findIndex(function (l) { return l.subscriberId.toString() === listener.subscriberId.toString() && l.groupId.toString() === listener.groupId.toString(); }); if (listenerToRemoveIndex > -1) { this.listeners.splice(listenerToRemoveIndex, 1); } }; Hub.prototype.removeBroadcastListener = function (subscriberId) { if (!this.isAnyBroadcastListener()) { return; } var listenerToRemoveIndex = this.broadcastListeners .findIndex(function (listener) { return listener.subscriberId.toString() === subscriberId.toString(); }); if (listenerToRemoveIndex > -1) { this.broadcastListeners.splice(listenerToRemoveIndex, 1); } }; Hub.prototype.getHubName = function () { return this.hubName; }; Hub.prototype.post = function (message) { var _this = this; if (!this.isAnyListener() && message) { return; } var recipients = this.listeners; if (!message.recipientIds || message.recipientIds.indexOf(message.publisherId.toString()) < 0) { recipients = recipients.filter(function (l) { return l.subscriberId.toString() !== message.publisherId.toString(); }); } if (message.recipientIds && message.recipientIds.length > 0) { recipients = recipients.filter(function (sub) { return message.recipientIds.indexOf(sub.subscriberId.toString()) >= 0; }); } if (message.groupId && message.groupId.length > 0) { recipients = recipients.filter(function (sub) { return sub.groupId.toString() === message.groupId.toString(); }); } if (!recipients || recipients.length === 0) { return; } recipients.forEach(function (recipient) { try { recipient.callback(message.payload); } catch (e) { switch (_this.errorHanndling) { case exports.ErrorHandlingEnum.Log: console.error(e); break; case exports.ErrorHandlingEnum.Throw: throw e; case exports.ErrorHandlingEnum.None: default: break; } } }); }; Hub.prototype.broadcast = function (message) { if (!this.isAnyBroadcastListener()) { return; } this.broadcastListeners .filter(function (listener) { return listener.subscriberId.toString() !== message.publisherId.toString(); }) .forEach(function (listener) { return listener.callback(message.payload); }); }; Hub.prototype.dispose = function () { this.subscribers = null; this.broadcastListeners = null; this.hubName = null; this.listeners = null; }; Hub.prototype.isAnyListener = function () { return this.listeners && this.listeners.length > 0; }; Hub.prototype.isAnyBroadcastListener = function () { return this.broadcastListeners && this.broadcastListeners.length > 0; }; return Hub; }()); var Listener = /** @class */ (function () { function Listener() { } return Listener; }()); var Message = /** @class */ (function () { function Message() { } return Message; }()); var Subscription = /** @class */ (function () { function Subscription() { } return Subscription; }()); var MessageBus = /** @class */ (function () { function MessageBus() { this.errorHandling = exports.ErrorHandlingEnum.Log; this.hubs = {}; } MessageBus.prototype.setLogLevel = function (errorHandling) { this.errorHandling = errorHandling; }; MessageBus.prototype.connect = function (hubName, subscriberId) { hubName = hubName.toString(); if (!this.hubs[hubName] || this.hubs[hubName] === null) { this.hubs[hubName] = new Hub(hubName, this.errorHandling); } var hub = this.hubs[hubName]; var connection = new Connection(hub, subscriberId); hub.connect(connection); return connection; }; MessageBus.prototype.disconnect = function (connection) { if (!connection || connection == null) { return; } var hubName = connection.getHubName().toString(); var hub = this.hubs[hubName]; if (hub) { hub.disconnect(connection); if (hub.getActiveConnections() < 1) { hub.dispose(); this.hubs[hubName] = null; } } }; return MessageBus; }()); MessageBus.ɵprov = i0.ɵɵdefineInjectable({ factory: function MessageBus_Factory() { return new MessageBus(); }, token: MessageBus, providedIn: "root" }); MessageBus.decorators = [ { type: i0.Injectable, args: [{ providedIn: 'root' },] } ]; MessageBus.ctorParameters = function () { return []; }; /** * Generated bundle index. Do not edit. */ exports.Connection = Connection; exports.Hub = Hub; exports.Listener = Listener; exports.Message = Message; exports.MessageBus = MessageBus; exports.Subscription = Subscription; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=ngx-message-bus.umd.js.map