mitt README

The project's README from github.com/developit/mitt. As one markdown file: README.md.

mitt
npm build status gzip size

Mitt

Tiny 200b functional event emitter / pubsub.

Mitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.

Table of Contents

Install

This project uses node and npm. Go check them out if you don't have them locally installed.

$ npm install --save mitt

Then with a module bundler like rollup or webpack, use as you would anything else:

// using ES6 modules
import mitt from 'mitt'

// using CommonJS modules
var mitt = require('mitt')

The UMD build is also available on unpkg:

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

You can find the library on window.mitt.

Usage

import mitt from 'mitt'

const emitter = mitt()

// listen to an event
emitter.on('foo', e => console.log('foo', e) )

// listen to all events
emitter.on('*', (type, e) => console.log(type, e) )

// fire an event
emitter.emit('foo', { a: 'b' })

// clearing all events
emitter.all.clear()

// working with handler references:
function onFoo() {}
emitter.on('foo', onFoo)   // listen
emitter.off('foo', onFoo)  // unlisten

Typescript

Set "strict": true in your tsconfig.json to get improved type inference for mitt instance methods.

import mitt from 'mitt';

type Events = {
  foo: string;
  bar?: number;
};

const emitter = mitt<Events>(); // inferred as Emitter<Events>

emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'

emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)

Alternatively, you can use the provided Emitter type:

import mitt, { Emitter } from 'mitt';

type Events = {
  foo: string;
  bar?: number;
};

const emitter: Emitter<Events> = mitt<Events>();

Examples & Demos

Preact + Mitt Codepen Demo
preact + mitt preview

API

Table of Contents

mitt

Mitt: Tiny (~200b) functional event emitter / pubsub.

Returns Mitt

all

A Map of event names to registered handler functions.

on

Register an event handler for the given type.

Parameters

off

Remove an event handler for the given type. If handler is omitted, all handlers of the given type are removed.

Parameters

emit

Invoke all handlers for the given type. If present, '*' handlers are invoked after type-matched handlers.

Note: Manually firing '*' handlers is not supported.

Parameters

Contribute

First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.

Reporting Issues

Found a problem? Want a new feature? First of all see if your issue or idea has already been reported. If don't, just open a new clear and descriptive issue.

Submitting pull requests

Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.

License

MIT License © Jason Miller

mitt (developit/mitt)

Searching Database…

11.9K ·GitHub
这是什么?Mitt 是一个体积极小的 JavaScript 函数式事件发射器(发布/订阅)库,gzip 压缩后不到 200 字节。
它是像 Node 那样基于类的 EventEmitter,还是有所不同?它是函数式的,而不是基于类的:方法不依赖 'this',因此无需绑定处理函数。不过它沿用了 Node 的 EventEmitter 相同的方法名和设计思路。
我该怎样把它加入项目?可以通过 npm 安装,并用 rollup、webpack 等模块打包工具进行打包;也可以直接从 unpkg 加载 UMD 版本,加载后它会暴露为 window.mitt。
使用它还需要其他库吗?不需要,它没有任何依赖,可在任何 JavaScript 运行环境中运行,包括 IE9+。
通配符事件监听器有什么需要注意的地方吗?触发事件时,通配符 '*' 处理函数会在与事件类型匹配的处理函数之后执行;而且你无法手动触发 '*' 处理函数。
它只能在浏览器中使用吗?它最初是为浏览器设计的,但由于没有依赖,它也可以在任何 JavaScript 运行环境中使用。
怎样一次监听所有事件?为特殊的事件类型 '*' 注册一个处理函数,它相当于通配符,可以监听所有被触发的事件。
TypeScript 有专门的支持吗?有。在 tsconfig.json 中设置 strict: true 可改善类型推断,也可以直接使用提供的 Emitter 类型。
怎样移除监听器?调用 off(type, handler);如果省略 handler,则会一次性移除该事件类型的所有处理函数。
这个项目为什么叫 mitt?根据 README,作者只是喜欢 'mitt' 这个名字还没有被用作包名。
Creator
developit
Size
0.2 MB
Stars
11.9K
Age
9y
Commits · 90 Days