Pub/sub is a very effective javascript pattern in order to avoid direct interaction between code modules. However its one way mode may require additional hard-coded pub/sub if you expect some reply… I suggest improving javascript pub/sub pattern in order to provide bidirectionality.
This implementation creates a temporary, silent reverse topic in order to avoid hard-coding a new pub/sub couple. You can find details here: https://www.medway.fr/bloog/javascript-bidirectional-pub-sub.php
The "events" object functions names are similar to those found in any standard pub/sub implementation.
Publishes with an optional callback function as a third argument. If such a callback exists, if will handle a potential response:
events.publish("myTopic", data, function(response){
// some code handling response...
});
If not, the pub/sub pattern will work as usual...
events.publish("myTopic", data);
The submitter can "reply" in case the publisher provided a callback function:
var mySubmit = events.submit("myTopic", function(data, reply){
// Some code generating a response...
reply(response);
});
If not, the pub/sub pattern will work as usual...
var mySubmit = events.submit("myTopic", function(data){
...
};
Removes the callback:
mySubmit.unsubmit();
If a callback function is provided when publishing, you should consider it only as a one-to-one communication, because in case of several subscribers, you may have to manage several responses...
- We started from Andy Osmany pub/sub vanilla JS implementation