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

ton.emit

v2.0.2

Published

一个事件管理工具

Readme

TonEmitter

A lightweight and fully type-safe Event Emitter for JavaScript / TypeScript, supporting:

  • Priority-based listeners
  • Once-only listeners
  • Listener IDs for replacement
  • Synchronous and asynchronous listeners
  • Attach emitter methods (on, off, once, emit, clear) directly to any class instance
  • Full TypeScript generic support for event names and parameters

Installation

# npm
npm install ton.emit

# yarn
yarn add ton.emit

Usage (JavaScript)

import { TonEmitter } from 'ton.emit';

const emitter = new TonEmitter();

// Regular listener
emitter.on('event1', data => {
  console.log('event1 received', data);
});

// Once listener
emitter.once('event2', () => console.log('event2 fired once'));

// Emit events
emitter.emit('event1', 123);
emitter.emit('event2');
emitter.emit('event2'); // will not fire

// Remove listener
const handler = data => console.log(data);
emitter.on('event3', handler);
emitter.off('event3', handler);

// Clear events
emitter.clear('event1'); // clear specific
emitter.clear(); // clear all

Usage (TypeScript, Type-Safe)

import { TonEmitter } from 'ton.emit';

interface MyEvents {
  login: [string];           // username
  logout: [];                 // no arguments
  dataUpdate: [number, string]; // id, value
}

class MyService {
  constructor() {
    // Automatically attach emitter methods to this
    this.emitter = new TonEmitter<MyEvents>(this);
  }

  doStuff() {
    // Type-safe event registration
    this.on('login', username => {
      console.log('User logged in:', username);
    });

    this.emit('login', 'Alice'); // ✅ correct
    // this.emit('login', 123);  ❌ TypeScript will report error
  }
}

API

new TonEmitter(target?: object)

  • target (optional): If provided, automatically attaches on, off, once, emit, clear methods to the object.

on(eventName, listener, options?)

Registers a listener.

  • eventName: string | keyof Events
  • listener: function to handle event
  • options:
    • id (optional): unique ID for the listener, allows replacement
    • priority (optional, default 0): higher priority listeners are executed first

once(eventName, listener, options?)

Registers a listener that executes only once.

off(eventName, identifier)

Removes listener(s) by:

  • function reference
  • { fn } object
  • listener ID string

emit(eventName, ...args)

Triggers event, returning an array of results. Automatically handles async listeners with Promise.all.

clear(eventName?)

  • Clears a specific event if eventName provided, otherwise clears all events.

attachTo(target, methods?)

  • Attach emitter methods to any object.
  • methods (optional): array of method names to attach (default ['on','off','once','emit','clear']).

Features

  • Fully TypeScript typed, supports generics for event names and listener argument types.
  • Priority-based listener execution.
  • Listener replacement by ID.
  • Works with sync and async listeners.
  • Can inject emitter methods directly into other classes for easy usage.

License

MIT