
Mitt: tiny 200b functional event emitter / pubsub.
- 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
Preact + Mitt Codepen DemoMitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.
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) // unlistenMitt: Tiny (~200b) functional event emitter / pubsub.
Returns Mitt
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 the given event
Remove an event handler for the given type.
Parameters
typeString Type of event to unregisterhandlerfrom, or"*"handlerFunction Handler function to remove
Invoke all handlers for the given type.
If present, "*" handlers are invoked prior to type-matched handlers.
Parameters
typeString The event type to invokeevent[Any] An event object, passed to each handler