README.md
This file
Path
README.md
82 lines · 2.4 KiB · Markdown
Last changed by
Spruce up the readme 🌲Jason Millerbefore adoption44362fe
History of this fileTake it
Spruce up the readme 🌲Jason Millerbefore adoption44362fe
lines 1–7why, one step back1<p align="center">2 <img src="https://i.imgur.com/BqsX9NT.png" width="256" height="256" alt="mitt">3 <br>4 <b>Mitt</b>: tiny 200b functional event emitter / pubsub.5 <br>6 <a href="https://www.npmjs.org/package/mitt"><img src="https://img.shields.io/npm/v/mitt.svg?style=flat" alt="npm"></a> <a href="https://travis-ci.org/developit/mitt"><img src="https://travis-ci.org/developit/mitt.svg?branch=master" alt="travis"></a>7</p>89Spruce up the readme 🌲Jason Millerbefore adoption44362fe
line 10why, one step back10## Why Mitt?11Spruce up the readme 🌲Jason Millerbefore adoption44362fe
lines 12–16why, one step back12- **Microscopic:** weighs less than 200 bytes gzipped13- **Useful:** a wildcard `"*"` event type listens to all events14- **Famliar:** same names & ideas as [Node's EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)15- **Functional:** methods don't rely on `this`16- **Great Name:** somehow [mitt](https://npm.im/mitt) wasn't taken17Spruce up the readme 🌲Jason Millerbefore adoption44362fe
line 18why, one step back18Initial commit! 🚀Jason Millerbefore adoptione77a42c
lines 19–2119* * *2021Spruce up the readme 🌲Jason Millerbefore adoption44362fe
lines 22–43why, one step back22## Usage2324After installing via `npm install --save mitt`:2526```js27import mitt from 'mitt'2829let emitter = mitt()3031// listen to an event32emitter.on('foo', e => console.log('foo', e) )3334// listen to all events35emitter.on('*', (type, e) => console.log(type, e) )3637// fire an event38emitter.emit('foo', { a: 'b' })3940// working with handler references:41function onFoo() {}42emitter.on('foo', onFoo) // listen43emitter.off('foo', onFoo) // unlistenInitial commit! 🚀Jason Millerbefore adoptione77a42c
lines 44–8244```4546* * *4748## API4950### mitt5152Mitt: Tiny (~200b) functional event emitter / pubsub.5354Returns **Mitt** 5556#### on5758Register an event handler for the given type.5960**Parameters**6162- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all events63- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to the given event6465#### off6667Remove an event handler for the given type.6869**Parameters**7071- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"`72- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove7374#### emit7576Invoke all handlers for the given type.77If present, `"*"` handlers are invoked prior to type-matched handlers.7879**Parameters**8081- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke82- `event` **\[Any]** An event object, passed to each handler