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

@dominicstop/ts-event-emitter

v1.1.0

Published

Yet another event emitter written in typescript.

Downloads

108,380

Readme

ts-event-emitter

(yet another) event emitter written in typescript 😔

Installation

# install via npm...
npm install @dominicstop/ts-event-emitter

# or install via yarn.
yarn add @dominicstop/ts-event-emitter

Usage

Basic Usage

type EventKeys = 'Foo' | 'Baz';

export const emitter: TSEventEmitter<EventKeys, {
  Foo: string;
  Baz: { age: number } | null;
}> = new TSEventEmitter();

emitter.once('Foo', (event) => { console.log(event) });
emitter.emit('Foo', 'hello world');

emitter.once('Baz', (event) => { console.log(event?.age) });
emitter.emit('Baz', { age: 23 });

In-Depth Usage

// 1. Create a string enum...
enum Events { Foo = 'Foo', Bar = 'Bar', Baz = 'Baz' };

// 1.1. ...or alt., a union of strings (whichever you prefer).
type EventKeys = 'Foo' | 'Bar' | 'Baz';

// 2. Create the emitter + event map...
// * For each key in the event enum, define the type of the 
//    `event` argument that the listener will receive.
// * As mentioned in 1.1. you can also use the `EventKeys` union 
//   in place of the `Events` enum.
export const emitter: TSEventEmitter<EventKeys, {

  // 2.1. For the event `TestEnum.Foo`, this will be its event object.
  Foo: { name: string },

  // 2.2. If you don't want to pass an event argument, 
  //      then set it to `null` or `undefined`.
  // * This means the event listener won't receive any arguments
  //   and you can't pass a data argument to `emit` (see step 4.2.)
  Bar: null, 

  // 2.3. or alt., you can make the event param "optional".
  Baz: null | { age: number }
}> = new TSEventEmitter();

// 3.1. `event` arg. will be inferred as `(event: { name: string }) => void`
emitter.once('Foo', (event) => { console.log(event.name) });

// 3.2.`event` arg. will be inferred as `() => void`
// * Note: If you try to add an event param, TS produces an error.
emitter.once('Bar', () => { /** no-op */ });

// 3.3. `event` will be inferred as 
//      `(event: { age: number } | null) => void`.
// * As such, we need to use the optional chaining operator to access `event`.
emitter.once(Events.Baz, (event) => { console.log(event?.age) });
   
// 4. Next, lets broadcast some events.
emitter.emit(Events.Foo, { name: 'd'});
emitter.emit('Baz', { age: 1 });
 
// 4.1. Note: You can't pass a data argument with the 'Bar' event
// because in step 2.2., the event object is defined as `null`.
emitter.emit('Bar');
 
// 4.2. We must explicitly pass a null/undefined value for the data
// argument since it can be optional (e.g. step 2.3.)
emitter.emit('Baz', null);

Documentation

TSEvenEmitter

| Property/Method | Description | | ------------------------------------------------------------ | ----------- | | 🔤 addListener⚛️ (eventKey, listener) => { unsubscribe: () => void } | | | 🔤 removeListener⚛️ (eventKey, listenerToRemove) => void | | | 🔤 once⚛️ (eventKey, listener) => void | | | 🔤 removeAllListeners⚛️ () => void | | | 🔤 emit⚛️ (eventKey, data) => void | |