Skip to content
Myr

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 file

Take it

Spruce up the readme 🌲Jason Millerbefore adoption44362fe

lines 1–7why, one step back
1<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>

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 8–9
8
9

Spruce up the readme 🌲Jason Millerbefore adoption44362fe

line 10why, one step back
10## Why Mitt?

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 11
11

Spruce up the readme 🌲Jason Millerbefore adoption44362fe

lines 12–13why, one step back
12- **Microscopic:** weighs less than 200 bytes gzipped
13- **Useful:** a wildcard `"*"` event type listens to all events

Merge pull request #18 from pmg103/patch-1Jason Millerbefore adoption927ddfa

line 14why, one step back
14- **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 back
15- **Functional:** methods don't rely on `this`
16- **Great Name:** somehow [mitt](https://npm.im/mitt) wasn't taken

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 17
17

Add a design note, hopefully addressing #10 👍Jason Millerbefore adoption0d51fe3

lines 18–19why, one step back
18> 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.
19

Spruce up the readme 🌲Jason Millerbefore adoption44362fe

line 20why, one step back
20

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 21–23
21* * *
2223

Spruce up the readme 🌲Jason Millerbefore adoption44362fe

lines 24–45why, one step back
24## Usage
2526After 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) // unlisten

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 46–84
46```
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