Monologue.js add-on that allows seamless bridging to postal.js.
First things first - monopost has three dependencies: underscore, postal and monologue.
If you are using it in the browser, simply include monopost.js (it can act as a standard include or an AMD module). If you're not using an AMD loader, you will need to include monopost after its dependencies. If you are using it in node, the monopost.js module returns a function which takes underscore, monologue and postal as arguments. This function must be invoked before monopost will take effect:
var _ = require'underscore';var postal = require'postal';var Monologue = require'monologue.js';var bridge = require'monopost';bridge_ Monologue postal;
Let's pretend we have a simple constructor function that is taking advantage of monologue's behavior:
varthisname = name;;thisemit"work.done" who: thisname ;;MonologuemixinWorker;var worker = "Bugs";
Assuming monopost has been included in the project, the
goPostal method (yes, we went there) will now be available on any object that has monologue mixed into it. The
goPostal method has 4 different ways it can be called:
// 1.) with no arguments. This bridges ALL events to the default postal channel "/"workergoPostal;// 2.) with a channel name. This bridges all events to the specified channel in postalworkergoPostal"SomeChannel";// 3.) with a channel & topic binding. This bridges only events matching the specified// topic binding, and publishes them to postal on the channel providedworkergoPostal"SomeChannel" "work.#"; // will match work.done, work.started, work.almost.done, etc.// 4.) with an object literal providing key/value pairs of topic binding/channelworkergoPostal"match.stuff.like.#" : "ThisChannelYo""secret.sauce.*" : "SeeecretChannel""another.*.topic" : "YayMoarChannelsChannel";
Any events that have been bridged will also be published to postal's message bus when the event is emitted.
People often use the term "pub/sub" to describe eventing in general. However, there is an observer pattern based 'pub/sub' - where listeners must have a direct reference to what they are subscribing - and a mediator pattern 'pub/sub' - where subscribers and publishers use a 3rd party 'broker' to manage the list of subscribers, and the routing of messages to the correct listeners, etc. Both patterns are important, and have different implications. For objects that truly should have a direct reference to one another, the 'observer' pattern makes sense (and a mediated approach might truly be overkill in those instances). This is where monologue comes into play - making objects event emitters for other subscribers to subscribe directly. However, it's often the case that other objects (in separate modules) in the application need to know about an event that has occurred - but those objects would have to take a direct dependency on the publishing object only to get a handle to subscribe. This is where postal comes into play - it acts as a mediator so that your modules do not have to become tightly coupled simply to listen to events that are important beyond the modules that publish them. Using both patterns in an application is a very common scenario, and you typically see boilerplate code where a subscriber is added to an event emitter in order to bridge the event to the bus:
// example of the kind of boilerplate cruft requred to manually bridge most emitters to a bus// uuuuugly. Too much of this in an app and you'll want to scratch your eyes out...someObjecton"someEvent"buspublishchannel: "someChannel"topic: "someEvent"data:arg1: arg1arg2: arg2;
The point of monopost is to make the above boilerplate disappear. Since monologue and postal share the same envelope structure (except that postal's envelope has the additional
channel member), bridging the two is quite simple (as seen in the examples further above).
You've probably noticed that monologue uses a 'single object payload' style for passing arguments to subscribers, as opposed to the '0-to-n arguments' approach most event emitters take. See monologue's README for more discussion behind the reason(s) for this approach.