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 🙏

© 2025 – Pkg Stats / Ryan Hefner

yang-eventemitter

v1.0.6

Published

A lightweight event emitter in TypeScript

Readme

📘 使用文档 - yang-eventemitter

yang-eventemitter 是一个用 TypeScript 编写的轻量级事件发布订阅工具,适合前端和 Node.js 项目使用,支持注册、触发、取消监听器等功能。


📦 安装

npm install yang-eventemitter

yarn add yang-eventemitter

🔰 快速开始

引入并初始化

import EventEmitter from 'yang-eventemitter';

const emitter = new EventEmitter();

🔄 注册事件

使用 on 方法注册事件监听器:

emitter.on('login', (username) => {
  console.log(`${username} 登录成功`);
});

你可以注册多个事件或多个监听器:

emitter.on('login', () => console.log('另一个登录监听器'));
emitter.on('logout', () => console.log('退出事件监听器'));

🚀 触发事件

使用 emit 方法触发事件:

emitter.emit('login', '张三');
// 输出:
// 张三 登录成功
// 另一个登录监听器

你可以传递任意数量的参数:

emitter.on('message', (from, text) => {
  console.log(`${from} 说:${text}`);
});

emitter.emit('message', '李四', '你好呀');

❌ 取消监听器

使用 off 方法移除指定监听器:

function onLogin() {
  console.log('登录了');
}

emitter.on('login', onLogin);
emitter.off('login', onLogin);

emitter.emit('login'); // 没有输出

注意:off 只能移除注册时使用的同一个函数引用。


🧱 类型定义(TypeScript)

type EventCallback = (...args: any[]) => void;

支持自动类型推导和参数传递。


🌍 应用场景

  • 前端组件通信(无第三方依赖)
  • 跨模块事件派发
  • Node.js 脚本消息通知
  • 简易状态管理中间层

📌 示例:计数器模块通信

// counter.ts
import EventEmitter from 'yang-eventemitter';

export const counterEmitter = new EventEmitter();

export function increment() {
  counterEmitter.emit('change', 1);
}

// ui.ts
import { counterEmitter } from './counter';

counterEmitter.on('change', (step: number) => {
  console.log(`增加了 ${step}`);
});