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

event-bus-common

v1.2.1

Published

A powerful and flexible event bus library for TypeScript

Readme

Event Bus

一个强大的 TypeScript 事件总线库,支持事件管理、状态管理和事件分组。

特性

  • 基础事件发布/订阅
  • 事件优先级
  • 一次性事件订阅
  • 通配符(全局)事件订阅
  • 异步事件处理
  • 错误捕获和处理
  • 事件分组管理
  • 事件历史记录
  • 状态管理
  • 事件过滤器
  • 事件总线快照
  • 实用工具函数(节流、防抖、Promise 转事件)

安装

npm install event-bus-common --save

使用

基础用法

import { EventBus, EventPriority } from 'event-bus-common';

const eventBus = new EventBus();

// 订阅事件
eventBus.on('userCreated', (user) => {
  console.log('新用户创建:', user);
});

// 发布事件
eventBus.emit('userCreated', { id: 1, name: '张三' });

事件优先级

const results: number[] = [];

eventBus.on('test', () => { void results.push(3); }, EventPriority.Low);
eventBus.on('test', () => { void results.push(1); }, EventPriority.High);
eventBus.on('test', () => { void results.push(2); }, EventPriority.Normal);
eventBus.on('test', () => { void results.push(0); }, EventPriority.Critical);

eventBus.emit('test');
// 结果: [0, 1, 2, 3]

一次性订阅

eventBus.once('oneTime', (data) => {
  console.log('只触发一次:', data);
});

通配符订阅

eventBus.onAny(({ event, data }) => {
  console.log(`收到事件 ${event}:`, data);
});

事件分组

import { EventManager } from 'event-bus-common';

const manager = new EventManager();

// 创建事件组
manager.createGroup('userEvents', {
  events: ['userCreated', 'userUpdated', 'userDeleted'],
  description: '用户相关事件'
});

// 订阅组内所有事件
manager.onGroup('userEvents', (event, data) => {
  console.log(`用户事件 ${event}:`, data);
});

状态管理

const manager = new EventManager({
  enableStateManagement: true
});

// 设置状态
manager.setState('user', { id: 1, name: '张三' });

// 监听状态变化
manager.onStateChange('user', (newState) => {
  console.log('用户状态更新:', newState);
});

事件历史记录

const eventBus = new EventBus({
  maxHistorySize: 100
});

// 获取历史记录
const history = eventBus.getHistory();

事件过滤器

const eventBus = new EventBus();

// 使用过滤器订阅事件
eventBus.on('userCreated', (user) => {
  console.log('新用户创建:', user);
}, {
  filter: (data) => data.age > 18
});

事件总线快照

const eventBus = new EventBus();

// 获取快照
const snapshot = eventBus.getSnapshot();

// 从快照恢复
eventBus.restoreFromSnapshot(snapshot);

实用工具

import { EventUtils } from 'event-bus-common';

// 节流
const throttledFn = EventUtils.throttle((data) => {
  console.log('节流函数:', data);
}, 1000);

// 防抖
const debouncedFn = EventUtils.debounce((data) => {
  console.log('防抖函数:', data);
}, 1000);

// Promise 转事件
const promise = fetch('/api/data');
EventUtils.promiseToEvent(promise, 'dataLoaded', { source: 'api' });

API 文档

EventBus

  • on(event: string, handler: EventHandler, priority?: EventPriority): EventSubscription
  • once(event: string, handler: EventHandler, priority?: EventPriority): EventSubscription
  • onAny(handler: EventHandler<{ event: string; data: any }>): EventSubscription
  • off(event: string): void
  • emit(event: string, data?: any): void | Promise<void>
  • clear(): void
  • getSnapshot(): EventBusSnapshot
  • restoreFromSnapshot(snapshot: EventBusSnapshot): void
  • getHistory(): Array<{ event: string; data: any; timestamp: number }>
  • clearHistory(): void
  • getListenerCount(event: string): number
  • getWildcardListenerCount(): number
  • hasAnyListeners(): boolean
  • hasListeners(event: string): boolean
  • eventNames(): string[]
  • listenerCount(event: string): number
  • getState(): { eventCount: number; totalListeners: number; historySize: number; options: EventBusOptions }

EventManager

  • createGroup(name: string, options: { events: string[]; description?: string }): EventGroup
  • updateGroup(name: string, options: { events?: string[]; description?: string }): EventGroup
  • deleteGroup(name: string): void
  • onGroup(groupName: string, handler: (event: string, data: any) => void): EventSubscription
  • setState(key: string, value: any): void
  • getState(key: string): any
  • onStateChange(key: string, handler: (value: any) => void): EventSubscription
  • reset(): void
  • getSnapshot(): EventManagerSnapshot
  • restoreFromSnapshot(snapshot: EventManagerSnapshot): void

EventUtils

  • throttle<T extends (...args: any[]) => any>(fn: T, wait: number): (...args: Parameters<T>) => ReturnType<T>
  • debounce<T extends (...args: any[]) => any>(fn: T, wait: number): (...args: Parameters<T>) => ReturnType<T>
  • promiseToEvent<T>(promise: Promise<T>, eventName: string, metadata?: Record<string, any>): Promise<T>

许可证

MIT