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

mitt-pubsub

v1.0.2

Published

一个3KB的发布者/订阅者插件

Downloads

19

Readme

mitt-pubsub

一个3KB的发布者/订阅者插件

  • Microscopic(微小): 压缩后小于 3KB
  • Useful(实用): 支持通配符 "*" 监听所有事件
  • Familiar(熟悉): 使用与 Node 的 EventEmitter 相同的概念和命名
  • Functional(函数式): 方法不依赖于 this

mitt-pubsub是鉴于mitt上多了异步支持和once方法,并且修改了'*'方法的应用, 是为浏览器设计的,但也适用于任何 JavaScript 运行环境。它没有依赖项,并且支持 IE9+。

目录

安装

该项目使用 nodenpm,如果你本地没有安装,请先去下载安装。

$ npm install --save mitt-pubsub

然后在像 rollupwebpack 这样的模块打包工具中,可以像其他模块一样使用:

// 使用 ES6 模块
import mitt from 'mitt-pubsub'

// 使用 CommonJS 模块
var mitt = require('mitt')

UMD 构建版本也通过 unpkg 提供:

<script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>

你可以在 window.mitt 中找到该库。

使用方式

import mitt from 'mitt'

const emitter = new mitt();

// 监听某个事件
emitter.on('foo', e => console.log('foo', e) )

// 监听一次某个事件
emitter.once("foo1", (e) => {console.log('foo', e)});

// 触发一个事件
emitter.emit('foo', { a: 'b' })

// 触发多个事件
emitter.emit(['foo','too'], { a: 'b' })

// 给所有事件发送消息
emitter.emit('*', { a: 'b' } )

// 清除所有事件
emitter.clear()

// 使用 handler 引用:
function onFoo() {}
emitter.on('foo', onFoo)   // 添加监听器
emitter.off('foo', onFoo)  // 移除监听器

API 文档

表格目录

mitt

Mitt:一个小型(约 2.5kb 字节)的函数式事件发射器 / 发布订阅模型。

返回 Mitt

all

一个 Map,键是事件名,值是注册的处理函数。

on

为指定类型的事件注册一个处理函数。

once

为指定类型的事件注册一个只执行一次的处理函数。

参数

  • type (string | symbol) 要监听的事件类型,或 '*' 表示监听所有事件
  • handler Function 响应事件时调用的函数

off

移除指定类型的事件处理函数。 如果省略 handler,则会移除该类型下的所有处理函数。

参数

  • type (string | symbol) 要移除 handler 的事件类型,或 '*'
  • handler Function? 要移除的处理函数

emit

触发指定类型的事件的所有处理函数。 如果存在,'*' 处理函数会在匹配类型的处理函数之后被调用。

注意:手动触发 '*' 处理函数不受支持。

参数

  • type (string | symbol) 要触发的事件类型
  • evt 任意类型? 可以传入任意值(推荐对象),并传递给每个处理函数