dtang-pubsub
Version:
A small PubSub library under the PS constructor function.
4 lines • 1.09 kB
JavaScript
/*! PubSub-Library - v2.0.0 - 2013-02-06
* https://github.com/skaterdav85/PubSub-Library.git
* Copyright (c) 2013 ; Licensed */
(function(e,t){var n=function(){this.topicList={},this.topicLookup={},this.subID=0};n.prototype={constructor:n,publish:function(e){var n,r,i=null;if(typeof e!="string")throw new Error("Topic must be a string");arguments[1]!==t&&(i=arguments[1]);if(this.topicList[e])for(n in this.topicList[e])this.topicList[e].hasOwnProperty(n)&&(r=this.topicList[e][n],r.cb.call(r.context,i))},subscribe:function(e,t,n){var r;if(typeof e!="string"&&typeof t!="function")throw new Error("Subscribe arguments of the wrong type.");return this.topicList[e]||(this.topicList[e]={}),this.subID+=1,r=n||this,this.topicList[e][this.subID]={cb:t,context:r},this.topicLookup[this.subID]=e,this.subID},unsubscribe:function(e){var t;if(typeof e!="number")throw new Error("Subscription ID of the wrong type.");return this.topicLookup.hasOwnProperty(e)?(t=this.topicLookup[e],delete this.topicList[t][e],delete this.topicLookup[e],!0):!1}},e.PS=n})(typeof exports=="undefined"?window:exports);