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

eventment

v1.0.4

Published

Event class with some extra features

Downloads

13

Readme

Eventment

Node's event emitter for all engines with some extra features✨ With class

This implements the Node.js events module for environments that do not have it, like browsers.

events currently matches the Node.js API: Event: 'newListener' Event: 'removeListener' emitter.addListener(eventName, listener) emitter.emit(eventName[, ...args]) emitter.eventNames() emitter.getMaxListeners() emitter.listenerCount(eventName) emitter.listeners(eventName) emitter.off(eventName, listener) emitter.on(eventName, listener) emitter.once(eventName, listener) emitter.prependListener(eventName, listener) emitter.prependOnceListener(eventName, listener) emitter.removeAllListeners([eventName]) emitter.removeListener(eventName, listener) emitter.setMaxListeners(n) emitter.rawListeners(eventName)

Install

npm i eventment

Usage

import Eventment from 'eventment';
const eventment = new Eventment();

eventment.on('message', event => {
  console.log('on message', event);
});

eventment.emit('message', { hello: 'world' });

API

See the Node.js EventEmitter docs. events currently matches the Node.js API.

Extra features ✨

Multiple arguments

Allows issuing multiple arguments to listeners and emitters in a simple usage.

eventment.on('message', (event1, event2) => {
  console.log('on message', { event1, event2 });
});

eventment.emit('message', { some: 'message' }, { my: 'event' });

Multiple events

It is possible to multiple events in a listener and contrary.

It works for: .on, .once, .attach, .collect, .enable, .disable

eventment.on([ 'message', 'letter' ], data => {
  console.log('on message or letter', data);
});

eventment.emit('message', { my: 'message' });
eventment.emit('letter', { my: 'letter' });

// Or also
eventment.on('message', data => {
  console.log('on message', data);
});
eventment.on('letter', data => {
  console.log('on letter', data);
});

eventment.emit([ 'message', 'letter' ], { my: 'message', or: 'letter' });

Promisify .on and .once

In case there is no callback, the on and once functions return events in promises.

let count = 0;
setInterval(() => eventment.emit('plus', count++), 500);

const number = await eventment.once('plus');
console.log('once promise', { number });

for await (const number of eventment.on('plus')){
  console.log('on promise', { number });
  if(number >= 10) break;
}

Functions .attach and .collect

Accumulate events and capture later.

eventment.attach('thing');

eventment.emit('thing', '💻');
eventment.emit('thing', '⌚');
eventment.emit('thing', '🕹️');

const things = eventment.collect('thing');
// [ '💻', '⌚', '🕹️' ]

// With filter
eventment.attach('thing', thing => [ '💻', '⌨️', '🖱️' ].includes(thing));

eventment.emit('thing', '💻');
eventment.emit('thing', '⌨️');
eventment.emit('thing', '🖱️');
eventment.emit('thing', '💻');
eventment.emit('thing', '⌚');
eventment.emit('thing', '🕹️');

const things = eventment.collect('thing');
// [ '💻', '⌨️', '🖱️', '💻' ]

Functions .enable and .disable

eventment.on('fruit', fruit => console.log('on fruit', fruit));

eventment.emit('fruit', '🍏'); // listener working
eventment.emit('fruit', '🍊'); // listener working

eventment.disable('fruit');
eventment.emit('fruit', '🍇'); // do nothing
eventment.emit('fruit', '🍓'); // do nothing

eventment.enable('fruit');
eventment.emit('fruit', '🥑'); // listener working
eventment.emit('fruit', '🍍'); // listener working

Chained callbacks

Allows listeners with chained callbacks.

eventment.on('divergence', (num1, num2) => {
  console.log('on divergence', { num1, num2 });
  return num1 * num2;
}, (product, num1, num2) => {
  console.log('on product', { product, num1, num2 });
  return Math.pow(product, 2);
}, (square, num1, num2) => {
  console.log('on square', { square, num1, num2 });
});

let count1 = 0, count2 = 0;
eventment.emit('divergence', count1++, count2--);

Global listener *

Listen to all events with *

eventment.on('*', (event, data) => {
  console.log('on event', event, data);
});

eventment.emit('message', { my: 'event' });
eventment.emit('signal', { my: 'signal' });

Bind listener

Bind emitter in callback

const messageEmitterCallback = eventment.bind('message');
messageEmitterCallback({ my: 'data' });

eventment.on('message', data => {
  console.log('on event message', data);
});

License

MIT License © Fellipe Paiva