README.md
This file
Path
README.md
96 lines · 3.2 KiB · Markdown
Last changed by
readme, re-run build processtunnckoCorebefore adoption19a32dc
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>8Spruce up the readme 🌲Jason Millerbefore adoption44362fe
line 9why, one step back9## Why Mitt?10readme, re-run build processtunnckoCorebefore adoption19a32dc
lines 11–15why, one step back11- **Microscopic:** weighs less than 200 bytes gzipped12- **Useful:** a wildcard `"*"` event type listens to all events13- **Familiar:** same names & ideas as [Node's EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)14- **Functional:** methods don't rely on `this`15- **Great Name:** somehow [mitt](https://npm.im/mitt) wasn't taken16resolve readme conflicttunnckoCorebefore adoptione361ab5
line 17why, one step back17> Mitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.Add a design note, hopefully addressing #10 👍Jason Millerbefore adoption0d51fe3
line 18why, one step back18resolve readme conflicttunnckoCorebefore adoptione361ab5
lines 19–26why, one step back19## Examples & Demos2021<a href="http://codepen.io/developit/pen/rjMEwW?editors=0110">22 <b>Preact + Mitt Codepen Demo</b>23 <br>24 <img src="https://i.imgur.com/CjBgOfJ.png" width="278" alt="preact + mitt preview">25</a>26Initial commit! 🚀Jason Millerbefore adoptione77a42c
lines 27–2827* * *28Spruce up the readme 🌲Jason Millerbefore adoption44362fe
lines 29–50why, one step back29## Usage3031After installing via `npm install --save mitt`:3233```js34import mitt from 'mitt'3536let emitter = mitt()3738// listen to an event39emitter.on('foo', e => console.log('foo', e) )4041// listen to all events42emitter.on('*', (type, e) => console.log(type, e) )4344// fire an event45emitter.emit('foo', { a: 'b' })4647// working with handler references:48function onFoo() {}49emitter.on('foo', onFoo) // listen50emitter.off('foo', onFoo) // unlistenInitial commit! 🚀Jason Millerbefore adoptione77a42c
lines 51–6051```5253* * *5455## API5657### mitt5859Mitt: Tiny (~200b) functional event emitter / pubsub.60readme, re-run build processtunnckoCorebefore adoption19a32dc
line 61why, one step back61Returns **Mitt** 62refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
line 63why, one step back63#### emit64refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
lines 65–77why, one step back65Invoke all handlers for the given type.66If present, `"*"` handlers are invoked prior to type-matched handlers.6768**Parameters**6970- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke71- `arg1` **\[Any]** A value (first argument), passed to each handler72- `arg2` **\[Any]** A value (second argument), passed to each handler7374Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining7576### on77Initial commit! 🚀Jason Millerbefore adoptione77a42c
lines 78–8278Register an event handler for the given type.7980**Parameters**8182- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all eventsrefactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
line 83why, one step back83- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event84refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
line 85why, one step back85Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining86refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
lines 87–88why, one step back87### off88Initial commit! 🚀Jason Millerbefore adoptione77a42c
lines 89–9589Remove an event handler for the given type.9091**Parameters**9293- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"`94- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove95refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
line 96why, one step back96Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining