# emitter-pro

> 一个简单的 Javascript 事件管理，支持浏览器端和 node 端。

Latest version **1.2.4** (published 2026-01-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install emitter-pro
pnpm add emitter-pro
yarn add emitter-pro
bun add emitter-pro
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.2.4 |
| Published | 2026-01-28 |
| First published | 2023-01-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 56.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | caijf |
| Maintainers | caijf |
| Keywords | event, emitter, eventEmitter, listener, observer, subscribe |

## Links

- npm: https://www.npmjs.com/package/emitter-pro
- Repository: https://github.com/caijf/emitter-pro
- Homepage: https://github.com/caijf/emitter-pro#readme
- Issues: https://github.com/caijf/emitter-pro/issues
- npm.io page: https://npm.io/package/emitter-pro

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.2.4 (latest) — 2026-01-28
- 1.2.3 — 2026-01-15
- 1.2.2 — 2024-10-11
- 1.2.1 — 2024-04-16
- 1.2.0 — 2024-04-07
- 1.1.0 — 2024-04-07
- 1.0.3 — 2023-06-27
- 1.0.2 — 2023-04-26
- 1.0.1 — 2023-01-13
- 1.0.0 — 2023-01-13
- 0.0.1 — 2023-01-13
- 0.0.0 — 2023-01-13

## README

# emitter-pro

[![npm][npm]][npm-url] [![codecov](https://codecov.io/gh/caijf/emitter-pro/graph/badge.svg?token=PC2MBY6NQL)](https://codecov.io/gh/caijf/emitter-pro) ![GitHub](https://img.shields.io/github/license/caijf/emitter-pro.svg)

一个简单的 Javascript 事件管理，支持浏览器端和 node 端。

## 使用

### 安装

```shell
npm install emitter-pro
```

```shell
yarn add emitter-pro
```

```shell
pnpm add emitter-pro
```

### 示例

```typescript
import Emitter from 'emitter-pro';

const emitter = new Emitter();

// 注册监听方法
emitter.on('foo', () => console.log('bar'));
emitter.on('foo', () => console.log('baz'));
emitter.on('foo', () => console.log(42));

// 触发方法
emitter.emit('foo');

// 取消监听
emitter.off('foo');
```

## 实例方法

### on(eventName: string|symbol, listener: F, context?: object | null)

注册监听方法。允许多次添加同一引用的函数。

返回当前实例。

```typescript
const emitter = new Emitter();

// 注册监听方法
emitter.on('foo', () => console.log('bar'));
emitter.on('foo', () => console.log(42));

emitter.emit('foo');
// bar
// 42
```

### emit(eventName: string|symbol, ...args: Parameters<F>)

触发监听方法。

返回当前实例。

```typescript
const emitter = new Emitter();

emitter.on('foo', () => console.log('bar'));
emitter.on('foo', () => console.log(42));

emitter.emit('foo');
// bar
// 42

// 支持传入参数
emitter.on('test' (a, b) => console.log(a + b));
emitter.on('test' (a, b) => console.log(a * b));

emitter.emit('test', 2, 5);
// 7
// 10

emitter.emit('test', 5, 5);
// 10
// 25
```

### off(eventName: string|symbol, listener?: F)

取消监听方法。如果不传第二个参数，将取消该事件名称的全部监听方法。如果多次添加同一引用的函数，需要多次删除。

返回当前实例。

```typescript
const emitter = new Emitter();

const fn = () => console.log('bar');

emitter.on('foo', fn);
emitter.on('foo', () => console.log('baz'));
emitter.on('foo', () => console.log(42));

emitter.emit('foo');
// bar
// baz
// 42

emitter.off('foo', fn); // 取消 foo 的监听方法 fn
emitter.emit('foo');
// bar
// 42

emitter.off('foo'); // 取消 foo 的全部监听方法
emitter.emit('foo'); // 什么都没发生
```

### once(eventName: string|symbol, listener: F, context?: object | null)

仅触发一次的监听方法。使用方法同 `on` 。

返回当前实例。

```typescript
const emitter = new Emitter();

// 注册监听方法
emitter.on('foo', () => console.log('bar'));
emitter.once('foo', () => console.log(42));

emitter.emit('foo');
// bar
// 42

emitter.emit('foo');
// bar
```

### offAll()

取消全部事件名称的监听方法。

返回当前实例。

```typescript
const emitter = new Emitter();

const fn = () => console.log('bar');
emitter.on('test', fn);
emitter.on('test', () => console.log('baz'));
emitter.on('test', () => console.log(42));

emitter.on('other', fn);
emitter.on('other', () => console.log('baz'));

emitter.offAll(); // 取消全部监听方法

emitter.emit('test'); // 什么都没发生
emitter.emit('other'); // 什么都没发生
```

### eventNames()

获取全部事件名称。

返回事件名称数组。

```typescript
const emitter = new Emitter();

const fn = () => console.log('bar');
emitter.on('test', fn);
emitter.on('other', fn);

console.log(emitter.eventNames()); // ["test", "other"]
```

### listeners(eventName: string|symbol)

获取事件名称的全部监听方法（如通过 `once` 方法注册，返回的是包装方法）。

返回事监听方法数组。

```typescript
const emitter = new Emitter();

const fn1 = () => console.log('bar');
const fn2 = () => console.log('baz');
emitter.on('test', fn1);
emitter.once('test', fn2);

console.log(emitter.listeners('test')); // [fn1, wrapFn2]
```

### rawListeners(eventName: string|symbol)

获取事件名称的全部监听方法（原始方法，未经过包装处理）。

返回对应事件名称的监听方法数组。

```typescript
const emitter = new Emitter();

const fn1 = () => console.log('bar');
const fn2 = () => console.log('baz');
emitter.on('test', fn1);
emitter.once('test', fn2);

console.log(emitter.rawListeners('test')); // [fn1, fn2]
```

### hasListener(eventName: string|symbol, listener: F)

判断监听方法是否存在。

内部使用 `rawListeners` 获取原始方法进行判断。

```typescript
const emitter = new Emitter();

const fn1 = () => console.log('bar');
const fn2 = () => console.log('baz');
emitter.on('test', fn1);
emitter.once('test', fn2);

emitter.hasListener('test', fn1); // true
emitter.hasListener('test', fn2); // true

emitter.emit('test');

emitter.hasListener('test', fn1); // true
emitter.hasListener('test', fn2); // false
```

### prependListener(eventName: string|symbol, listener: F, context?: object | null)

注册监听方法。同 `on` 方法，只是将监听方法添加到最前面（事件触发是按顺序执行）。

返回当前实例。

```typescript
const emitter = new Emitter();

// 注册监听方法
emitter.on('foo', () => console.log('bar'));
emitter.prependListener('foo', () => console.log(42));

emitter.emit('foo');
// 42
// bar
```

### prependOnceListener(eventName: string|symbol, listener: F, context?: object | null)

仅触发一次的监听方法。同 `once` 方法，只是添加到最前面（事件触发是按顺序执行）。

返回当前实例。

```typescript
const emitter = new Emitter();

// 注册监听方法
emitter.on('foo', () => console.log('bar'));
emitter.prependOnceListener('foo', () => console.log(42));

emitter.emit('foo');
// 42
// bar

emitter.emit('foo');
// bar
```

[npm]: https://img.shields.io/npm/v/emitter-pro.svg
[npm-url]: https://npmjs.com/package/emitter-pro

---
_Source: https://npm.io/package/emitter-pro · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
