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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@kinngyo/event-emitter

v0.0.5

Published

轻量事件派发器,支持基础事件监听、一次性监听、移除监听,以及 `all` / `any` 组合事件

Downloads

82

Readme

@kinngyo/event-emitter

轻量事件派发器,支持基础事件监听、一次性监听、移除监听,以及 all / any 组合事件。

安装

npm install @kinngyo/event-emitter
yarn add @kinngyo/event-emitter

导入

import EventEmitter from '@kinngyo/event-emitter'

CommonJS:

const EventEmitterModule = require('@kinngyo/event-emitter')
const EventEmitter = EventEmitterModule.default || EventEmitterModule

基本使用

import EventEmitter from '@kinngyo/event-emitter'

const emitter = new EventEmitter()

emitter.on('message', data => {
    console.log(data)
})

emitter.emit('message', { text: 'hello' })

绑定 this

构造函数可以传入 thisArg,监听函数执行时会使用它作为 this

const context = { name: 'event-context' }
const emitter = new EventEmitter(context)

emitter.on('ready', function () {
    console.log(this.name)
})

emitter.emit('ready')

API

new EventEmitter(thisArg?)

创建事件派发器实例。

| 参数 | 类型 | 必填 | 说明 | | ---- | ---- | ---- | ---- | | thisArg | unknown | 否 | 监听函数执行时的 this |

emitter.on(target, callback)

注册事件监听。

emitter.on('change', value => {
    console.log(value)
})

target 支持字符串、Symbol,也支持数组批量注册。

emitter.on(['open', 'close'], event => {
    console.log(event)
})

emitter.once(target, callback)

注册只触发一次的监听。

emitter.once('ready', data => {
    console.log(data)
})

emitter.emit('ready', 1)
emitter.emit('ready', 2)

上面代码只会输出 1

emitter.emit(target, ...params)

触发事件。

emitter.emit('change', 1, 2, 3)

监听函数会按注册顺序执行。

emitter.on('change', (a, b, c) => {
    console.log(a, b, c)
})

emitter.emit('change', 1, 2, 3)

target 也支持数组,表示依次触发多个事件。

emitter.emit(['open', 'close'], 'done')

emitter.off(target?, callback?)

移除监听。

不传参数时,移除全部监听。

emitter.off()

只传 target 时,移除该事件的全部监听。

emitter.off('change')

同时传 targetcallback 时,只移除匹配的监听。

const listener = data => {
    console.log(data)
}

emitter.on('change', listener)
emitter.off('change', listener)

once 注册的监听也可以通过原 callback 移除。

const listener = data => {
    console.log(data)
}

emitter.once('change', listener)
emitter.off('change', listener)

组合事件

EventEmitter.all(targets, callback)

等待所有目标事件都触发后执行回调。

const task = EventEmitter.all(['user', 'config'], (user, config) => {
    console.log(user, config)
})

task.emit('user', { id: 1 })
task.emit('config', { theme: 'dark' })

回调参数顺序与 targets 顺序一致。

EventEmitter.any(targets, callback)

任意一个目标事件触发后执行回调。

const task = EventEmitter.any(['success', 'error'], result => {
    console.log(result)
})

task.emit('success', 'ok')

allany 返回的是轻量派发对象,只提供 emit 方法。

any 的组合事件只会等待事件触发成功,不包含失败分支;因此内部使用“第一个完成”的语义实现,保持 ES2020 产物目标。

类型

type EventEmitterTarget = string | symbol | Array<string | symbol>

type EventEmitterStaticTarget = Array<string | symbol>

type EventEmitterCallback = (...args: unknown[]) => void

interface EventEmitterTrigger {
    emit(target: EventEmitterTarget, ...params: unknown[]): void
}

注意事项

  • target 必须是 stringsymbol 或它们组成的数组。
  • off(target, callback) 每次只移除一个匹配的监听。
  • emit 触发不存在的事件时不会报错。
  • EventEmitter.version 是构建时写入的版本号。
  • 当前构建目标为 ES2020。