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

eventing-bus

v2.0.1

Published

Simple event bus

Downloads

11,785

Readme

Event Bus

Build Status

Simple event bus for your JavaScript application without any dependencies and with a low size footprint.

Installation:

Node.js:

npm install --save eventing-bus

Webpack:

yarn add eventing-bus

or for NPM:

npm install --save eventing-bus

Global event bus

By default exports in JavaScript are evaluated only once. This way we can ensure to have one global event bus without doing anything on our side.

There is also a possibility to define an event stream separately from the global bus.

Subscribing to events:

import EventBus from 'eventing-bus'

const callback = (name) => { console.log(`Hello, ${name}!`) };

EventBus.on("exampleEventName", callback);

Publishing events:

import EventBus from 'eventing-bus';

EventBus.publish("exampleEventName", "Watson");
/* After registering the subscription and publishing an event you should see
   "Hello, Watson!" printed in your console. */

More than one event bus:

By default you have only one, singleton event bus instance which holds subscriptions from all parts of your application. But nothing stands on your way to create your own, private instances (for example, for each logically distinct part of your complex app):

import EventStream from 'eventing-bus/lib/event_stream';

/* You can use EventStream both as a constructor and as a factory function. */
const privateBus = EventStream();
const newPrivateBus = new EventStream();

Those streams created by you won't share any subscriptions, nor events.

Unregistering event handlers:

If you need to unregister a subscription – a typical case would be when cleaning after your UI library – you can do it in two ways:

Call value returned by #on

After registering an event handler, a return calue will be a function unregistering the specific handler.

import EventBus from 'eventing-bus';

const subscription = EventBus.on('event', () => { console.log('test') })

EventBus.publish('event') // Console: 'test'

// This will unregister this (and only this) subscription.
subscription();

EventBus.publish('event') // No output in console

Use #unregisterCallback

In case you have no easy access to value returned by #on, you can just call #unregisterCallback. Additional benefit is that this function does not require knowing an event name. This way you can de-register every single usage of suck callback.

import EventBus from 'eventing-bus';

const callback = () => { console.log('test') }

EventBus.on('eventA', callback)
EventBus.on('eventB', callback)

EventBus.publish('eventA') // Console: 'test'
EventBus.publish('eventB') // Console: 'test'

EventBus.unregisterCallback(callback)

EventBus.publish('eventA') // No output in console
EventBus.publish('eventB') // No output in console

Unregistering subscriptions in bulk:

Since by default EventBus is a singleton instance of the bus, there may be occasions where you need to unregister all subscriptions (most notably - during testing). It can be done by calling following methods:

#unregisterAllCallbacks

Removes all event handlers.

import EventBus from 'eventing-bus';

EventBus.unregisterAllCallbacks();

#unregisterCallbacksForEvent

Remvoes all event handlers for specific event

import EventBus from 'eventing-bus';

EventBus.on('exampleEvent', () => { console.log("EXAMPLE") });
EventBus.publish("exampleEvent"); // Logs `EXAMPLE`

EventBus.unregisterCallbacksForEvent('exampleEvent');

EventBus.publish("exampleEvent"); // Empty output

Compatibility

If you want to use this library on legacy browsers (IE <= 8 etc.), you need to provide polyfills for Array.forEach and Array.filter functions. Check out e.g. es5-shim to read more.

Contributing

Feel free to report any issue or idea on the GitHub page of this project. If you report an issue, please try to provide reproducing steps or any piece of code that can reproduce the issue.