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

@esmj/emitter

v0.4.3

Published

Lightweight emitter with zero dependencies for sync/async operation with from listeners result

Downloads

432

Readme

Emitter

The @esmj/emitter is tiny (O,9kB) async event emitter library.

It works in Node.js and the browser (using a bundler like webpack).

Requirements

  • Node 18+

Install

npm install @esmj/emitter

Usage

It works for both Javascript modules (ESM and CJS).

import { Emitter } from '@esmj/emitter';

const emitter = new Emitter();

emitter.on('some-event', async (event) => {
  return 2 * event.result;
});

emitter.on('some-event', (event) => {
  return 3 * event.result;
});

emitter.on('some-event', (event) => {
  event.stopPropagation();
});

emitter.on('some-event', async (event) => {
  return 4 * event.result;
});

const { result } = await emitter.emit('some-event', { result: 1 });

console.log(result); // 6

Or if you want to use custom name for result from your listeners. You can use RESULT_KEY symbol as example below.

import { Emitter, RESULT_KEY } from '@esmj/emitter';

const emitter = new Emitter();

emitter.on('some-event', async (event) => {
  return 2 * event.count;
});

emitter.on('some-event', (event) => {
  return 3 * event.count;
});

emitter.on('some-event', (event) => {
  event.stopPropagation();
});

emitter.on('some-event', async (event) => {
  return 4 * event.count;
});

const { count } = await emitter.emit('some-event', { count: 1, [RESULT_KEY]: 'count' });

console.log(count); // 6

API

emitter = new Emitter(options?)

Create a new instance of Emitter.

options?

Type: object

Configure the new instance of Emitter.

logger?

Type: object Default: console

Configure the logger for this instance.

debug?

Type: boolean|function(eventName, event) Default: false

Configure the debugging options for this instance.

on(eventName, listener) / addListener(eventName, Listener)

Subscribe listener for event.

Returns an unsubscribe method.

off(eventName, listener) / removeListener(eventName, Listener)

Remove subscription.

once(eventName, listener)

Subscribe event for listener only once. It will be unsubscribed after the first call.

emit(eventName, data?)

Trigger an event asynchronously/synchronously. It is depends on returns value from listener. Listeners are called in the order they were added and executed serially.

emitParallel(eventName, data?)

Trigger an event asynchronously in parallel. The listeners for the event will be executed concurrently and when the listeners are not dependent on each other.

Returns a promise that resolves with an event, which has result an array of results from the event listeners. The promise is rejected if one of the listeners throws an error.


import { Emitter } from '@esmj/emitter';

const emitter = new Emitter();

emitter.on('some-event', async (event) => {
  return 2 * event.count;
});

emitter.on('some-event', (event) => {
  return 3 * event.count;
});

emitter.on('some-event', () => {});

emitter.on('some-event', async (event) => {
  return 4 * event.count;
});

const { counts } = await emitter.emitParallel('some-event', { count: 1, RESULT_KEY: 'counts' });

console.log(counts); // [2, 3, undefined, 4]