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

tahasoft-event-emitter

v1.1.0

Published

A simple and generic JavaScript Event Emitter class for implementing the Observer Pattern, now with support for AbortController for automatic listener removal.

Downloads

48

Readme

Event Emitter

npm version license npm downloads

A modern, lightweight Event Emitter for JavaScript and TypeScript, inspired by the Observer Pattern.

Key features:

  • 🚀 Simple API: add, emit, remove, addOnce, removeAll
  • 🧑‍💻 TypeScript-first: Full type safety out of the box
  • ✨ Both callback and Promise/async usage: await emitter.addOnce()
  • 🦺 Memory-safe: Listeners can auto-remove with AbortController
  • ⚡ Zero dependencies, production-ready, tiny footprint

Use it for events, hooks, signals, or decoupled reactive design!

observer-pattern-typescript-javascript


Installation

npm install tahasoft-event-emitter

Usage

import { EventEmitter } from 'tahasoft-event-emitter';

Basic Example

/** @type {EventEmitter<string>} */
const onStatusChange = new EventEmitter();

onStatusChange.add(status => {
	console.log('Status changed to:', status);
});

onStatusChange.emit('Ready');

Usage in a Class

class User {
	constructor() {
		/** @type {EventEmitter<string>} */
		this.onStatusChange = new EventEmitter();
	}

	updateStatus(status) {
		this.onStatusChange.emit(status);
	}
}

const user = new User();
user.onStatusChange.add(status => {
	console.log('New status:', status);
});

user.updateStatus('Active');

TypeScript Example

class User {
	public onLogin = new EventEmitter<string>();

	public login(userName: string, password: string) {
		// validate...
		this.onLogin.emit(userName);
	}
}

const user = new User();

user.onLogin.add(name => {
	console.log(`User ${name} has logged in`);
});

user.login('John', '1234abcd');

Advanced Usage

One-Time Listeners

Execute a listener only once, then remove automatically:

onStatusChange.addOnce(status => {
	console.log('This runs once for status:', status);
});

onStatusChange.emit('Online'); // Triggers above
onStatusChange.emit('Offline'); // Listener is not called again

Promise/Async-Await Style

Wait for the next event occurrence with a promise:

async function waitForStatus() {
	const newStatus = await onStatusChange.addOnce();
	console.log('Status awaited:', newStatus);
}
waitForStatus();
onStatusChange.emit('Loaded');

Cleanup with AbortController

Auto-remove listeners using AbortSignal:

const controller = new AbortController();
onStatusChange.add(status => console.log('Listen once, then remove automatically if aborted:', status), {
	signal: controller.signal
});

// Later...
controller.abort(); // Listener is removed

API Reference

Add Listeners

add(listener, options?)
addListener(listener, options?)
addEventListener(listener, options?)
subscribe(listener, options?)

options can include { signal: AbortSignal } for automatic removal.

Remove Listeners

remove(listener);
removeListener(listener);
removeEventListener(listener);
unsubscribe(listener);

Remove All Listeners

removeAll();
removeAllListeners();

Emit an Event

emit(arg);
dispatch(arg);

The arg is passed to all listeners.

One-Time Listeners

  • Callback form:
    addOnce(listener, options?)
  • Promise form:
    await addOnce(options?) resolves with the next value emitted.

Listener Options

{ signal: AbortSignal } – pass an AbortController signal for automatic listener removal:

const controller = new AbortController();
emitter.add(listener, { signal: controller.signal });
controller.abort(); // listener removed

Why Choose This EventEmitter?

  • Strictly typed for single-argument events—works great with primitives or objects.
  • Promise/async support for modern codebases.
  • Abortsafe: no memory leaks—auto-cleanup with AbortController.
  • Minimal and fast—~1KB gzipped, zero dependencies.
  • Production ready—used in both Node.js and browser projects.

License

MIT


Powered by tahasoft-event-emitter – the robust TypeScript/JavaScript event emitter for modern apps.