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
What is this?Mitt is a tiny functional event emitter / pubsub library for JavaScript, weighing less than 200 bytes gzipped.
Is this a class-based EventEmitter like Node's, or something different?It's functional rather than class-based: methods don't rely on 'this', so you don't need to bind handlers. It mimics the same names and ideas as Node's EventEmitter though.
How do I add it to a project?Install it via npm and bundle it with a module bundler like rollup or webpack, or load the UMD build directly from unpkg where it's exposed as window.mitt.
Do I need any other libraries to use it?No, it has zero dependencies and works in any JavaScript runtime, including IE9+.
Any gotchas with the wildcard event listener?The wildcard '*' handlers are invoked after the type-matched handlers when you emit an event, and you can't manually fire '*' handlers yourself.
Does it only work in the browser?It was originally made for the browser but works in any JavaScript runtime since it has no dependencies.
How do I listen to all events at once?Register a handler for the special '*' event type, which acts as a wildcard listening to every emitted event.
Does TypeScript get special support?Yes, set strict: true in tsconfig.json for improved type inference, or use the provided Emitter type directly.
How do I remove a listener?Call off(type, handler); if you omit the handler, all handlers for that event type are removed at once.
Why is the project called mitt?According to the README, the author simply liked that 'mitt' wasn't already taken as a package name.
Creator
developit
Size
0.2 MB
Stars
11.9K
Age
9y
Commits · 90 Days