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

event-as-a-property

v2.0.0

Published

a strongly typed typed event-as-a-property wrapper for Node's EventEmitter

Downloads

2

Readme

A strongly typed event-as-a-property wrapper for Node's EventEmitter.

Synopsis

All methods delegate to the EventEmitter passed in the constructor. Their semantics are identical, except the event name is implicit, and the return type is void.

export default class Event<TArgs extends any[] = []> {
  readonly name: string | symbol;

  constructor(emitter: EventEmitter, name?: string | symbol = Symbol()) {}

  /** has to be the same `EventEmitter` from which it was constructed */
  emit(emitter: EventEmitter, ...args: TArgs): void;

  addListener(listener: (...args: TArgs) => void): void;
  on(listener: (...args: TArgs) => void): void;

  once(listener: (...args: TArgs) => void): void;

  removeListener(listener: (...args: TArgs) => void): void;
  off(listener: (...args: TArgs) => void): void;

  removeAllListeners(): void;

  prependListener(listener: (...args: TArgs) => void): void;
  prependOnceListener(listener: (...args: TArgs) => void): void;

  promise(): Promise<TArgs>;
}

Usage

import { EventEmitter } from "events";
import Event from "event-as-a-property";

class Publisher {
  /** create an `EventEmitter` for the event source */
  private _emitter = new EventEmitter();

  /** declare the event as a property on the event source */
  onPublish = new Event<[date: Date, text: string]>(this.emitter);

  publish(date: Date, text: string): void {
    /** emit the event using the `EventEmitter` */
    this.emitter.emit(this.onPublish.name, date, text);

    /** or use a typed wrapper */
    this.onPublish.emit(this._emitter, date, text);
  }
}

const publisher = new Publisher();

/** subscribe to an event using the event propery */
publisher.onPublish.addListener((date, text) => console.log(date, text));

/** or add a listener that fires only once */
publisher.onPublish.once((date, text) => console.log(date, text));

/** or await the next event */
const [date, text] = await publisher.onPublish.promise();

Why?

self-documenting code

/**
 * What events does `Bad` have?
 * You need to either document it textually,
 * or provide _seven_ method overloads per event.
 */
interface Bad extends EventEmitter {
  addListener(event: "exit", listener: (status: number) => void): void;
  on(event: "exit", listener: (status: number) => void): void;
  once(event: "exit", listener: (status: number) => void): void;
  prependListener(event: "exit", listener: (status: number) => void): void;
  prependOnceListener(event: "exit", listener: (status: number) => void): void;
  removeListener(event: "exit", listener: (status: number) => void): void;
  off(event: "exit", listener: (status: number) => void): void;
}

/**
 * `Good` has an `exit` event with a `status` parameter that is a `number`.
 */
interface Good {
  readonly exit: Event<[status: number]>;
}

separation of concerns

/**
 * Why should clients be able to emit events?
 */
let bad: Bad;
/* ... */
bad.addListener("exit", console.log);
bad.emit("exit", 0);

/**
 * Have to have the right EventEmitter to emit events.
 */
let good: Good;
/* ... */
good.exit.addListener(console.log);
good.exit.emit(new EventEmitter(), 0); // error: wrong EventEmitter

easy to navigate and refactor

/**
 * To rename the event, you have to somehow find all subscribers
 * and change the `"exit"` literal in each of them
 */
let bad: Bad;
/* ... */
bad.addListener("exit", console.log);

/**
 * To rename the event, you just have to rename the property with an IDE.
 * To find subscribers, you can just `Find All Usages` with an IDE.
 */
let good: Good;
/* ... */
good.exit.addListener(console.log);

easier to use with documentation generators

/**
 * You can use JSDoc.
 * @event exit
 */
interface Bad extends EventEmitter {
  /**
   * Whoops! Event has been renamed, but the docs are not updated.
   */
  addListener(event: "stop", listener: (status: number) => void): void;
}

interface Good {
  /**
   * Event docs are inseparable from the event property.
   */
  readonly exit: Event<[status: number]>;
}

easy to migrate from EventEmitter

/**
 * @event start
 * @event exit
 */
class Bad extends EventEmitter {
  /* ... */
}

/**
 * First, add the event properties.
 * Then, deprecate the methods inherited from `EventEmitter`
 */
class Better extends EventEmitter {
  readonly start = new Event<[]>(this, "start");
  readonly exit = new Event<[status: number]>(this, "exit");
}

/**
 * Finally, move `EventEmitter` to an implementation detail.
 */
class Best {
  private readonly _emitter = new EventEmitter();

  readonly start = new Event<[]>(this._emitter);
  readonly exit = new Event<[status: number]>(this._emitter);
}