-
-
Save karolk/3761754 to your computer and use it in GitHub Desktop.
pubsub implementation with data passing and sticky events
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
/** | |
* Pubsub event management | |
* Dependencies: none | |
* Autor: Karol Kowalski | |
* Repo: https://gist.github.com/3761754 | |
*/ | |
(function(host) { | |
var events = {}, | |
event_exists = function(event_name) { | |
return !!events[event_name]; | |
}, | |
for_all_subscribers = function(event_name, fn) { | |
if (event_exists(event_name)) { | |
var objs = events[event_name]; | |
for (var i=0, l=objs.length; i<l; i++) { | |
fn(objs[i], objs, i); | |
} | |
} | |
}, | |
create_if_none = function(event_name, cfg) { | |
event_exists(event_name) || (events[event_name] = []); | |
cfg && cfg.sticky && (events[event_name].sticky = true); | |
cfg && cfg.data && (events[event_name].data = cfg.data); | |
}, | |
dispatch_event = function(event_name, cfg) { | |
cfg && cfg.sticky && create_if_none(event_name, cfg); | |
for_all_subscribers(event_name, | |
function(subscriber) { | |
trigger(event_name, subscriber, (cfg && cfg.data)); | |
}); | |
}, | |
bind = function(event_name, fn) { | |
create_if_none(event_name) | |
events[event_name].push(fn); | |
//if event is sticky it will be dispatch every time object subscribes to this | |
//just for this object | |
events[event_name].sticky && trigger(event_name, fn, events[event_name].data); | |
}, | |
bind_once = function(event_name, fn) { | |
var already_subscribed = false; | |
for_all_subscribers(event_name, | |
function(subscriber, all_subscribers, index) { | |
if (subscriber === fn) { | |
already_subscribed = true; | |
} | |
}); | |
already_subscribed || bind(event_name, fn); | |
}, | |
trigger = function(event_name, fn, args) { | |
var apply_args = [{type:event_name}].concat(args); | |
fn.apply(host, apply_args); | |
}, | |
unbind = function(event_name, fn) { | |
for_all_subscribers(event_name, | |
function(subscriber, all_subscribers, index) { | |
if (subscriber === fn) { | |
all_subscribers.splice(index,1); | |
} | |
}); | |
}; | |
host.E = { | |
publish: dispatch_event, | |
pub: dispatch_event, | |
unsubscribe: unbind, | |
unsub: unbind, | |
subscribe: bind, | |
subscribeUnique: bind_once, | |
sub: bind, | |
subUnique: bind_once, | |
//peek into events | |
__events__: events | |
}; | |
})(self); |
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
(function(d){var c={},g=function(a,b){if(c[a])for(var e=c[a],f=0,d=e.length;f<d;f++)b(e[f],e,f)},i=function(a,b){c[a]||(c[a]=[]);b&&b.sticky&&(c[a].sticky=!0);b&&b.data&&(c[a].data=b.data)},j=function(a,b){b&&b.sticky&&i(a,b);g(a,function(e){var c=[{type:a}].concat(b&&b.data);e.apply(d,c)})},h=function(a,b){i(a);c[a].push(b);if(c[a].sticky){var e=[{type:a}].concat(c[a].data);b.apply(d,e)}},k=function(a,b){var c=!1;g(a,function(a){a===b&&(c=!0)});c||h(a,b)},l=function(a,b){g(a,function(a,c,d){a=== | |
b&&c.splice(d,1)})};d.E={publish:j,pub:j,unsubscribe:l,unsub:l,subscribe:h,subscribeUnique:k,sub:h,subUnique:k,__events__:c}})(self); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment