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

single-emit

v2.0.0

Published

Emit a single event for a collection of events

Downloads

171

Readme

Single-emit

Travis Build Status David devDependencies Status

Small super light-weight concise module with no dependencies to listen for events on an EventEmitter instance and emit a single event when all events have been emitted. Think of it as a Promise.all, but for events. With support for Objects and Arrays

How it works

npm install --save single-emit

const fs = require('fs')
const EmitOnce = require('single-emit')

const myObjectOfStreams = {
  stream1: fs.createReadStream('./myFile1.txt'),
  stream2: fs.createReadStream('./myFile2.txt'),
  stream3: fs.createReadStream('./myFile3.txt'),
}

// listen for the 'end' event
const emitOnce = new EmitOnce(myObjectOfStreams, 'end')

emitOnce.on('end', () => {
  // All streams have emitted the 'end' event
})

If you expect your events to fire with data, the collection of data is passed to the callback as an array or an object. If the collection of events is an array, the data passed to the callback is in the same order as the array passed in, not in the order the events were emitted. If the collection is an object, the data is simply mapped to the keys of the object.

For another contrived example:

const fs = require('fs')
const EmitOnce = require('single-emit')

const stream1 = fs.createReadStream('./myFile1.txt')
const stream2 = fs.createReadStream('./myFile2.txt')
const stream3 = fs.createReadStream('./myFile3.txt')

const myArrayOfStreams = [stream1, stream2, stream3]

const emitOnce = new EmitOnce(myArrayOfStreams, 'data')

emitOnce.on('data', (data) => {
  // All streams have emitted the 'data' event once
  // `data` contains an array of data from the streams
  console.log(data[0]) // -> stream1 data
  console.log(data[1]) // -> stream2 data
  console.log(data[2]) // -> stream3 data
})

Important

Events are only recorded the first time they are emitted so in the example above, only the first data event on each stream will be recorded

Errors

Certain errors will be thrown and others will be emitted. If an event emitter emits an error, the error will be emitted as well, so it's a good idea to listen for errors. Also, if a value in the collection is not an instance of the Event emitter, an error will be emitted. Other errors like passing in an invalid collection of event emitters or not passing an event to listen for will throw an error.

API

  • The constructor accepts two required arguments:
    • collection: A collection of event emitters which could be an array of or an object
    • event: The event to listen for. This could be any string or Symbol. Note that if you listen for the error event, this would be emitted once any item emits an error and does not wait on others.

Contributing

Tests are written with Jest. Issues and pull requests are always welcome :-)