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

arbiter-subpub

v1.1.0

Published

Arbiter.js is a light-weight, library-agnostic javascript implementation of the pub/sub pattern

Downloads

5

Readme

Arbiter-Node

ArbiterJS was created by Matt Kruse. I take no credit for this work.

Arbiter.js is a light-weight, library-agnostic JavaScript implementation of the pub/sub pattern, written by Matt Kruse. It allows objects on your page to be de-coupled, and communicate with each other through messages. This leads to a cleaner, more easily understood design, and easier maintenance.

For example, if the user changes a value on one part of the page, it can publish a message saying which action was taken. Other parts of the page can subscribe to that action, and do something when it happens. But the first interaction doesn't have to know anything about the second. It just announces what happened, and anyone who cares can act on it.

##Example var Arbiter = require('arbiter-subpub');

// I want to do something when new mail arrives
Arbiter.subscribe("email/new", mail_arrival);

// Listens for any email related event
Arbiter.subscribe("email/*", mail_process);

// Listens for any published message for logging purposes
Arbiter.subscribe("*", console.log.bind(console));

// This code should be called ed by the system that detects incoming email
Arbiter.publish("email/new", {from:"Bob"});

Documentation

This is a complete description of the Arbiter API.

Message Format

At the heart of Abriter is the message format. A message can be any format but cannot contain whitespave, commas, or asterisk. Each of these have special meaning for subscribers. A structure like a/b/c is recommended by convention to allow messages to be categorized.

A subscriber can end their message with an asterisk (*). This message format will match any published message that has the same subscribers message format until the asterisk. For example email/* will match email/new and email/send. This can be useful for handling all messages of a certain component or category. If you take care when naming your messages, using wildcards can help avoid subscribing to multiple individual messages and needing to update as new messages are added.

Subscribers can also subscribe to multiple evens by separating them by a comma and a space.

Arbiter.subscribe( 'component/msg, component/msg2', function() { } );

Method Summary

Arbiter.publish( msg [, data [, options] ] )

Publishes data to all subscribers of the msg. This method returns true on success, false if any subscriber has thrown a JavaScript exception.

Arbiter.publish( 'component/msg', {"data": "value"} );
Options

Options is a JavaScript object that accepts these options.

cancelable: By default, subscribers can return "false" to prevent subsequent subscribers from receiving the message. By passing cancelable:false in the options, the publisher can prevent canceling. persist: By default, subscribers only receive notifications about messages sent after they subscribe. But for some events, like "system initalized" that may fire only once, it can be useful to allow subscribers to that message to get fired if the message has already been sent. If the publishers wants subscribers to be notified of this message even if they subscribe later, setting the persist flag will do that. async: By default, subscribers are notified and their functions are run synchronously, so the publish() function doesn't return until all subscribers have finished. If you wish to notify the subscribers but return from the publish() call before the subscriber functions execute, use asynchronous mode. Note: Subscribers cannot cancel asynchonous messages, because the subscribers are executed independently using setTimeout()

//The default options
Arbiter.publish( 'component/msg', "ready", { canceleable: true, persist: false, async: false } );

Arbiter.subscribe( msg, [, options, [context] ], func )

Executes func when a message matches the message format msg. msg can also be an array of messages specified to the Message Format.

Options

Options is a JavaScript object that accepts these options.

async: A subscriber can be set to execute asynchronously, even if the message wasn't published as async. If a subscriber knows that it will do some heavy calculations, for example, it can force itself to be async so it won't interfere with the execution of other subscribers. persist: If a message was persisted, a subscriber will be notified of it even if was sent in the past. If your subscriber is not interested in any past messages that may have been persisted, you can force them to be ignored. priority: By default, all subscribers have a priority of 0. Higher values get higher priority and are executed first. Negative values are allowed.

//The default options
Arbiter.publish( 'component/msg', "ready", { canceleable: true, persist: false, async: false, priority: 0 } );
context

The value of this inside func can be changed by using the context.

func( published_data, message, subscriber_data)
  • published_data: Data that the publisher provided
  • message: The message that triggered the notification (useful if a handler is used for multiple messages)
  • subscriber_data: An initally empty object that will be passed between all subscribers.

Arbiter.unsubscribe( subscription_id )

Unsubscribing simply sets a flag which prevents the subscriber from executing, in case you want to re-subscribe later.

var subscription_id = Arbiter.subscribe( 'msg', function(){} );
Arbiter.unsubscribe( subscription_id );

Arbiter.resubscribe( subscription_id )

After unsubscribing, you can later re-subscribe to begin receiving messages again.

var subscription_id = Arbiter.subscribe( 'msg', function(){} );
Arbiter.unsubscribe( subscription_id );
Arbiter.resubscribe( subscription_id );

Arbiter.create()

This creates a separate Arbiter instance. If you want to have different message handlers entirely, for example, this will allow for that. Messages sent to the new object will not be shared with the default Arbiter object. You may create as many arbiters as you wish, and they will all operate independently.

var MyController = Arbiter.create()

Arbiter.onError()

This function sets the error handling function. When a subscriber throws a javascript exception this function executes. The default implementation warns the user. The this value is the same as the subscriber that threw.

Arbiter.onError( function( err, data, msg, internal_data ) { // custom notification. } );

License

This work is in the public domain and may be used in any way, for any purpose, without restriction.