# znu-event

> A simple event emitter

Latest version **0.0.10** (published 2020-12-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install znu-event
pnpm add znu-event
yarn add znu-event
bun add znu-event
```

## Health

**Score 30/100 (F)** — status: abandoned.

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.10 |
| Published | 2020-12-03 |
| First published | 2020-07-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 42.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | chenwuai |
| Maintainers | chenwuai |
| Keywords | simple, event, emitter, typescript |

## Links

- npm: https://www.npmjs.com/package/znu-event
- Repository: https://github.com/chenchenwuai/znu-event
- Homepage: https://github.com/chenchenwuai/znu-event#readme
- Issues: https://github.com/chenchenwuai/znu-event/issues
- npm.io page: https://npm.io/package/znu-event

## Recent versions

- 0.0.10 (latest) — 2020-12-03
- 0.0.9 — 2020-12-03
- 0.0.8 — 2020-10-27
- 0.0.7 — 2020-07-14
- 0.0.6 — 2020-07-14
- 0.0.5 — 2020-07-14
- 0.0.4 — 2020-07-10
- 0.0.1 — 2020-07-10

## README

## znu-event

🚀 a sample event emitter 🌈. 无依赖，TypeScript、rollup、自动测试

[GitHub](https://github.com/chenchenwuai/znu-event)
[NPM](https://www.npmjs.com/package/znu-event)
如果帮助到了你，请给我一个star~~~ 💖，如果你发现bug，请尽情的提issue。

## 安装
``` javascript
    npm i znu-event 
```

## 使用

``` javascript
import EventEmitter from  './znu-event';
const emitter = new EventEmitter();
emitter.on('add', data=>{
    console.log(data) // 1
});
emitter.emit('add', 1);
```
## 方法


### on(eventName, listener)
绑定事件

|名称|类型|数据类型|是否必填|说明|
|---|---|---|---|---|
|eventName| 参数 |`String/Symbol`|是|事件名称|
|listener| 参数 |`Function`|是|对应的回调函数|
|emitter| 返回值 |`EventEmitter`|---|实例|

### off(eventName, listener)
解除绑定, 如果不填写`listener`, 那么`eventName`对应的`listener`都会被移除.

|名称|类型|数据类型|是否必填|说明|
|---|---|---|---|---|
|eventName| 参数 |`String/Symbol`|是|事件名称|
|listener| 参数 |`Function`|是|对应的回调函数|
|emitter| 返回值 |`EventEmitter`|---|实例|

``` javascript
const callback = data=>{
    alert(data)
};
emitter.on('add', callback);
// 解除绑定
emitter.off('add', callback);
// add事件不会触发
emitter.emit('add', 1);
```

### once(eventName, listener)
绑定事件, 只触发一次

|名称|类型|数据类型|是否必填|说明|
|---|---|---|---|---|
|eventName| 参数 |`String/Symbol`|是|事件名称|
|listener| 参数 |`Function`|是|对应的回调函数|
|emitter| 返回值 |`EventEmitter`|---|实例|

``` javascript
const callback = data=>{
    alert(data)
};
emitter.once('add', callback);
// add事件触发
emitter.emit('add', 1);
// add事件不会触发
emitter.emit('add', 1);
```

### emit(eventName [, ...args])
触发事件, 支持任意数量参数

|名称|类型|数据类型|是否必填|说明|
|---|---|---|---|---|
|eventName| 参数 |`String/Symbol`|是|事件名称|
| ...args| 参数 |`Any`|是|对应的回调函数|
|emitter| 返回值 |`Boolean`|---|实例|

``` javascript
const callback = (a,b,c,d)=>{
    console(a,b,c,d); // 1,2,3,4
};
emitter.once('add', callback);
// add事件触发
emitter.emit('add', 1,2,3,4);
```

### has(eventName)
是否绑定了指定事件

|名称|类型|数据类型|是否必填|说明|
|---|---|---|---|---|
|eventName| 参数 |`String/Symbol`|是|事件名称|

``` javascript
emitter.on('add', callback);

emitter.has('add'); // true
```

### eventNames()
返回所有绑定事件名称

``` javascript
emitter.eventNames();
```

### offAll()
解除所有绑定事件

``` javascript
const callback = (a,b,c,d)=>{
    console(a,b,c,d); // 1,2,3,4
};
emitter.on('add', callback);
emitter.on('edf', callback);
emitter.offAll();

// add，edf事件不会触发
emitter.emit('add', 1,2,3,4);
emitter.emit('edf', 1,2,3,4);
```

### destroy()
销毁实例

``` javascript
const callback = (a,b,c,d)=>{
    console(a,b,c,d); // 1,2,3,4
};
emitter.once('add', callback);
emitter.destroy();

// add事件不会触发
emitter.emit('add', 1,2,3,4);
```

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