README.md
This file
Path
README.md
84 lines · 2.6 KiB · Markdown
Last changed by
Merge pull request #18 from pmg103/patch-1Jason Millerbefore adoption927ddfa
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–13why, one step back12- **Microscopic:** weighs less than 200 bytes gzipped13- **Useful:** a wildcard `"*"` event type listens to all eventsMerge pull request #18 from pmg103/patch-1Jason Millerbefore adoption927ddfa
line 14why, one step back14- **Familiar:** same names & ideas as [Node's EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)Spruce up the readme 🌲Jason Millerbefore adoption44362fe
lines 15–16why, one step back15- **Functional:** methods don't rely on `this`16- **Great Name:** somehow [mitt](https://npm.im/mitt) wasn't taken17Add a design note, hopefully addressing #10 👍Jason Millerbefore adoption0d51fe3
lines 18–19why, one step back18> Mitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and only uses basic language features. It probably works in Internet Explorer 5.19Spruce up the readme 🌲Jason Millerbefore adoption44362fe
line 20why, one step back20Initial commit! 🚀Jason Millerbefore adoptione77a42c
lines 21–2321* * *2223Spruce up the readme 🌲Jason Millerbefore adoption44362fe
lines 24–45why, one step back24## Usage2526After installing via `npm install --save mitt`:2728```js29import mitt from 'mitt'3031let emitter = mitt()3233// listen to an event34emitter.on('foo', e => console.log('foo', e) )3536// listen to all events37emitter.on('*', (type, e) => console.log(type, e) )3839// fire an event40emitter.emit('foo', { a: 'b' })4142// working with handler references:43function onFoo() {}44emitter.on('foo', onFoo) // listen45emitter.off('foo', onFoo) // unlistenInitial commit! 🚀Jason Millerbefore adoptione77a42c
lines 46–8446```4748* * *4950## API5152### mitt5354Mitt: Tiny (~200b) functional event emitter / pubsub.5556Returns **Mitt** 5758#### on5960Register an event handler for the given type.6162**Parameters**6364- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all events65- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to the given event6667#### off6869Remove an event handler for the given type.7071**Parameters**7273- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"`74- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove7576#### emit7778Invoke all handlers for the given type.79If present, `"*"` handlers are invoked prior to type-matched handlers.8081**Parameters**8283- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke84- `event` **\[Any]** An event object, passed to each handler