Skip to content
Myr

README.md

This file

Path

README.md

88 lines · 3.1 KiB · Markdown

Last changed by

fix: switch Set with array; update tests; drop toLowerCase()tunnckoCorebefore adoption0c8202b

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

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

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

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 19–20
19* * *
20

Spruce up the readme 🌲Jason Millerbefore adoption44362fe

lines 21–42why, one step back
21## Usage
2223After installing via `npm install --save mitt`:2425```js26import mitt from 'mitt'2728let emitter = mitt()2930// listen to an event31emitter.on('foo', e => console.log('foo', e) )3233// listen to all events34emitter.on('*', (type, e) => console.log(type, e) )3536// fire an event37emitter.emit('foo', { a: 'b' })3839// working with handler references:40function onFoo() {}41emitter.on('foo', onFoo) // listen42emitter.off('foo', onFoo) // unlisten

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 43–54
43```
4445* * *4647## API4849### mitt5051Mitt: Tiny (~200b) functional event emitter / pubsub.5253Returns **Mitt** 54

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

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

Initial commit! 🚀Jason Millerbefore adoptione77a42c

line 56
56

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

lines 57–69why, one step back
57Invoke all handlers for the given type.
58If present, `"*"` handlers are invoked prior to type-matched handlers.5960**Parameters**6162- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke63- `arg1` **\[Any]** A value (first argument), passed to each handler64- `arg2` **\[Any]** A value (second argument), passed to each handler6566Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the `mitt` instance for chaining6768### on69

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 70–74
70Register an event handler for the given type.
7172**Parameters**7374- `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 75why, one step back
75- `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 76
76

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

line 77why, one step back
77Returns **[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 78
78

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

lines 79–80why, one step back
79### off
80

Initial commit! 🚀Jason Millerbefore adoptione77a42c

lines 81–87
81Remove an event handler for the given type.
8283**Parameters**8485- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"`86- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove87

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

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