PubSub Message Maintainability - sjonesyodle/Cluster GitHub Wiki
In large applications, it can be easy to loose track of messages and their related modules.
While using Cluster in debug mode can help by printing a list of messages and attached modules in the console, you can also setup your application with an array of messages.
var myApp = Cluster();
myApp.enhance({
messages: ["doThis", "doThat", /*...*/]
});
The .enhace()
method listens for a property named messages
and converts the Array to an Object Literal. Doing this will set the messages for use in each Module in the Cluster:
myApp.collect({
init: function(){
var messages = this.cluster.messages;
this._sub(messages.doThis, function( text ){
console.log( text ); // Will log 'Yo!'
});
}
}).collect({
init: function(){
var messages = this.cluster.messages;
this._pub(messages.doThis, "Yo!");
}
}).start({debug: true}); // Start and debug...
This is done purely for organizational purposes, keeping all variables at the top of the JS document where they are easy to read. Cluster does not parse the messages
Array in any way, except to convert it to an Object Literal.