npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

monopost

v0.1.2

Published

Monologue.js add-on that allows seamless bridging to postal.js.

Downloads

43

Readme

monopost.js - v0.1.0

What is it?

monopost is an adapter that bridges monologue.js - an event emitter utility - to postal.js - a JavaScript message bus. In a nutshell, monologue is to the observer pattern, as postal is to the mediator pattern. monopost adds an additional method to monologue's prototype that makes bridging events to the message bus very simple.

How do I use it?

Including it in your project

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);

How to bridge events

Let's pretend we have a simple constructor function that is taking advantage of monologue's behavior:

var Worker = function(name) {
    this.name = name;
};
Worker.prototype.doWork = function() {
    this.emit("work.done", { who: this.name });
};
Monologue.mixin(Worker);

var worker = new 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 "/"
worker.goPostal();

// 2.) with a channel name. This bridges all events to the specified channel in postal
worker.goPostal("SomeChannel");

// 3.) with a channel & topic binding. This bridges only events matching the specified
//     topic binding, and publishes them to postal on the channel provided
worker.goPostal("SomeChannel", "work.#"); // will match work.done, work.started, work.almost.done, etc.

// 4.) with an object literal providing key/value pairs of topic binding/channel
worker.goPostal({
    "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.

Why would I use it?

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...
someObject.on("someEvent", function(arg1, arg2){
    bus.publish({
        channel: "someChannel",
        topic: "someEvent",
        data: {
            arg1: arg1,
            arg2: 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.