Mittt
Tiny functional event emitter / pubsub.
Forked from https://github.com/developit/mitt
New project created from TSDX CLI.
Mittt 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 mittt
Then with a module bundler like rollup or webpack, use as you would anything else:
import mittt from 'mittt'
var mittt = require('mittt')
The ESM build is also available on unpkg:
<script type="module" src="https://unpkg.com/mittt/dist/mittt.esm.js"></script>
Usage
import mittt from 'mittt'
const emitter = mittt()
function onEvent(eventType, payload) {
console.log(eventType, payload)
}
emitter.on('foo', onEvent)
emitter.emit('foo')
const payload = { a: 'b' }
emitter.emit('bar', payload)
emitter.on('foo', onEvent)
emitter.off('foo', onEvent)
const emitter = mittt({
foo: [
(eventType, payload) => {
console.log(eventType, payload)
},
(eventType, payload) => {
console.log(eventType, payload)
},
],
})
emitter.emit('foo')
TypeScript
import mittt, { Emitter, EventHandler } from 'mittt'
const emitter: Emitter = mittt()
let foo: EventHandler = (eventType, payload) => {}
emitter.on('foo', foo)