Skip to content

Instantly share code, notes, and snippets.

@typpo
Last active April 8, 2021 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typpo/6144044 to your computer and use it in GitHub Desktop.
Save typpo/6144044 to your computer and use it in GitHub Desktop.
/*
* A simple callback wrapper to have topics
*/
;(function(d) {
var topics = {};
d.Topic = function( id ) {
var callbacks,
method,
topic = id && topics[ id ];
if ( !topic ) {
callbacks = jQuery.Callbacks();
topic = {
publish: callbacks.fire,
subscribe: callbacks.add,
unsubscribe: callbacks.remove,
subscribe_once: function(fn) {
var wrapper_fn = function(value) {
callbacks.remove(wrapper_fn);
return fn(value);
};
callbacks.add(wrapper_fn);
}
};
if ( id ) {
topics[ id ] = topic;
}
}
return topic;
};
})(jQuery);
@nvu-github
Copy link

sdfsdfsdfs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment