zpubsub
Version:
A javascript implementation of a publish/subscribe pattern.
1 lines • 5.6 kB
JavaScript
(function webpackUniversalModuleDefinition(root,factory){if(typeof exports==="object"&&typeof module==="object")module.exports=factory();else if(typeof define==="function"&&define.amd)define([],factory);else if(typeof exports==="object")exports["zpubsub"]=factory();else root["zpubsub"]=factory()})(this,function(){return function(modules){var installedModules={};function __webpack_require__(moduleId){if(installedModules[moduleId]){return installedModules[moduleId].exports}var module=installedModules[moduleId]={i:moduleId,l:false,exports:{}};modules[moduleId].call(module.exports,module,module.exports,__webpack_require__);module.l=true;return module.exports}__webpack_require__.m=modules;__webpack_require__.c=installedModules;__webpack_require__.d=function(exports,name,getter){if(!__webpack_require__.o(exports,name)){Object.defineProperty(exports,name,{configurable:false,enumerable:true,get:getter})}};__webpack_require__.n=function(module){var getter=module&&module.__esModule?function getDefault(){return module["default"]}:function getModuleExports(){return module};__webpack_require__.d(getter,"a",getter);return getter};__webpack_require__.o=function(object,property){return Object.prototype.hasOwnProperty.call(object,property)};__webpack_require__.p="";return __webpack_require__(__webpack_require__.s=1)}([function(module,exports,__webpack_require__){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var ZPubSubEventObject=function(){function ZPubSubEventObject(owner,callback){this.owner=owner;this.callback=callback}return ZPubSubEventObject}();exports.ZPubSubEventObject=ZPubSubEventObject},function(module,exports,__webpack_require__){"use strict";function __export(m){for(var p in m)if(!exports.hasOwnProperty(p))exports[p]=m[p]}Object.defineProperty(exports,"__esModule",{value:true});__export(__webpack_require__(2));__export(__webpack_require__(0))},function(module,exports,__webpack_require__){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var zpubsub_event_1=__webpack_require__(0);var ZPubSub=function(){function ZPubSub(){this.subMap={}}ZPubSub.prototype.publish=function(topic){var args=[];for(var _i=1;_i<arguments.length;_i++){args[_i-1]=arguments[_i]}if(!topic){throw new Error("The topic to publish was not supplied.")}var callbacks=this._getSubscription(topic);var results=[];for(var _a=0,callbacks_1=callbacks;_a<callbacks_1.length;_a++){var store=callbacks_1[_a];results.push(store.callback.apply(store.owner,args))}return results};ZPubSub.prototype.yell=function(topic){var args=[];for(var _i=1;_i<arguments.length;_i++){args[_i-1]=arguments[_i]}if(!topic){throw new Error("The topic to yell was not supplied.")}var argsToApply=[topic].concat(args);var results=this.publish.apply(this,argsToApply);var definedElements=results.filter(function(x){return x!==null&&x!==undefined});return definedElements.length===0?null:definedElements[0]};ZPubSub.prototype.subscribe=function(topic,owner,callback){if(!topic){throw new Error("The topic to subscribe to was not supplied.")}if(!owner){throw new Error("The owner for the subscription was not supplied.")}if(typeof callback!=="function"){var msg=["The callback function for the topic was not defined or not a function.","Did you forget to pass the owner?"].join(" ");throw new Error(msg)}var store=this._getSubscription(topic);var event=new zpubsub_event_1.ZPubSubEventObject(owner,callback);store.push(event);return event};ZPubSub.prototype.unsubscribe=function(topic,owner,callback){if(!topic){throw new Error("The topic to unsubscribe from was not supplied.")}if(!owner){throw new Error("The owner that owns the topic to unsubscribe from was not supplied.")}if(typeof callback!=="function"){throw new Error("The specific callback for the topic and owner pair to unsubscribe from was not supplied.")}var callbacks=this._getSubscription(topic);var modified=false;for(var i=callbacks.length-1;i>=0;i-=1){var current=callbacks[i];if(current.owner===owner&¤t.callback===callback){callbacks.splice(i,1);modified=true}}return modified};ZPubSub.prototype.unsubscribeAll=function(owner){if(!owner){throw new Error("The owner to remove all subscriptions for was not supplied.")}var modified=false;for(var property in this.subMap){var value=this.subMap[property];for(var index=value.length-1;index>=0;index=index-1){var currentCallback=value[index];if(currentCallback.owner===owner){value.splice(index,1);modified=true}}}return modified};ZPubSub.prototype.register=function(topic){if(!topic){throw new Error("The topic to register was not supplied.")}var publishName="publish"+topic;var yellName="yell"+topic;var subscribeName="subscribe"+topic;var unsubscribeName="unsubscribe"+topic;this[publishName]=this.publish.bind(this,topic);this[yellName]=this.yell.bind(this,topic);this[subscribeName]=this.subscribe.bind(this,topic);this[unsubscribeName]=this.unsubscribe.bind(this,topic)};ZPubSub.prototype.deregister=function(topic){if(!topic){throw new Error("The topic to deregister was not supplied.")}var publishName="publish"+topic;var yellName="yell"+topic;var subscribeName="subscribe"+topic;var unsubscribeName="unsubscribe"+topic;if(this.hasOwnProperty(publishName)){delete this[publishName]}if(this.hasOwnProperty(yellName)){delete this[yellName]}if(this.hasOwnProperty(subscribeName)){delete this[subscribeName]}if(this.hasOwnProperty(unsubscribeName)){delete this[unsubscribeName]}};ZPubSub.prototype._getSubscription=function(topic){if(!this.subMap.hasOwnProperty(topic)){this.subMap[topic]=[]}return this.subMap[topic]};return ZPubSub}();exports.ZPubSub=ZPubSub}])});