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

simple-event-bus

v1.0.1

Published

A simple event bus library, without dependencies

Downloads

82

Readme

Simple Event Bus

This project implements a basic but configurable event bus.

Usage

// In Node.js
const EventBus = require('simple-event-bus');

// For the browser, using a bundler
import EventBus from 'simple-event-bus';

const eventBus = new EventBus();

// subscribe to events
const removeHandler = eventBus.on('my-event', (...args) => {
  console.log(...args);
});

// trigger events
eventBus.emit('my-event', 'Hello', 'world', '!');
// => Hello world !

// unsubscribe when you are done
removeHandler();

Live Demo and its source code.

Features

  • An error in one of the handlers will not stop the propagation of the event to other handlers.
  • Simple api: adding a handler returns the function to remove it.
  • Handlers are called in the order of insertion.
  • Customizable logger (to plug your custom logging logic).
  • Customizable broker (to transform messages between events emitted and handlers).

API

new EventBus([options: Object])

Returns the event bus instance, that holds the handlers and is able to dispatch events

eventBus.on(action: string, handler: Function, [handlerId: number]): Function

Registers the handler for the given action. Optionally, a handlerId can be passed, that will be used when logging errors. It returns a function that removes the handler when called.

eventBus.emit(action: string, ...parameters: any): number

Emits an event, dispatching it to all registered handlers. It does not do anything if no handlers have been registered on that event.

eventBus.handlersCount(action: string)

Returns the number of handlers currently subscribed to the action.

[options = {}]

options.logger

Type: Object Default: console

The only mandatory method for now is error, but some additional loggings will most likely be added (log, info, debug, warn);

options.broker

Type: Function Default: (action, ...payload) => payload

A function to customize the interface between events and handlers, giving the ability for example to standardize all the messages transmitted to the handlers into a normalized format. By default, the broker just propagates the arguments that have been emitted.

Compatibility

The node entry point ("main": "index.js" in the package.json) requires Node >= 6.5 (see ES6 compatibility table for class). The browser entry point ("browser": "browser.js" in the package.json) exposes some code bundled and transpiled into ES5 for compatibility with older browsers, bundlers, minifiers, ...

Future

  • [ ] More logging: Currently, we only log errors. We could log more information, to help with troubleshooting.

  • [ ] Log level: We could add a logLevel option, to allow filtering the log messages while still keeping the default logger.

  • [ ] Test framework: A better test framework (for example Jest) should be used in order to for example have a test coverage report.

  • [ ] Examples: A showcase application could be built as a demo, showing the interest of using a custom logger and broker in a real-case example.