README.md
This file
Path
README.md
205 lines · 6.1 KiB · Markdown
Last changed by
Merge masterJason Millerbefore adoption9539abe
History of this fileTake it
Spruce up the readme 🌲Jason Millerbefore adoption44362fe
line 1why, one step back1<p align="center">Merge branch 'master' into refactorJason Millerbefore adoption4799316
line 2why, one step back2 <img src="https://i.imgur.com/BqsX9NT.png" width="300" height="300" alt="mitt">Spruce up the readme 🌲Jason Millerbefore adoption44362fe
line 3why, one step back3 <br>update readme badgesJason Millerbefore adoption8d987a7
lines 4–6why, one step back4 <a href="https://www.npmjs.org/package/mitt"><img src="https://img.shields.io/npm/v/mitt.svg" alt="npm"></a>5 <img src="https://github.com/developit/mitt/workflows/CI/badge.svg" alt="build status">6 <a href="https://unpkg.com/mitt/dist/mitt.js"><img src="https://img.badgesize.io/https://unpkg.com/mitt/dist/mitt.js?compression=gzip" alt="gzip size"></a>Spruce up the readme 🌲Jason Millerbefore adoption44362fe
line 7why, one step back7</p>8Merge branch 'master' into refactorJason Millerbefore adoption4799316
line 9why, one step back9# Mitt10Merge branch 'master' into refactorJason Millerbefore adoption4799316
lines 11–12why, one step back11> Tiny 200b functional event emitter / pubsub.12readme, re-run build processtunnckoCorebefore adoption19a32dc
lines 13–17why, one step back13- **Microscopic:** weighs less than 200 bytes gzipped14- **Useful:** a wildcard `"*"` event type listens to all events15- **Familiar:** same names & ideas as [Node's EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)16- **Functional:** methods don't rely on `this`17- **Great Name:** somehow [mitt](https://npm.im/mitt) wasn't taken18Merge branch 'master' into refactorJason Millerbefore adoption4799316
line 19why, one step back19Mitt 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 20why, one step back20Merge branch 'master' into refactorJason Millerbefore adoption4799316
line 21why, one step back21## Table of Contentsresolve readme conflicttunnckoCorebefore adoptione361ab5
line 22why, one step back22Update docs.Jason Millerbefore adoption07f8819
lines 23–29why, one step back23- [Install](#install)24- [Usage](#usage)25- [Examples & Demos](#examples--demos)26- [API](#api)27- [Contribute](#contribute)28- [License](#license)29Merge branch 'master' into refactorJason Millerbefore adoption4799316
line 30why, one step back30## Installresolve readme conflicttunnckoCorebefore adoptione361ab5
line 31why, one step back31Merge branch 'master' into refactorJason Millerbefore adoption4799316
line 32why, one step back32This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.33Merge branch 'master' into refactorJason Millerbefore adoption4799316
lines 34–36why, one step back34```sh35$ npm install --save mitt36```Spruce up the readme 🌲Jason Millerbefore adoption44362fe
line 37why, one step back37Merge branch 'master' into refactorJason Millerbefore adoption4799316
line 38why, one step back38Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else:Spruce up the readme 🌲Jason Millerbefore adoption44362fe
line 39why, one step back39Merge branch 'master' into refactorJason Millerbefore adoption4799316
lines 40–52why, one step back40```javascript41// using ES6 modules42import mitt from 'mitt'4344// using CommonJS modules45var mitt = require('mitt')46```4748The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):4950```html51<script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>52```Update docs.Jason Millerbefore adoption07f8819
line 53why, one step back53Merge branch 'master' into refactorJason Millerbefore adoption4799316
lines 54–57why, one step back54You can find the library on `window.mitt`.5556## Usage57Spruce up the readme 🌲Jason Millerbefore adoption44362fe
lines 58–60why, one step back58```js59import mitt from 'mitt'60Fixing TypeScript usage. Fixes #60 (#67)David Khourshidbefore adoption0f4e8f0
line 61why, one step back61const emitter = mitt()Spruce up the readme 🌲Jason Millerbefore adoption44362fe
lines 62–71why, one step back6263// listen to an event64emitter.on('foo', e => console.log('foo', e) )6566// listen to all events67emitter.on('*', (type, e) => console.log(type, e) )6869// fire an event70emitter.emit('foo', { a: 'b' })71Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
lines 72–74why, one step back72// clearing all events73emitter.all.clear()74Spruce up the readme 🌲Jason Millerbefore adoption44362fe
lines 75–78why, one step back75// working with handler references:76function onFoo() {}77emitter.on('foo', onFoo) // listen78emitter.off('foo', onFoo) // unlistenInitial commit! 🚀Jason Millerbefore adoptione77a42c
lines 79–8079```80Typescript definitions (#25)Severinbefore adoptiond63b350
line 81why, one step back81### Typescript1.1.0Jason Millerbefore adoptionf147246
line 82why, one step back82Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 83–84why, one step back83Set `"strict": true` in your tsconfig.json to get improved type inference for `mitt` instance methods.84Typescript definitions (#25)Severinbefore adoptiond63b350
line 85why, one step back85```tsFixing TypeScript usage. Fixes #60 (#67)David Khourshidbefore adoption0f4e8f0
line 86why, one step back86import mitt from 'mitt';Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
lines 87–88why, one step back8788type Events = {Merge masterJason Millerbefore adoption9539abe
lines 89–91why, one step back89 foo: string;90 bar?: number;91};Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 92why, one step back92Merge masterJason Millerbefore adoption9539abe
lines 93–110why, one step back93const emitter = mitt<Events>(); // inferred as Emitter<Events>9495emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'9697emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)98```99100Alternatively, you can use the provided `Emitter` type:101102```ts103import mitt, { Emitter } from 'mitt';104105type Events = {106 foo: string;107 bar?: number;108};109110const emitter: Emitter<Events> = mitt<Events>();Typescript definitions (#25)Severinbefore adoptiond63b350
lines 111–112why, one step back111```112Merge branch 'master' into refactorJason Millerbefore adoption4799316
line 113why, one step back113## Examples & Demos114Merge branch 'master' into refactorJason Millerbefore adoption4799316
lines 115–119why, one step back115<a href="http://codepen.io/developit/pen/rjMEwW?editors=0110">116 <b>Preact + Mitt Codepen Demo</b>117 <br>118 <img src="https://i.imgur.com/CjBgOfJ.png" width="278" alt="preact + mitt preview">119</a>Typescript definitions (#25)Severinbefore adoptiond63b350
lines 120–121why, one step back120121* * *Merge branch 'master' into refactorJason Millerbefore adoption4799316
line 122why, one step back122Initial commit! 🚀Jason Millerbefore adoptione77a42c
lines 123–124123## API124Docs update.Jason Millerbefore adoptiona0578b7
lines 125–126why, one step back125<!-- Generated by documentation.js. Update this documentation by updating the source code. -->126Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 127–129why, one step back127#### Table of Contents128129- [mitt](#mitt)Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
line 130why, one step back130- [all](#all)Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
lines 131–137why, one step back131- [on](#on)132 - [Parameters](#parameters)133- [off](#off)134 - [Parameters](#parameters-1)135- [emit](#emit)136 - [Parameters](#parameters-2)137Initial commit! 🚀Jason Millerbefore adoptione77a42c
lines 138–141138### mitt139140Mitt: Tiny (~200b) functional event emitter / pubsub.141Add generic types and update tests (#107)Jack Franklinbefore adoption5116df4
line 142why, one step back142Returns **Mitt** Merge branch 'expose-all' of https://github.com/jaylinski/mittJason Millerbefore adoption0f41ead
lines 143–146why, one step back143144### all145146A Map of event names to registered handler functions.Docs update.Jason Millerbefore adoptiona0578b7
line 147why, one step back147refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
lines 148–149why, one step back148### on149Initial commit! 🚀Jason Millerbefore adoptione77a42c
lines 150–151150Register an event handler for the given type.151Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 152why, one step back152#### Parameters153Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 154why, one step back154- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to listen for, or `'*'` for all eventsDocs update.Jason Millerbefore adoptiona0578b7
line 155why, one step back155- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** Function to call in response to given event156refactor: fixes #2, #6, #12 and #13tunnckoCorebefore adoptiona6190e9
lines 157–158why, one step back157### off158159Remove an event handler for the given type.Add support for mitt.off("type"), which removes all handlers of a given typeJason Millerbefore adoption4cce9cb
line 160why, one step back160If `handler` is omitted, all handlers of the given type are removed.161Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 162why, one step back162#### Parameters163Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 164why, one step back164- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** Type of event to unregister `handler` from, or `'*'`Add support for mitt.off("type"), which removes all handlers of a given typeJason Millerbefore adoption4cce9cb
line 165why, one step back165- `handler` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)?** Handler function to removeDocs update.Jason Millerbefore adoptiona0578b7
lines 166–169why, one step back166167### emit168169Invoke all handlers for the given type.Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 170why, one step back170If present, `'*'` handlers are invoked after type-matched handlers.Docs update.Jason Millerbefore adoptiona0578b7
line 171why, one step back171Merge branch 'master' into patch_123Jason Millerbefore adoption53eb689
line 172why, one step back172Note: Manually firing '\*' handlers is not supported.Add note about manually firing "*" handlersJason Millerbefore adoptioncfd246c
line 173why, one step back173Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 174why, one step back174#### ParametersDocs update.Jason Millerbefore adoptiona0578b7
line 175why, one step back175Migrate to TypeScript and use Map (#99)Jack Franklinbefore adoptionbe5237f
line 176why, one step back176- `type` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) \| [symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))** The event type to invokeDocs update.Jason Millerbefore adoptiona0578b7
line 177why, one step back177- `evt` **Any?** Any value (object is recommended and powerful), passed to each handlerMerge branch 'master' into refactorJason Millerbefore adoption4799316
lines 178–179why, one step back178179## ContributeUpdate docs.Jason Millerbefore adoption07f8819
line 180why, one step back180Merge branch 'master' into refactorJason Millerbefore adoption4799316
lines 181–182why, one step back181First off, thanks for taking the time to contribute!182Now, take a moment to be sure your contributions make sense to everyone else.Add Standard Version for Semantic Releases (#43)Abhi Aiyerbefore adoptiond728e90
line 183why, one step back183Merge branch 'master' into refactorJason Millerbefore adoption4799316
line 184why, one step back184### Reporting IssuesUpdate docs.Jason Millerbefore adoption07f8819
line 185why, one step back185Merge branch 'master' into refactorJason Millerbefore adoption4799316
lines 186–189why, one step back186Found a problem? Want a new feature? First of all see if your issue or idea has [already been reported](../../issues).187If don't, just open a [new clear and descriptive issue](../../issues/new).188189### Submitting pull requestsUpdate docs.Jason Millerbefore adoption07f8819
line 190why, one step back190Merge branch 'master' into refactorJason Millerbefore adoption4799316
lines 191–192why, one step back191Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.192Update docs.Jason Millerbefore adoption07f8819
lines 193–201why, one step back193- Fork it!194- Clone your fork: `git clone https://github.com/<your-username>/mitt`195- Navigate to the newly cloned directory: `cd mitt`196- Create a new branch for the new feature: `git checkout -b my-new-feature`197- Install the tools necessary for development: `npm install`198- Make your changes.199- Commit your changes: `git commit -am 'Add some feature'`200- Push to the branch: `git push origin my-new-feature`201- Submit a pull request with full remarks documenting your changes.Merge branch 'master' into refactorJason Millerbefore adoption4799316
lines 202–204why, one step back202203## License204Clean up repo rootJason Millerbefore adoption4a8e0a5
line 205why, one step back205[MIT License](https://opensource.org/licenses/MIT) © [Jason Miller](https://jasonformat.com/)