{"action":"created","issue":{"url":"https://api.github.com/repos/appendto/amplify/issues/94","labels_url":"https://api.github.com/repos/appendto/amplify/issues/94/labels{/name}","comments_url":"https://api.github.com/repos/appendto/amplify/issues/94/comments","events_url":"https://api.github.com/repos/appendto/amplify/issues/94/events","html_url":"https://github.com/appendto/amplify/pull/94","id":24464007,"number":94,"title":"Have the publish method return the answers of subscribers","user":{"login":"louisameline","id":371814,"avatar_url":"https://avatars.githubusercontent.com/u/371814?v=3","gravatar_id":"","url":"https://api.github.com/users/louisameline","html_url":"https://github.com/louisameline","followers_url":"https://api.github.com/users/louisameline/followers","following_url":"https://api.github.com/users/louisameline/following{/other_user}","gists_url":"https://api.github.com/users/louisameline/gists{/gist_id}","starred_url":"https://api.github.com/users/louisameline/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/louisameline/subscriptions","organizations_url":"https://api.github.com/users/louisameline/orgs","repos_url":"https://api.github.com/users/louisameline/repos","events_url":"https://api.github.com/users/louisameline/events{/privacy}","received_events_url":"https://api.github.com/users/louisameline/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/appendto/amplify/labels/Feature/Enhancement","name":"Feature/Enhancement","color":"bfd4f2"},{"url":"https://api.github.com/repos/appendto/amplify/labels/For%20Review","name":"For Review","color":"fbca04"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2013-12-18T01:31:49Z","updated_at":"2015-01-23T16:53:17Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/appendto/amplify/pulls/94","html_url":"https://github.com/appendto/amplify/pull/94","diff_url":"https://github.com/appendto/amplify/pull/94.diff","patch_url":"https://github.com/appendto/amplify/pull/94.patch"},"body":"Hello all,\r\n\r\nthe publish method currently returns true, or false if a subscriber returned false.\r\n\r\nI have thought of a handier behavior where the function would return an array of all answers of subscribers, like :\r\n\r\n    amplify.subscribe( 'example', function(){ return 'hey' })\r\n    amplify.subscribe( 'example', function(){ return ['ho', 8] })\r\n    amplify.subscribe( 'example', function(){ return false })\r\n    responses = amplify.publish( 'example' })\r\n\r\n    ==> responses == ['hey', ['ho', 8], false]\r\n\r\nThe goal is easier communication between two or more modules that only communicate via amplify messages, with fewer messages.\r\n\r\nWhen you publish a message, you often expect something back in return (synchronously or not), and that's why I wanted to get a promise immediately returned. Consider how this code :\r\n\r\n    //In module A :\r\n    amplify.subscribe( 'loadRequest', function(requestId){\r\n        $.ajax(\r\n            ...,\r\n            success: function(result){\r\n                //publish a second message to communicate back to module B\r\n                amplify.publish('loaded', requestId, result)\r\n            }\r\n        )\r\n    })\r\n   \r\n    //in module B\r\n    // The requestId is used to identify the request when it comes back via a pubsub message,\r\n    // so we know it's the one we were waiting for\r\n    var requestId = random();\r\n    amplify.subscribe( 'loaded', function(reqId, result){\r\n        if(reqId === requestId){\r\n            alert 'Alright : ' + result;\r\n        }\r\n    });\r\n    amplify.publish( 'loadRequest', requestId );\r\n\r\n...could be written like this :\r\n\r\n    //In module A :\r\n    amplify.subscribe( 'loadRequest', function(){\r\n        //return a promise\r\n        return $.ajax(\r\n            ...\r\n        )\r\n    })\r\n   \r\n    //in module B\r\n    var promise = amplify.publish( 'loadRequest' )[0];\r\n    promise.done(function( result ){\r\n        alert 'Alright : ' + result;\r\n    });\r\n\r\nNicer, no ? Only 1 message and no more requestId.\r\n\r\nTo preserve the ability to stop the propagation of a message by returning `false` in a subscriber, I added a helper as first parameter to the subscriber handlers. It goes like this :\r\n\r\n    amplify.subscribe('example', function(helper, arg1, arg2, ...){\r\n        //to stop propagation\r\n        helper.stopPropagation();\r\n    })\r\n\r\nI know it is a dead breaking change in the Amplify API, but I thought it was worth proposing anyway because I think it is very very nice. The code of my app looks way better and has shrunk. I won't go back :) But tell me if you see any con to this.\r\n\r\nIf you considered implementing this, you'd probably want to create a flag in the Amplify config for people who would like to keep the old behavior, for the sake of backward compatibility. I have not included that in my pull request however.\r\n\r\nThank you guys for the good work on Amplify anyway !"},"comment":{"url":"https://api.github.com/repos/appendto/amplify/issues/comments/71224412","html_url":"https://github.com/appendto/amplify/pull/94#issuecomment-71224412","issue_url":"https://api.github.com/repos/appendto/amplify/issues/94","id":71224412,"user":{"login":"dotnetchris","id":116135,"avatar_url":"https://avatars.githubusercontent.com/u/116135?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnetchris","html_url":"https://github.com/dotnetchris","followers_url":"https://api.github.com/users/dotnetchris/followers","following_url":"https://api.github.com/users/dotnetchris/following{/other_user}","gists_url":"https://api.github.com/users/dotnetchris/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnetchris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnetchris/subscriptions","organizations_url":"https://api.github.com/users/dotnetchris/orgs","repos_url":"https://api.github.com/users/dotnetchris/repos","events_url":"https://api.github.com/users/dotnetchris/events{/privacy}","received_events_url":"https://api.github.com/users/dotnetchris/received_events","type":"User","site_admin":false},"created_at":"2015-01-23T16:53:17Z","updated_at":"2015-01-23T16:53:17Z","body":"This PR should rejected. Publishers should have no knowledge of subscribers."}}