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.
2 lines • 5.43 kB
JavaScript
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("@angular/core")):"function"==typeof define&&define.amd?define("ngx-message-bus",["exports","@angular/core"],r):r((t="undefined"!=typeof globalThis?globalThis:t||self)["ngx-message-bus"]={},t.ng.core)}(this,(function(t,r){"use strict";var e,n=function(){function t(t,r){this.hub=t,this.subscriberId=r}return t.prototype.getHubName=function(){return this.hub.getHubName()},t.prototype.getSubscriberId=function(){return this.subscriberId.toString()},t.prototype.off=function(t){this.hub.removeListener(this.subscriptionToListener(t))},t.prototype.offBroadcast=function(){this.hub.removeBroadcastListener(this.getSubscriberId())},t.prototype.on=function(t){this.hub.addListener(this.subscriptionToListener(t))},t.prototype.onBroadcast=function(t){this.hub.addBroadcastListener({subscriberId:this.subscriberId,callback:t,groupId:null})},t.prototype.post=function(t){t.publisherId=this.subscriberId,t.timeGenerated=new Date,this.hub.post(t)},t.prototype.broadcast=function(t){var r={publisherId:this.subscriberId,payload:t,timeGenerated:new Date,groupId:null,metadata:null,recipientIds:null};this.hub.broadcast(r)},t.prototype.subscriptionToListener=function(t){return{subscriberId:this.getSubscriberId(),groupId:t.groupId,callback:t.callback}},t}();(e=t.ErrorHandlingEnum||(t.ErrorHandlingEnum={}))[e.None=0]="None",e[e.Throw=1]="Throw",e[e.Log=3]="Log";var i=function(){function r(t,r){this.errorHanndling=r,this.subscribers=[],this.listeners=[],this.broadcastListeners=[],this.hubName=t}return r.prototype.connect=function(t){this.subscribers.push(t)},r.prototype.disconnect=function(t){if(this.subscribers&&0!==this.subscribers.length){var r=this.subscribers.findIndex((function(r){return r.getSubscriberId()===t.getSubscriberId()}));if(r>=0&&this.subscribers.splice(r,1),this.isAnyListener())for(var e=-1;(e=this.listeners.findIndex((function(r){return r.subscriberId.toString()===t.getSubscriberId()})))>=0;)this.listeners.splice(e,1);if(this.isAnyBroadcastListener())for(var n=-1;(n=this.broadcastListeners.findIndex((function(r){return r.subscriberId.toString()===t.getSubscriberId()})))>=0;)this.broadcastListeners.splice(n,1)}},r.prototype.getActiveConnections=function(){return this.subscribers.length},r.prototype.addListener=function(t){this.listeners.find((function(r){return r.subscriberId.toString()===t.subscriberId.toString()&&r.groupId.toString()===t.groupId.toString()}))||this.listeners.push(t)},r.prototype.addBroadcastListener=function(t){this.broadcastListeners.find((function(r){return r.subscriberId.toString()===t.subscriberId.toString()}))||this.broadcastListeners.push(t)},r.prototype.removeListener=function(t){if(this.isAnyListener()){var r=this.listeners.findIndex((function(r){return r.subscriberId.toString()===t.subscriberId.toString()&&r.groupId.toString()===t.groupId.toString()}));r>-1&&this.listeners.splice(r,1)}},r.prototype.removeBroadcastListener=function(t){if(this.isAnyBroadcastListener()){var r=this.broadcastListeners.findIndex((function(r){return r.subscriberId.toString()===t.toString()}));r>-1&&this.broadcastListeners.splice(r,1)}},r.prototype.getHubName=function(){return this.hubName},r.prototype.post=function(r){var e=this;if(this.isAnyListener()||!r){var n=this.listeners;(!r.recipientIds||r.recipientIds.indexOf(r.publisherId.toString())<0)&&(n=n.filter((function(t){return t.subscriberId.toString()!==r.publisherId.toString()}))),r.recipientIds&&r.recipientIds.length>0&&(n=n.filter((function(t){return r.recipientIds.indexOf(t.subscriberId.toString())>=0}))),r.groupId&&r.groupId.length>0&&(n=n.filter((function(t){return t.groupId.toString()===r.groupId.toString()}))),n&&0!==n.length&&n.forEach((function(n){try{n.callback(r.payload)}catch(r){switch(e.errorHanndling){case t.ErrorHandlingEnum.Log:console.error(r);break;case t.ErrorHandlingEnum.Throw:throw r;case t.ErrorHandlingEnum.None:}}}))}},r.prototype.broadcast=function(t){this.isAnyBroadcastListener()&&this.broadcastListeners.filter((function(r){return r.subscriberId.toString()!==t.publisherId.toString()})).forEach((function(r){return r.callback(t.payload)}))},r.prototype.dispose=function(){this.subscribers=null,this.broadcastListeners=null,this.hubName=null,this.listeners=null},r.prototype.isAnyListener=function(){return this.listeners&&this.listeners.length>0},r.prototype.isAnyBroadcastListener=function(){return this.broadcastListeners&&this.broadcastListeners.length>0},r}(),s=function(){},o=function(){},u=function(){},c=function(){function r(){this.errorHandling=t.ErrorHandlingEnum.Log,this.hubs={}}return r.prototype.setLogLevel=function(t){this.errorHandling=t},r.prototype.connect=function(t,r){t=t.toString(),this.hubs[t]&&null!==this.hubs[t]||(this.hubs[t]=new i(t,this.errorHandling));var e=this.hubs[t],s=new n(e,r);return e.connect(s),s},r.prototype.disconnect=function(t){if(t&&null!=t){var r=t.getHubName().toString(),e=this.hubs[r];e&&(e.disconnect(t),e.getActiveConnections()<1&&(e.dispose(),this.hubs[r]=null))}},r}();c.ɵprov=r.ɵɵdefineInjectable({factory:function(){return new c},token:c,providedIn:"root"}),c.decorators=[{type:r.Injectable,args:[{providedIn:"root"}]}],c.ctorParameters=function(){return[]},t.Connection=n,t.Hub=i,t.Listener=s,t.Message=o,t.MessageBus=c,t.Subscription=u,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=ngx-message-bus.umd.min.js.map