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

eventyoshi

v1.0.0

Published

Allows several event emitters to be listened and emitted through a single one.

Downloads

1,535

Readme

eventyoshi

Allows several event emitters to be handled and emitting to a single one.

concept

Build Status Dependency Status codecov

Usage

const EventEmitter = require('events').EventEmitter;
const EventYoshi = require('eventyoshi');

let ee1 = new EventEmitter();
let ee2 = new EventEmitter();
let yoshi = new EventYoshi()
  .add(ee1)
  .add(ee2);

yoshi.on('foo', () => {
  console.log('foo!');
});
ee1.emit('foo'); // foo!
ee2.emit('foo'); // foo!

Why?

Why would you use this instead of doing something like

ee1.on('foo', listener);
ee2.on('foo', listener);

Well, you could do that, or you could let EventYoshi handle all the logic for you flawlessly and without modifying the underlying child event emitters. EventYoshi can be treated as another EventEmitter. You can pass it around without having to tell whoever you passed it to what emitters you're listening to and which you aren't listening to anymore.

Same goes for events you might listen to or remove later. As you add more event emitters to event yoshi, it will add listeners that you were already listening for to the emitter you added.

let yoshi = new EventYoshi();
yoshi.on('a', () => {
  console.log('a emitted');
});

let ee = new EventEmitter();
yoshi.add(ee);

ee.emit('a'); // a emitted

And as you remove emitters, all listeners that were added through event yoshi are removed.

yoshi.remove(ee);
ee.emit('a'); // nothing emitted on yoshi

EventYoshi also supports the once method. It supports listening to newListener such that it is emitted only when listeners are added to your EventYoshi instance and not when they are added to child emitters.

API

yoshi.add(emitter)

Adds an event emitter to an event yoshi.

yoshi.remove(emitter)

Remove an event emitter from an event yoshi.

yoshi.proxy(fn)

Proxies all calls from to yoshi[fn] to its children.

yoshi.add(writeStream);
yoshi.proxy('write', 'end');

yoshi.write(data); // this will call writeStream.write() with data
yoshi.end(); // will call writeStream.end()

When the proxy'd functions are called, they return the values returned from called functions in an array. If the array's length is only 1, returns only the first value.

Events

When events are emitted, yoshi.child will contain the child emitter the event came from.

yoshi.on('event', () => {
  console.log('Event came from: ', yoshi.child);
});

Event: 'newChildListener'

  • string - Event.
  • Function - Listener.

Emitted when a listener is added to a child event emitter. Does not emit listeners added by EventYoshi.

Event: 'removeChildListener'

  • string - Event.
  • Function - Listener.

Emitted when a listener is removed from a child event emitter. Does not emit listeners added by EventYoshi.

Install

npm install eventyoshi

Tests

Tests are written with mocha

npm test