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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@supercat1337/event-emitter

v1.0.12

Published

Event Emitter

Readme

@supercat1337/event-emitter 🐈⚡

A modern, feature-rich EventEmitter implementation for JavaScript and TypeScript with advanced capabilities and excellent type safety.

Features

  • Full TypeScript support with generics and complete type definitions
  • 🎯 Promise-based event waiting with timeout support
  • 📊 Listener lifecycle tracking - know when events gain/lose listeners
  • 🛡️ Memory safe - automatic cleanup and unsubscribe functions
  • High performance - optimized for frequent events
  • 🔒 Immutable patterns - listener arrays are copied during emission
  • 🚀 Modern ES2022+ - private fields, arrow functions, and more

Installation

npm install @supercat1337/event-emitter

Quick Start

import { EventEmitter } from '@supercat1337/event-emitter';

// Define your event types
type AppEvents = 'user:created' | 'user:deleted' | 'notification:sent';

const emitter = new EventEmitter<AppEvents>();

// Subscribe to events
const unsubscribe = emitter.on('user:created', (userData) => {
    console.log('User created:', userData);
});

// Emit events
emitter.emit('user:created', { id: 1, name: 'John' });

// Unsubscribe when done
unsubscribe();

API Reference

Core Properties

isDestroyed

Is the event emitter destroyed?

console.log(emitter.isDestroyed); // false
emitter.destroy();
console.log(emitter.isDestroyed); // true

events

The list of events and their listeners.

console.log(emitter.events); // { 'user:created': [Function] }

logErrors

Should errors be logged to the console?

emitter.logErrors = true;

Core Methods

on(event, listener)

Subscribe to an event. Returns an unsubscribe function.

const unsubscribe = emitter.on("user:created", (data) => {
    console.log(data);
});

// Later...
unsubscribe();

off(event, listener)

Remove a specific listener from an event.

function listener(data) {
    /* ... */
}
emitter.on("user:created", listener);
emitter.off("user:created", listener);

emit(event, ...args)

Emit an event with optional arguments.

emitter.emit("user:created", { id: 1, name: "Alice" });

once(event, listener)

Add a one-time listener that auto-removes after execution.

emitter.once("app:ready", () => {
    console.log("App is ready!");
});

Advanced Methods

waitForEvent(event, [maxWaitMs])

Wait for an event using Promises.

// Wait indefinitely
const result = await emitter.waitForEvent("data:loaded");

// Wait with timeout (returns false if timeout reached)
const success = await emitter.waitForEvent("data:loaded", 5000);

waitForAnyEvent(events, [maxWaitMs])

Wait for any of multiple events.

const events = ['success', 'error', 'timeout'] as const;
const result = await emitter.waitForAnyEvent(events, 3000);

onHasEventListeners(callback)

Get notified when any event gains its first listener.

emitter.onHasEventListeners((eventName) => {
    console.log(`Event ${eventName} now has listeners!`);
});

onNoEventListeners(callback)

Get notified when any event loses its last listener.

emitter.onNoEventListeners((eventName) => {
    console.log(`Event ${eventName} has no more listeners!`);
});

onListenerError(callback)

Get notified when any listener throws an error.

emitter.onListenerError((error, eventName, ...args) => {
    console.error(`Listener for event ${eventName} threw an error:`, error);
});

Lifecycle Management

destroy()

Completely destroy the emitter and clean up all resources.

emitter.destroy();
console.log(emitter.isDestroyed); // true

clear()

Remove all listeners while keeping the emitter functional.

emitter.clear();

clearEventListeners(event)

Remove all listeners for a specific event.

emitter.clearEventListeners("user:created");

TypeScript Usage

import { EventEmitter } from "@supercat1337/event-emitter";

// Define your event types
type MyEvents =
    | "user:created"
    | "user:updated"
    | { type: "user:deleted"; payload: { id: string; reason: string } };

const emitter = new EventEmitter<MyEvents>();

// Full type safety!
emitter.emit("user:created", { id: 1, name: "John" }); // ✅ Correct
emitter.emit("user:created", "invalid"); // ❌ Type error

Error Handling

All listener errors are caught and logged to console, preventing emitter crashes:

emitter.on("data:received", () => {
    throw new Error("Something went wrong!");
});

// Error is caught and logged, emitter continues working
emitter.emit("data:received");

Performance Notes

  • 🔄 Listener arrays are copied before iteration to allow safe modification during emission
  • ⚡ Event existence checks are optimized with direct property access
  • 🗑️ Automatic cleanup prevents memory leaks
  • 📏 No external dependencies

Browser Support

| Feature | Support | | ---------- | --------------- | | ES2022+ | Modern browsers | | TypeScript | 4.0+ | | Node.js | 14+ |

License

MIT License - feel free to use in commercial projects.

Contributing

Contributions welcome! Please ensure:

  • ✅ All tests pass
  • ✅ TypeScript types are maintained
  • ✅ New features include tests
  • ✅ Code follows existing style

Made with ❤️ by supercat1337