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

@omts/event-emiter

v0.0.1

Published

A type-safe, flexible, and efficient `EventEmitter` implementation in TypeScript for both Node.js and browser environments. This package simplifies the management of event-driven architecture with strict type safety, optimized performance, and easy-to-use

Downloads

1

Readme

@omts/event-emitter 🚀

A type-safe, flexible, and efficient EventEmitter implementation in TypeScript for both Node.js and browser environments. This package simplifies the management of event-driven architecture with strict type safety, optimized performance, and easy-to-use APIs.

Installation 📦

Install the package via npm or pnpm:

npm install @omts/event-emitter

or

pnpm add @omts/event-emitter

Usage ✨

The package exports an EventEmitter class that allows you to subscribe to events, emit events, manage listeners with type safety, and handle errors with detailed context. Here’s how to use it:

Example

Define your events and their payloads using TypeScript's type system:

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

// Define your events and their payload types
interface Events {
  data: number;
  error: Error;
  message: string;
}

// Create an instance of EventEmitter
const emitter = new EventEmitter<Events>();


// Subscribe to 'data' event with disposable support
const subscription = emitter.on('data', (num) => {
  console.log('Received data:', num);
});

// Emit 'data' event
emitter.emit('data', 42);  // Output: "Received data: 42"

// Dispose of the subscription
subscription.dispose();

// Emit 'data' event again - no output this time since the listener is disposed
emitter.emit('data', 100);

// Subscribe to 'data' event with a listener that triggers only once
const singleUseSubscription = emitter.once('data', (num) => {
  console.log('Received data once:', num);
});

// Emit 'data' event - triggers the listener once
emitter.emit('data', 200);  // Output: "Received data once: 200"

// Emit 'data' event again - no output since the listener is automatically removed
emitter.emit('data', 300);

// Subscribe to an event
const subscription = emitter.on('data', (num) => {
  if (num === 42) throw new Error('The number 42 is not allowed!');
  console.log('Received data:', num);
});

// Register a custom error handler
const errorSubscription = emitter.onError((event, error, listener) => {
  console.error(`Error in event '${event}': ${error.message}`);
  console.error('Listener that caused the error:', listener);
});

// Emit 'data' event that will cause an error
emitter.emit('data', 42);  
// Output: "Error in event 'data': The number 42 is not allowed!"
// "Listener that caused the error: [Function: listener]"

// Dispose the error handler
errorSubscription.dispose();

// Emit 'data' event again - error will not be handled as the handler is removed
emitter.emit('data', 42);  // No output

// Dispose the data event listener
subscription.dispose();

Key Features ✨

  • Type-Safe Event Handling: Define events and payloads using TypeScript types, ensuring compile-time checks and reducing runtime errors.
  • Built-in once Method: Automatically remove listeners after they are triggered once, preventing memory leaks.
  • Detailed Error Handling: Register custom error handlers with onError to handle errors with context, including the event name, error object, and the listener that caused the error.
  • Flexible and Lightweight: Works seamlessly in both Node.js and browser environments without additional dependencies.
  • Supports Removing Specific Listeners: Easily manage event listeners and remove them when no longer needed.

API Documentation 📚

  • on(event, listener): Adds a listener for the specified event.

    • event: The event name.
    • listener: The callback function that will be invoked when the event is emitted.
    • Returns: A Disposable object to remove the listener.
  • off(event, listener): Removes a specific listener from the event.

    • event: The event name.
    • listener: The callback function to be removed.
  • once(event, listener): Adds a listener that will be called at most once.

    • event: The event name.
    • listener: The callback function that will be invoked only once.
    • Returns: A Disposable object to remove the listener.
  • emit(event, data): Emits an event with the specified data.

    • event: The event name.
    • data: The data to be passed to the listeners.
    • Returns: true if listeners are called; false if there are no listeners.
  • onError(handler): Registers a custom error handler for events.

    • handler: A function that receives the event name, error object, and listener that caused the error.
    • Returns: A Disposable object to remove the error handler.
  • removeAllListeners(event): Removes all listeners for the specified event.

    • event: The event name.

Complexity 📊

  • Time Complexity:

    • Adding or removing listeners: O(1) average case with Set.
    • Emitting events: O(n), where n is the number of listeners for the event.
    • Error handling: O(m), where m is the number of error handlers.
  • Space Complexity:

    • O(n + m), where n is the number of registered listeners and m is the number of registered error handlers.

Development 🛠️

Bun and Development Setup ⚙️

  1. Install Bun: Visit the official Bun website for installation instructions.

  2. Install Bun VSCode Extension: Install the Bun for Visual Studio Code extension by Oven from the Visual Studio Marketplace.

  3. Debugging with Bun:

    • Set a breakpoint in index.ts by adding debugger.
    • Switch to index.test.ts, then open the command palette (Cmd + Shift + P on Mac, Ctrl + Shift + P on Windows/Linux) and select Bun: Debug.

Development Commands

  • clean: Removes the dist directory.
    pnpm run clean

  • build: Cleans and builds the package.
    pnpm run build

  • tdd: Runs the tests in watch mode, useful for Test-Driven Development.
    pnpm run tdd

  • test: Runs all test cases.
    pnpm run test

  • prepublishOnly: Runs the build command before publishing.
    pnpm run prepublishOnly

Contributing 🤝

Contributions are welcome! If you have any improvements, suggestions, or bug fixes, please feel free to open an issue or submit a pull request.

License ⚖️

This project is licensed under the MIT License - see the LICENSE file for details.


Happy coding with @omts/event-emitter! 🎉

Feel free to use, contribute, and star the repository if you find it useful!