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

remitter

v0.4.6

Published

A TypeScript friendly event emitter with easy re-emitting events.

Downloads

4,364

Readme

remitter

Docs Build Status npm-version Coverage Status minified-size

A tiny TypeScript friendly event emitter that supports lazy re-emitting events from other sources.

Install

npm add remitter

Usage

import { Remitter } from "remitter";

interface EventData {
  event1: string;
  event2: void;
}

const remitter = new Remitter<EventData>();

const disposer = remitter.on("event1", value => {
  console.log("event1", value);
});

remitter.once("event1", value => {
  console.log("event1-once", value);
});

remitter.has("event1"); // true

remitter.emit("event1", "hello"); // logs "event1 hello" and "event1-once hello"

remitter.emit("event1", "hello"); // logs "event1 hello"

remitter.emit("event2"); // nothing logs

disposer();
remitter.emit("event1", "world"); // nothing logs

remitter.clear("event2"); // remove all listeners for event2

remitter.has(); // false

remitter.dispose(); // removes all listeners and dispose tapped events

Listen to any event

import { Remitter } from "remitter";

interface EventData {
  event1: string;
  event2: string;
}

const remitter = new Remitter<EventData>();

remitter.onAny(({ event, data }) => {
  console.log(event, data);
});

remitter.emit("event1", "hello"); // logs "event1 hello"
remitter.emit("event2", "world"); // logs "event2 world"

Listen to unhandled subscriber errors

import { Remitter } from "remitter";

interface EventData {
  event1: string;
  event2: string;
}

const remitter = new Remitter<EventData>();

remitter.onError(error => {
  console.log(error);
});

remitter.emit("event1", () => {
  throw new Error("error");
});

remitter.emit("event2", async () => {
  await new Promise(resolve => setTimeout(resolve, 100));
  throw new Error("async-error");
});

Remit

You may tap into other events easily with remit. It is lazy-executed when listener count of the event name grows from 0 to 1. It is disposed when listener count of the event name drops from 1 to 0.

remitter.remit("cursor", () => {
  const handler = ev => {
    remitter.emit("cursor", { x: ev.clientX, y: ev.clientY });
  };

  window.addEventListener("mousemove", handler);

  return () => {
    window.removeListener("mousemove", handler);
  };
});

// Remit callback does not execute until the first "cursor" listener is added
remitter.on("cursor", value => {
  console.log("cursor", value);
});

// Remit callback is disposed when no listener on the
// "cursor" event. (`window.removeListener` triggered)
remitter.clear("cursor");

The callback function can also be a pure function.

const myCursorEvent = remitter => {
  const handler = ev => {
    remitter.emit("cursor", { x: ev.clientX, y: ev.clientY });
  };

  window.addEventListener("mousemove", handler);

  return () => {
    window.removeListener("mousemove", handler);
  };
};

remitter.remit("cursor", myCursorEvent);

Acknowledgment

Huge thanks to @recursivefunk for giving away the NPM package name remitter.