README.md
This file
Path
README.md
73 lines · 1.9 KiB · Markdown
Last changed by
Merge pull request #4 from esebastian/sample-usageJason Millerbefore adoptionbf03623
History of this fileTake it
Initial commit! 🚀Jason Millerbefore adoptione77a42c
lines 1–541# `mitt`23[](https://www.npmjs.org/package/mitt)4[](https://travis-ci.org/developit/mitt)56**Tiny (~200b) functional event emitter / pubsub.**78**It's tiny:** no dependencies and only **190 bytes** when gzipped _(250b without)_.910* * *1112## Installation1314```sh15npm install --save mitt16```1718* * *1920## API2122### mitt2324Mitt: Tiny (~200b) functional event emitter / pubsub.2526Returns **Mitt** 2728#### on2930Register an event handler for the given type.3132**Parameters**3334- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all events35- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to the given event3637#### off3839Remove an event handler for the given type.4041**Parameters**4243- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"`44- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove4546#### emit4748Invoke all handlers for the given type.49If present, `"*"` handlers are invoked prior to type-matched handlers.5051**Parameters**5253- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke54- `event` **\[Any]** An event object, passed to each handlerMerge pull request #4 from esebastian/sample-usageJason Millerbefore adoptionbf03623
lines 55–73why, one step back5556* * *5758## Sample usage5960```es661import mitt from 'mitt'6263let emitter = mitt()6465// listen to an event66emitter.on('foo', e => console.log('foo', e) )6768// listen to all events69emitter.on('*', (type, e) => console.log(type, e) )7071// fire an event72emitter.emit('foo', { a: 'b' })73```