README.md
This file
Path
README.md
86 lines · 2.6 KiB · Markdown
Last changed by
Add demo, change support to IE9+Jason Millerbefore adoption84642dd
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?10Spruce up the readme 🌲Jason Millerbefore adoption44362fe
lines 11–12why, one step back11- **Microscopic:** weighs less than 200 bytes gzipped12- **Useful:** a wildcard `"*"` event type listens to all eventsMerge pull request #18 from pmg103/patch-1Jason Millerbefore adoption927ddfa
line 13why, one step back13- **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 14–15why, one step back14- **Functional:** methods don't rely on `this`15- **Great Name:** somehow [mitt](https://npm.im/mitt) wasn't taken16Add demo, change support to IE9+Jason Millerbefore adoption84642dd
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 back18Spruce up the readme 🌲Jason Millerbefore adoption44362fe
line 19why, one step back19Add demo, change support to IE9+Jason Millerbefore adoption84642dd
line 20why, one step back20## Examples & Demos21Add demo, change support to IE9+Jason Millerbefore adoption84642dd
lines 22–24why, one step back22[Preact + Mitt Codepen Demo](http://codepen.io/developit/pen/rjMEwW?editors=0110)2324* * *25Spruce up the readme 🌲Jason Millerbefore adoption44362fe
lines 26–47why, one step back26## Usage2728After installing via `npm install --save mitt`:2930```js31import mitt from 'mitt'3233let emitter = mitt()3435// listen to an event36emitter.on('foo', e => console.log('foo', e) )3738// listen to all events39emitter.on('*', (type, e) => console.log(type, e) )4041// fire an event42emitter.emit('foo', { a: 'b' })4344// working with handler references:45function onFoo() {}46emitter.on('foo', onFoo) // listen47emitter.off('foo', onFoo) // unlistenInitial commit! 🚀Jason Millerbefore adoptione77a42c
lines 48–8648```4950* * *5152## API5354### mitt5556Mitt: Tiny (~200b) functional event emitter / pubsub.5758Returns **Mitt** 5960#### on6162Register an event handler for the given type.6364**Parameters**6566- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to listen for, or `"*"` for all events67- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to the given event6869#### off7071Remove an event handler for the given type.7273**Parameters**7475- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Type of event to unregister `handler` from, or `"*"`76- `handler` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Handler function to remove7778#### emit7980Invoke all handlers for the given type.81If present, `"*"` handlers are invoked prior to type-matched handlers.8283**Parameters**8485- `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event type to invoke86- `event` **\[Any]** An event object, passed to each handler