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

better-event-emitter

v1.0.1

Published

A typed EventEmitter on steroids

Downloads

3

Readme

Better Event Emitter

BetterEventEmitter is a custom event emitter with types and more functions.

Installation

npm i better-event-emitter

Usage

Typing the emitter

Typing using Generics


import { BetterEventEmitter } from 'better-event-emitter';

type EmitterType = {
    login: (username: string, password: string) => void,
    logout: (username: string) => void,
}

const emitter = new BetterEventEmitter<EmitterType>();

Typing using constructor


import { BetterEventEmitter } from 'better-event-emitter';

const emitter = new BetterEventEmitter({
    login: (username: string, password: string) => {},
    logout: (username: string) => {},
});

Register event listeners

on method

You can register a regular event listener using the on method. This listener will be called every time the specified event is emitted:

emitter.on('eventName1', (arg1, arg2) => {
  // Your event handler logic here
  console.log(`Event "eventName1" emitted with arguments: ${arg1}, ${arg2}`);
});

once method

You can register a one-time event listener using the once method. This listener will be called only once for the specified event:

emitter.once('eventName2', () => {
  // Your event handler logic here
  console.log('This listener will be called once when "eventName2" is emitted.');
});

onAny method

You can register an event listener for every event using the onAny method. This listener will be called for every emitted event:

emitter.onAny((event, ...args) => {
  // Your event handler logic here
  console.log(`Event ${event} emitted with arguments: ${arg1}, ${arg2}`);
});

Emit events

emit method

You can emit events using the emit method. Pass the event name and any required arguments:

emitter.emit('eventName1', 'Hello', 42);
emitter.emit('eventName2');

Remove event listeners

removeListener method

You can remove a specific event listener using the removeListener method. Provide the event name and the callback function:

const eventHandler = (arg1: string, arg2: number) => {
  // Your event handler logic here
};
emitter.on('eventName1', eventHandler);

// Later, remove the event listener
emitter.removeListener('eventName1', eventHandler);

removeGlobalListener method

You can remove a global event listener (registered using onAny) using the removeGlobalListener method. Provide the callback function:

const globalEventHandler = (event: keyof typeof emitter, ...args: any) => {
  // Your global event handler logic here
};
emitter.onAny(globalEventHandler);

// Later, remove the global event listener
emitter.removeGlobalListener(globalEventHandler);

Dispose the emitter

You can dispose the BetterEventEmitter instance to remove all event listeners:

emitter.dispose();

Example


import { BetterEventEmitter } from 'better-event-emitter';

type EmitterType = {
    login: (username: string, password: string) => void,
    logout: (username: string) => void,
}

const emitter = new BetterEventEmitter<EmitterType>();

emitter.on('login', (username, password) => {
    console.log(`${username} has logged in`);
});

emitter.on('logout', (username) => {
    console.log(`${username} has logged out`);
});