Created
August 8, 2012 21:53
-
-
Save digitarald/3299122 to your computer and use it in GitHub Desktop.
µPubsub
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
| class Pubsub | |
| constructor: -> | |
| @topics = [] | |
| @scopes = [] | |
| @methods = [] | |
| acquire: (host) -> | |
| @acquired = true | |
| host.pubsub = @ | |
| @host = host | |
| @ | |
| release: -> | |
| @acquired = false | |
| @host = @host.pubsub = null | |
| @topics.length = @scopes.length = @methods.length = 0 | |
| @ | |
| sub: (topic, scope, method) -> | |
| @topics.push(topic or null) | |
| @scopes.push(scope or null) | |
| @methods.push(method or null) | |
| @ | |
| pub: (topic, a0, a1, a2, a3, a4, a5, a6, a7) -> | |
| topics = @topics | |
| scopes = @scopes | |
| i = topics.length | |
| while i-- | |
| if scopes[i] and (not topics[i] or topics[i] is topic) | |
| @scopes[@methods[i] or topic](a0, a1, a2, a3, a4, a5, a6, a7) | |
| @ | |
| unsub: (topic, scope, method) -> | |
| scopes = @scopes | |
| topics = @topics | |
| methods = @methods | |
| i = scopes.length | |
| while i-- | |
| if scopes[i] and (not topic or topics[i] is topic) and (not method or methods[i] is method) | |
| topics[i] = scopes[i] = methods[i] = null | |
| @ | |
| Pubsub.pool = new Pool(-> | |
| return new Pubsub() | |
| , 512) |
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
| // Generated by CoffeeScript 1.3.3 | |
| var Pubsub; | |
| Pubsub = (function() { | |
| function Pubsub() { | |
| this.topics = []; | |
| this.scopes = []; | |
| this.methods = []; | |
| } | |
| Pubsub.prototype.acquire = function(host) { | |
| this.acquired = true; | |
| host.pubsub = this; | |
| this.host = host; | |
| return this; | |
| }; | |
| Pubsub.prototype.release = function() { | |
| this.acquired = false; | |
| this.host = this.host.pubsub = null; | |
| this.topics.length = this.scopes.length = this.methods.length = 0; | |
| return this; | |
| }; | |
| Pubsub.prototype.sub = function(topic, scope, method) { | |
| this.topics.push(topic || null); | |
| this.scopes.push(scope || null); | |
| this.methods.push(method || null); | |
| return this; | |
| }; | |
| Pubsub.prototype.pub = function(topic, a0, a1, a2, a3, a4, a5, a6, a7) { | |
| var i, scopes, topics; | |
| topics = this.topics; | |
| scopes = this.scopes; | |
| i = topics.length; | |
| while (i--) { | |
| if (scopes[i] && (!topics[i] || topics[i] === topic)) { | |
| this.scopes[this.methods[i] || topic](a0, a1, a2, a3, a4, a5, a6, a7); | |
| } | |
| } | |
| return this; | |
| }; | |
| Pubsub.prototype.unsub = function(topic, scope, method) { | |
| var i, methods, scopes, topics; | |
| scopes = this.scopes; | |
| topics = this.topics; | |
| methods = this.methods; | |
| i = scopes.length; | |
| while (i--) { | |
| if (scopes[i] && (!topic || topics[i] === topic) && (!method || methods[i] === method)) { | |
| topics[i] = scopes[i] = methods[i] = null; | |
| } | |
| } | |
| return this; | |
| }; | |
| return Pubsub; | |
| })(); | |
| Pubsub.pool = new Pool(function() { | |
| return new Pubsub(); | |
| }, 512); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment