Created
November 12, 2011 06:39
-
-
Save milesplit/1360146 to your computer and use it in GitHub Desktop.
PubSub with zepto... few fancy features like priorities and owner modules
This file contains 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
(function($) { | |
var Subscription = function(t, c, o, p) { | |
this.topic = t; | |
this.callback = c; | |
this.owner = o; | |
this.priority = p || 10; | |
this.signature = function(){ | |
return [t, c]; | |
}; | |
return this; | |
}, | |
subscribers = {} | |
filterArray = function(a, c){ | |
var i = 0; | |
while (i < a.length) { | |
if (c(a[i])) { | |
a.splice(i, 1); | |
} else { i++; } | |
} | |
}; | |
// Publish a pubsub event | |
// topic = string | |
// args (optional) = object, attached data | |
// owner (optional) = string, only publishes to subscribers from that owner | |
$.publish = function(topic, args, owner){ | |
if (subscribers['*']) { | |
subscribers['*'].forEach(function(s){ | |
s.callback({ topic:topic, owner:owner, args:args }); | |
}); | |
} | |
if (subscribers[topic]) { | |
subscribers[topic].forEach(function(s){ | |
(owner && owner == s.owner) ? s.callback(args) : (!owner) && s.callback(args); | |
}); | |
} else { | |
return null; | |
} | |
}; | |
// Subscribe to a pub sub event | |
// topic = string or array (can subscribe to multiple events with same callback) | |
// callback = function | |
// o (optional) = string, owner | |
// p (optional) = integer greater than 0, priority | |
$.subscribe = function(topic, c, o, p){ | |
var handle=[]; | |
topic = (typeof topic == 'string') ? [topic] : topic; | |
p = (o > 0) ? o : p; | |
o = (o > 0) ? null : o; | |
topic.forEach(function(t) { | |
subscribers[t] = subscribers[t] || []; | |
var s = new Subscription(t, c, o, p); | |
subscribers[t].push(s); | |
handle.push(s.signature()); | |
subscribers[t].sort(function(a, b){ | |
return (b.priority - a.priority); | |
}); | |
}); | |
return (handle.length==1) ? handle[0] : handle; | |
}; | |
$.unsubscribe = function(topic, owner) { | |
if (!topic && !owner) { | |
// nuclear option | |
subscribers = {}; | |
} else if (topic && !owner) { | |
if (typeof topic == 'string') { | |
// kill all subscribers of a certain topic | |
subscribers[topic] = []; | |
} else { | |
// kill subscriber with a certain signature | |
filterArray(subscribers[t], function(s){ | |
return (s.callback = topic[1]); | |
}); | |
} | |
} else if (topic && owner) { | |
// kill all with a certain topic owned by a certain module | |
filterArray(subscribers[topic], function(s){ | |
return (s.owner == owner); | |
}); | |
} else if (!topic && owner) { | |
// kill all owned by a certain module | |
for (t in subscribers) { | |
filterArray(subscribers[t], function(s){ | |
return (s.owner == owner); | |
}); | |
} | |
} | |
}; | |
// Subscribers | |
// topic = string | |
// owner (optional) = string, only publishes to subscribers from that owner | |
$.subscribers = function(topic, owner){ | |
var out = [], | |
makeArray = function(t){ | |
subscribers[t] && subscribers[t].forEach(function(s){ | |
(owner && owner == s.owner) ? out.push(s) : (!owner) && out.push(s); | |
}); | |
}; | |
(topic) ? makeArray(topic) : (function(){ | |
for (t in subscribers) { | |
makeArray(t); | |
} | |
})(); | |
return out; | |
}; | |
})(Zepto); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment