@kinngyo/event-emitter
v0.0.4
Published
事件派发
Downloads
3
Readme
event-emitter
事件派发
安装
npm i @kinngyo/event-emitter
或者
yarn add @kinngyo/event-emitter
基本使用
import EventEmitter from '@kinvirgo/event-emitter'
const event = new EventEmitter()
// 监听
event.on('test', data => {
console.log(data) // 123
})
// 触发
event.emit('test', 123)
EventEmitter
const emitter = new EventEmitter(thisArg?:unknown)
API
| 方法 | 类型 | 介绍 | | ---- | ---------------------------------------------------------------------- | ----------------------- | | on | (target: EventEmitterTarget, cb: EventEmitterCallback)=>EventEmitter | 监听 target 触发 cb | | once | (target: EventEmitterTarget, cb: EventEmitterCallback)=>EventEmitter | 监听 target 触发一次 cb | | emit | (target: EventEmitterTarget, ...params:unknown[])=>void | 触发 target | | off | (target?: EventEmitterTarget, cb?: EventEmitterCallback)=>EventEmitter | 取消 target |
EventEmitter.all
- 所有 target
eventEmit.emit(target)
,执行回调
const eventEmit = new EventEmitter.all(target:EventEmitterStaticTarget, cb: EventEmitterCallback)
API
| 方法 | 类型 | 介绍 | | ---- | ------------------------------------------------------- | ----------- | | emit | (target: EventEmitterTarget, ...params:unknown[])=>void | 触发 target |
EventEmitter.any
- 任何一个 target
eventEmit.emit(target)
,执行回调
const eventEmit = new EventEmitter.all(target:EventEmitterStaticTarget, cb: EventEmitterCallback)
API
| 方法 | 类型 | 介绍 | | ---- | ------------------------------------------------------- | ----------- | | emit | (target: EventEmitterTarget, ...params:unknown[])=>void | 触发 target |
types
type EventEmitterTarget = string | symbol | Array<string | symbol>
type EventEmitterStaticTarget = Array<string | symbol>
interface EventEmitterCallback {
(...args: unknown[]): void
cb?: (...args: unknown[]) => void
}