Last active
December 17, 2015 14:38
-
-
Save yeco/5625444 to your computer and use it in GitHub Desktop.
TinyPubSub.js Library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var EventBus = function () { | |
| this.NewsPaperList = {}; | |
| this.OrderList = []; | |
| }; | |
| EventBus.prototype = { | |
| subscribe: function (newsPaper, address) { | |
| if ((typeof newsPaper !== "string") || (typeof address !== "function")) { | |
| return -1; | |
| } | |
| var AList = this.NewsPaperList[newsPaper]; | |
| if (typeof AList !== "object") { | |
| AList = this.NewsPaperList[newsPaper] = []; | |
| } | |
| var customer = AList.push(address) - 1 ; | |
| return this.OrderList.push({ | |
| newsPaper: newsPaper, | |
| customer: customer | |
| }) - 1; | |
| }, | |
| unsubscribe: function (orderId) { | |
| var O = this.OrderList[orderId]; | |
| if (O !== undefined) { | |
| delete this.NewsPaperList[O.newsPaper][O.customer]; | |
| delete O; | |
| } | |
| }, | |
| publish: function (newsPaper, content) { | |
| var AddressList = this.NewsPaperList[newsPaper]; | |
| if (typeof AddressList !== "undefined") { | |
| var l = AddressList.length; | |
| for (var i = 0; i < l; i++) { | |
| if (typeof AddressList[i] === "function") { | |
| AddressList[i].call(this, content); | |
| } | |
| } | |
| } | |
| } | |
| }; | |
| //// ----------------------- Use Of TinyPubSub.js -------------------///// | |
| var E = new EventBus(); | |
| var x = E.subscribe("resize", function (val) { | |
| log("Method1 :resize : called with value" + val); | |
| }); | |
| E.publish("resize", "1"); | |
| E.unsubscribe(x); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment