@dominicstop/ts-event-emitter

中文文档
1.1.0发布于 2021/08/24MITTypeScriptESM + CJS零依赖近 30 天下载 77万

Yet another event emitter written in typescript.

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Basic Usage
  4. In-Depth Usage
  5. Documentation
  6. TSEvenEmitter

ts-event-emitter

(yet another) event emitter written in typescript 😔


Installation

# install via npm...
npm install @dominicstop/ts-event-emitter

# or install via yarn.
yarn add @dominicstop/ts-event-emitter

Usage

Basic Usage

type EventKeys = 'Foo' | 'Baz';

export const emitter: TSEventEmitter<EventKeys, {
  Foo: string;
  Baz: { age: number } | null;
}> = new TSEventEmitter();

emitter.once('Foo', (event) => { console.log(event) });
emitter.emit('Foo', 'hello world');

emitter.once('Baz', (event) => { console.log(event?.age) });
emitter.emit('Baz', { age: 23 });

In-Depth Usage

// 1. Create a string enum...
enum Events { Foo = 'Foo', Bar = 'Bar', Baz = 'Baz' };

// 1.1. ...or alt., a union of strings (whichever you prefer).
type EventKeys = 'Foo' | 'Bar' | 'Baz';

// 2. Create the emitter + event map...
// * For each key in the event enum, define the type of the 
//    `event` argument that the listener will receive.
// * As mentioned in 1.1. you can also use the `EventKeys` union 
//   in place of the `Events` enum.
export const emitter: TSEventEmitter<EventKeys, {

  // 2.1. For the event `TestEnum.Foo`, this will be its event object.
  Foo: { name: string },

  // 2.2. If you don't want to pass an event argument, 
  //      then set it to `null` or `undefined`.
  // * This means the event listener won't receive any arguments
  //   and you can't pass a data argument to `emit` (see step 4.2.)
  Bar: null, 

  // 2.3. or alt., you can make the event param "optional".
  Baz: null | { age: number }
}> = new TSEventEmitter();

// 3.1. `event` arg. will be inferred as `(event: { name: string }) => void`
emitter.once('Foo', (event) => { console.log(event.name) });

// 3.2.`event` arg. will be inferred as `() => void`
// * Note: If you try to add an event param, TS produces an error.
emitter.once('Bar', () => { /** no-op */ });

// 3.3. `event` will be inferred as 
//      `(event: { age: number } | null) => void`.
// * As such, we need to use the optional chaining operator to access `event`.
emitter.once(Events.Baz, (event) => { console.log(event?.age) });
   
// 4. Next, lets broadcast some events.
emitter.emit(Events.Foo, { name: 'd'});
emitter.emit('Baz', { age: 1 });
 
// 4.1. Note: You can't pass a data argument with the 'Bar' event
// because in step 2.2., the event object is defined as `null`.
emitter.emit('Bar');
 
// 4.2. We must explicitly pass a null/undefined value for the data
// argument since it can be optional (e.g. step 2.3.)
emitter.emit('Baz', null);

Documentation

TSEvenEmitter

Property/MethodDescription
🔤 addListener

⚛️ (eventKey, listener) => { unsubscribe: () => void }
🔤 removeListener

⚛️ (eventKey, listenerToRemove) => void
🔤 once

⚛️ (eventKey, listener) => void
🔤 removeAllListeners

⚛️ () => void
🔤 emit

⚛️ (eventKey, data) => void

版本与发布记录

@dominicstop/ts-event-emitter 共发布 12 个版本,首次发布于 2021/08/15;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2021/08/245 年前
1.0.92021/08/165 年前
1.0.82021/08/165 年前
1.0.72021/08/165 年前
1.0.62021/08/165 年前
1.0.52021/08/165 年前

关于 @dominicstop/ts-event-emitter 的常见问题

@dominicstop/ts-event-emitter 是什么?

Yet another event emitter written in typescript.

如何安装 @dominicstop/ts-event-emitter?

在项目目录运行 npm install @dominicstop/ts-event-emitter,或使用 pnpm add / yarn add / bun add。

@dominicstop/ts-event-emitter 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dominicstop/ts-event-emitter 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@dominicstop/ts-event-emitter 可以商用吗?

@dominicstop/ts-event-emitter 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dominicstop/ts-event-emitter 的下载量有多少?

根据 npm 官方下载统计,@dominicstop/ts-event-emitter 在 2026/10/13 – 2026/11/11 这 30 天内被下载 769,508 次,日均约 25,650 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dominicstop/ts-event-emitter 还在维护吗?

最新版本 1.1.0 发布于 2021/08/24(5 年前);至今共发布 12 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/08/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。