README.md
This file
Path
README.md
95 lines · 3.2 KiB · Markdown
Last changed by
remove "drop support for emitting multiple args" thing as per https://github.com/developit/mitt/pull/19#issuecomment-273344689tunnckoCorebefore adoptionf4c60f4
History of this fileTake it
README.md
Mitt: tiny 200b functional event emitter / pubsub.
Why Mitt?
- Microscopic: weighs less than 200 bytes gzipped
- Useful: a wildcard
"*"event type listens to all events - Familiar: same names & ideas as Node's EventEmitter
- Functional: methods don't rely on
this - Great Name: somehow mitt wasn't taken
Mitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.
Examples & Demos
Preact + Mitt Codepen Demo
Usage
After installing via npm install --save mitt:
import mitt from 'mitt'
let 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' })
// working with handler references:
function onFoo() {}
emitter.on('foo', onFoo) // listen
emitter.off('foo', onFoo) // unlisten
API
mitt
Mitt: Tiny (~200b) functional event emitter / pubsub.
Returns Mitt
emit
Invoke all handlers for the given type.
If present, "*" handlers are invoked prior to type-matched handlers.
Parameters
typeString The event type to invokeevt[Any] Any value (object is recommended and powerful), passed to each handler
Returns Object the mitt instance for chaining
on
Register an event handler for the given type.
Parameters
typeString Type of event to listen for, or"*"for all eventshandlerFunction Function to call in response to given event
Returns Object the mitt instance for chaining
off
Remove an event handler for the given type.
Parameters
typeString Type of event to unregisterhandlerfrom, or"*"handlerFunction Handler function to remove
Returns Object the mitt instance for chaining