Skip to content

Instantly share code, notes, and snippets.

@rmehner
Created March 2, 2012 09:37
Show Gist options
  • Save rmehner/1957308 to your computer and use it in GitHub Desktop.
Save rmehner/1957308 to your computer and use it in GitHub Desktop.
diff --git a/js/vendor/pubsub.js b/js/vendor/pubsub.js
index ed8ecc4..b7b81cf 100644
--- a/js/vendor/pubsub.js
+++ b/js/vendor/pubsub.js
@@ -25,61 +25,61 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/** section: PubSub
* PubSubJS is a dependency free library for doing ['publish/subscribe'](http://en.wikipedia.org/wiki/Publish/subscribe)
* messaging in JavaScript.
- *
- * In order to not have surprising behaviour where the execution chain generates more than one message,
- * publication of messages with PubSub are done asyncronously (this also helps keep your code responsive, by
+ *
+ * In order to not have surprising behaviour where the execution chain generates more than one message,
+ * publication of messages with PubSub are done asyncronously (this also helps keep your code responsive, by
* dividing work into smaller chunkcs, allowing the event loop to do it's business).
*
* If you're feeling adventurous, you can also use syncronous message publication, which can lead to some very
* confusing conditions, when one message triggers publication of another message in the same execution chain.
* Don't say I didn't warn you.
- *
+ *
* ##### Examples
- *
+ *
* // create a function to receive the message
* var mySubscriber = function( msg, data ){
* console.log( msg, data );
* };
- *
+ *
* // add the function to the list of subscribers to a particular message
* // we're keeping the returned token, in order to be able to unsubscribe from the message later on
* var token = PubSub.subscribe( 'MY MESSAGE', mySubscriber );
*
* // publish a message asyncronously
* PubSub.publish( 'MY MESSAGE', 'hello world!' );
- *
+ *
* // publish a message syncronously, which is faster by orders of magnitude, but will get confusing
* // when one message triggers new messages in the same execution chain
* // USE WITH CATTION, HERE BE DRAGONS!!!
* PubSub.publishSync( 'MY MESSAGE', 'hello world!' );
- *
+ *
* // unsubscribe from further messages, using setTimeout to allow for easy pasting of this code into an example :-)
* setTimeout(function(){
* PubSub.unsubscribe( token );
* }, 0)
-**/
+**/
var PubSub = {};
(function(p){
"use strict";
-
+
p.version = "1.0.1";
-
+
var messages = {};
var lastUid = -1;
-
+
var publish = function( message, data, sync ){
// if there are no subscribers to this message, just return here
if ( !messages.hasOwnProperty( message ) ){
return false;
}
-
+
var deliverMessage = function(){
var subscribers = messages[message];
var throwException = function(e){
return function(){
throw e;
};
- };
+ };
for ( var i = 0, j = subscribers.length; i < j; i++ ){
try {
subscribers[i].func( message, data );
@@ -88,7 +88,7 @@ var PubSub = {};
}
}
};
-
+
if ( sync === true ){
deliverMessage();
} else {
@@ -107,7 +107,7 @@ var PubSub = {};
p.publish = function( message, data ){
return publish( message, data, false );
};
-
+
/**
* PubSub.publishSync( message[, data] ) -> Boolean
* - message (String): The message to publish
@@ -130,12 +130,12 @@ var PubSub = {};
if ( !messages.hasOwnProperty( message ) ){
messages[message] = [];
}
-
+
// forcing token as String, to allow for future expansions without breaking usage
// and allow for easy use as key names for the 'messages' object
var token = (++lastUid).toString();
messages[message].push( { token : token, func : func } );
-
+
// return token for unsubscribing
return token;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment