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

protected-event-emitter

v1.0.0

Published

An event emitter that can only be called through the protected interface.

Downloads

2

Readme

protected-event-emitter

How is this event library different?

Any code can listen to any events but not any code can emit any event. This library allows you to control what code is allowed to emit what events.

Installation

$ npm install protected-event-emitter

API

emitter ( namespace )

Create an event emitter that will emit events on the specified namespace. The namespace must be unique.

Parameters

  • namespace [optional, string] - The namespace to emit events on.

Returns an emit function that takes two parameters: 1) the type of event being emitted, and 2) the data for the event.

const event = require('protected-event-emmitter');
const emit = event.emitter('my-emitter');

emit('ready', 'Good to go');    // type is "ready", data is "Good to go"

listenerCount ( namespace [, event ] )

Count how many item are listening the the namespace, optionally limiting results to a specific event type.

Parameters

  • namespace [required, string] - The namespace to count events on.
  • event [optional, string] - The type of event to filter count to.

Returns a number.

namespace ( namespace )

Get an object that has shortcut functions to the off, on, and once event handler functions for this namespace.

Parameters

  • namespace [required, string] - The namespace to get the shortcut functions for.

Returns an object with event handler functions that are scoped to the namespace provided.

const event = require('protected-event-emmitter');

function readyHandler(data) {
    console.log('my-emiter is ready with data: ' + data);
}

const listeners = event.namespace('my-emitter');
listeners.on('ready', readyHandler);    // equivalent to: event.on('my-emitter', 'ready', readyHandler);
listeners.off('ready', readyHandler);   // equivalent to: event.off('my-emitter', 'ready', readyHandler);
listeners.once('ready', readyHandler);  // equivalent to: event.once('my-emitter', 'ready', readyHandler);

namespaces

A getter to get all namespaces that have been registered using the emitter function. Returns an array of strings.

off ( namespace, event, callback )

Remove an event handler from the specific namespace and event type.

Parameters

  • namespace [required, string] - The namespace to stop listening on.
  • event [required, string] - The event type to stop listening for.
  • callback [required, function] - The function to remove.

Returns undefined

const event = require('protected-event-emmitter');

function readyHandler(data) {
    console.log('my-emiter is ready with data: ' + data);
}

event.off('my-emitter', 'ready', readyHandler);

on ( namespace, event, callback )

Add an event handler from the specific namespace and event type.

Parameters

  • namespace [required, string] - The namespace to start listening on.
  • event [required, string] - The event type to start listening for.
  • callback [required, function] - The function to call when an event on the namespace and of type occurs.

Returns undefined

const event = require('protected-event-emmitter');

function readyHandler(data) {
    console.log('my-emiter is ready with data: ' + data);
}

event.on('my-emitter', 'ready', readyHandler);

once ( namespace, event, callback )

Add an event handler from the specific namespace and event type that will only handle the event once.

Parameters

  • namespace [required, string] - The namespace to start listening on.
  • event [required, string] - The event type to start listening for.
  • callback [required, function] - The function to call when an event on the namespace and of type occurs.

Returns undefined

const event = require('protected-event-emmitter');

function readyHandler(data) {
    console.log('my-emiter is ready with data: ' + data);
}

event.once('my-emitter', 'ready', readyHandler);