Last active
April 8, 2021 02:27
-
-
Save typpo/6144044 to your computer and use it in GitHub Desktop.
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
/* | |
* 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sdfsdfsdfs