Skip to content
Myr

README.md

This file

Path

README.md

97 lines · 3.2 KiB · Markdown

Last changed by

resolve readme conflicttunnckoCorebefore adoptione361ab5

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

line 8
8

Spruce up the readme 🌲Jason Millerbefore adoption44362fe

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 10
10

resolve readme conflicttunnckoCorebefore adoptione361ab5

lines 11–15why, one step back
11- **Microscopic:** weighs less than 200 bytes gzipped
12- **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 taken

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 16
16

resolve readme conflicttunnckoCorebefore adoptione361ab5

line 17why, one step back
17> 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 back
18

resolve readme conflicttunnckoCorebefore adoptione361ab5

lines 19–27why, one step back
19
20## Examples & Demos2122<a href="http://codepen.io/developit/pen/rjMEwW?editors=0110">23 <b>Preact + Mitt Codepen Demo</b>24 <br>25 <img src="https://i.imgur.com/CjBgOfJ.png" width="278" alt="preact + mitt preview">26</a>27

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 28–29
28* * *
29

Spruce up the readme 🌲Jason Millerbefore adoption44362fe

lines 30–51why, one step back
30## Usage
3132After installing via `npm install --save mitt`:3334```js35import mitt from 'mitt'3637let emitter = mitt()3839// listen to an event40emitter.on('foo', e => console.log('foo', e) )4142// listen to all events43emitter.on('*', (type, e) => console.log(type, e) )4445// fire an event46emitter.emit('foo', { a: 'b' })4748// working with handler references:49function onFoo() {}50emitter.on('foo', onFoo) // listen51emitter.off('foo', onFoo) // unlisten

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 52–61
52```
5354* * *5556## API5758### mitt5960Mitt: Tiny (~200b) functional event emitter / pubsub.61

resolve readme conflicttunnckoCorebefore adoptione361ab5

line 62why, one step back
62Returns **Mitt**

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 63
63

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 64why, one step back
64#### emit

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 65
65

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 66–78why, one step back
66Invoke all handlers for the given type.
67If present, `"*"` handlers are invoked prior to type-matched handlers.6869**Parameters**7071- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke72- `arg1` **\[Any]** A value (first argument), passed to each handler73- `arg2` **\[Any]** A value (second argument), passed to each handler7475Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining7677### on78

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 79–83
79Register an event handler for the given type.
8081**Parameters**8283- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all events

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 84why, one step back
84- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 85
85

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 86why, one step back
86Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 87
87

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

lines 88–89why, one step back
88### off
89

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 90–96
90Remove an event handler for the given type.
9192**Parameters**9394- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"`95- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove96

refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9

line 97why, one step back
97Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining