Hello all,
the publish method currently returns true, or false if a subscriber returned false.
I'm thinking of a handier behavior where the function would return an array of all answers of subscribers, like :
amplify.subscribe( 'example', function(){ return 'hey' })
amplify.subscribe( 'example', function(){ return ['ho', 8] })
amplify.subscribe( 'example', function(){ return false })
responses = amplify.publish( 'example' })
==> responses == ['hey', ['ho', 8], false]
The point is easier communication between two or more modules that only communicate via amplify. Consider how this code :
//In module A :
amplify.subscribe( 'loadRequest', function(requestId){
$.ajax(
...,
success: function(result){
amplify.publish('loaded', requestId, result)
}
)
})
//in module B
//the requestId is used to identify the request when it comes back via a pubsub message
var requestId = random();
amplify.subscribe( 'loaded', function(reqId, result){
if(reqId === requestId){
alert 'Alright : ' + result;
}
});
amplify.publish( 'loadRequest', requestId );
...could be written like this :
//In module A :
amplify.subscribe( 'loadRequest', function(requestId){
return $.ajax(
...
)
})
//in module B
var responses = amplify.publish( 'loadRequest', requestId );
var promise = responses[0];
promise.done(function( result ){
alert 'Alright : ' + result;
});
Nicer, no ?
To preserve the ability to stop the propagation of a message by returning `false` in a subscriber, we'd have to change the way the subscriber handlers work too. I suggest they work like this :
amplify.subscribe('example', function(helper, argumentsArray){
//to stop propagation
helper.stopPropagation();
})
I know this would be a dead breaking change in the amplify API, but I thought it was worth asking anyway because I think it would be very very nice. Let me know what you think.
Cheers